Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 :)

share|improve this question
2  
It's a lot easier if you use vectors. –  Edin M. Mar 26 '12 at 9:21
add comment

1 Answer

up vote 5 down vote accepted

You do want to add the ship's dx/dy to the bullet's dx/dy, but you want addition, not multiplication.

bulletDx = -(cos(degreesToRadians(bulletHeading)) * speed) + shipDx;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.