开发者

Move bitmap in certain direction Android

Ok so I am working on a game in Android and right now I have a bitmap that I have drawn at the center of the screen. I can rotate the bitmap left and right by certain degrees using the Matrix class. The bitmap is a picture of a ship so when the user wants to move forward, I want the ship to move at the current angle that the ship is rotated at. Any ideas ab开发者_开发百科out how I should go about doing this?


Ok well with a bunch of trial and error and some reading up on trig I have managed to solve my own question. The vector that holds the current location of the ship has an X and a Y. What I then need to do, was based on the current rotation of the ship calculate a speed vector and then add that speed vector to the position vector.

speedX = (float) Math.sin(rotation*(Math.PI/180)) * speed;
speedY = (float) -Math.cos(rotation*(Math.PI/180)) * speed;
x += speedX;
y += speedY;

The rotation is in degrees so they needed to be converted to radians. Also speed is the actual speed of the ship and is applied to each speed vector. Hope that will help someone having the same problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜