开发者

How to do 2d vector movement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_StackOverflow社区

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

Improve this question

my onscreen object has a var rotation (in degrees).

how do i fix it so when i push up arrow it move forwards and not just x++ and y++?


Not 100% sure that this is what you want, but I think you want to do:

x += speed * cos(angle);
y += speed * sin(angle);

Where angle is the rotation of your object, and x and y are its coordinates. speed is the speed of your object.


x += sin(rotation) * speed;
y += cos(rotation) * speed;

But it depends on the orientation of your rotation. This code will work for rotation orientated up (north) at 0 degrees, moving clock-wise.


Use trigonometry.

You have the angle and the length of movement (which is your hypotenuse) so you should be able to use sin or cos to calculate the amount of x and y movement required.


Just to clarify, since this is C#, you'd want to do this

double radians = (Math.PI/180)*angleInDegrees;
x += speed * Math.Cos(radians);
y += speed * Math.Sin(radians);

As other posters have mentioned, this is based on using trigonometry to apply a rotation to a speed vector (in this case, the 0 degrees vector is pointing to the right).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜