根据旋转获取精灵前面的位置
我有一个根据触摸位置移动的精灵。我使用 box2d 进行碰撞,当精灵与另一个对象碰撞时,我将精灵旋转 180 度以远离碰撞,我的目标是将碰撞的精灵沿旋转方向移动 x 像素量,基本上给出反弹影响。
谁能告诉我如何根据旋转获得精灵前面 x 个像素的位置。
谢谢
I have got a sprite that moves based on touch location. I am using box2d for collision and when the sprite collides with another object i rotate the sprite 180 degree to face away from the collision and my aim is to move the sprite that collided x amount of pixels in the direction of rotation basically giving a bounce off affect.
Can anyone tell me how i can get a position which is x amount of pixels in front of the sprite based on the rotation.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用以下公式获得精灵所面向的方向:
它返回的矢量将准确指向您要计算的点。将其标准化,然后乘以您想要达到的像素数。那应该可以解决问题。
上面是 C# 代码,但您应该能够将其转换为您的语言。
You can get the direction your sprite is facing with the following formula:
The vector it returns will be pointing exactly towards the spot you are trying to calculate. Normalize it, then multiply it by the number of pixels you want to reach out to. That should do the trick.
The above is C# code, but you should be able to convert it to your language.
您可以使用精灵的 contentSize 或boundingBox 来计算应移动的像素量。考虑到节点锚定在它们的中心,您可以执行以下操作(仅在 x 轴上更改):
根据您需要将精灵移动到的方向以及您需要的 xAmount 来更改总和的符号。
干杯。
You can use the contentSize or boundingBox of the sprite to calculate the amount of pixels you should move. Considering that nodes are anchored in their center, you could do something like this (Changes on the x-axis only):
Change the signs of the sum according to the orientation you need to move the sprite towards to and the xAmount as you need.
Cheers.
您可以将本地点转换为全局点:
You can just convert local point to global: