开发者

Trying to figure out the new rotation angle for rocket

开发者 https://www.devze.com 2023-03-11 10:48 出处:网络
i have this code float angle = rocket.rotation; float vx = sin(angle * M_PI / 180) * xVelocity; float vy = cos(angle * M_PI / 180) * yVelocity;

i have this code

float angle = rocket.rotation;
float vx = sin(angle * M_PI / 180) * xVelocity;
float vy = cos(angle * M_PI / 180) * yVelocity;
CGPoint direction = ccp(vx, vy);
[rocket setPosition:开发者_C百科ccpAdd(rocket.position, direction)];

yVelocity -= 0.2;

basically it fires a rocket in the direction i set it to face. This works fine the rocket goes up then comes down fine. I now need to make the rockets rotation change with the new direction i am setting so that the rocket is rotated correctly for the way it is flying. How can i work out the new angle i need to rotate the rocket properly? I'm assuming i can use the new direction to create this new angle but im not sure how. Thanks


I'm not really sure what you mean, but the first part is not accurate

float angle = rocket.rotation;
float vx = sin(angle * M_PI / 180) * xVelocity;
float vy = cos(angle * M_PI / 180) * yVelocity;

To say how fast the rocket is going and in what direction, you have two options:

  1. Tell direction(angle) and speed(how fast it goes in m/second)
  2. Tell horizontal velocity (how fast it goes horizontally in m/second) and vertical velocity(m/second)

If you have the velocity(horizontal and vertical), you can calculate the speed and direction. And also, if you have the angle and speed, you can calculate the velocity(horizontal and vertical). Your code seems to be calculating the velocity, from direction and velocity instead of direction and speed.

float angle = rocket.rotation;
xVelocity = sin(angle * M_PI / 180) * speed;
yVelocity = cos(angle * M_PI / 180) * speed;

yVelocity -= 0.2f;//apply gravity

//now we need to find the new angle and speed again
//speed is easy, Pythagoras helps
speed = sqrt(xVelocity*xVelocity + yVelocity*yVelocity);
//angle is more difficult, luckily atan2 solves this:
angle = atan2(yVelocity,xVelocity);

//now we can update the rocket
//Sorry, but I don't know COCOS...
0

精彩评论

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