Trouble with speed and vectors
Posted
by
Eegabooga
on Game Development
See other posts from Game Development
or by Eegabooga
Published on 2012-03-26T09:06:14Z
Indexed on
2012/03/26
11:44 UTC
Read the original article
Hit count: 256
I'm working on adding bullets to my game. Right now I can shoot bullets in the direction that I would like from a ship by getting the ship's angle:
int speed = 5;
int dx = -(cos(degreesToRadians(ship.angle)) * speed); // rate of change in the x direction
int dy = -(sin(degreesToRadians(ship.angle)) * speed); // rate of change in the y direction
bulletPosition.addX(dx); // addX(dx) is simply bulletPosition.x += dx
bulletPosition.addY(dy);
The ship is pretty much the exact same thing, except I use the += operator:
int dx += -(cos(degreesToRadians(angle)) * 0.15)
int dy += -(sin(degreesToRadians(angle)) * 0.15);
shipPosition.addX(dx);
shipPosition.addY(dy);
I would like to be able to add the ship's velocity to the bullet's velocity, but I'm a little confused as to how should get the speed from the ship's vector. I thought that adding the ship's dx
to the bullet's dx
like int dx = -(cos(degreesToRadians(ship.angle)) * speed * dx)
would work because I'm adding the rate of change of the ship to the rate of change of the bullet, but that doesn't work. So here's the final question: How can I get the speed of my ship and apply it to my bullet's speed?
Thanks in advance for all help :)
© Game Development or respective owner