Search code examples
c++mathcocos2d-iphonecocos2d-xtrigonometry

shooting bullet based on an angle from a point


what i am doing is scanning an area with a sprite of a straight line that i rotate 20 degrees back and forth

when i tap my action button i want to shoot a bullet in the direction of rotation of the sprite

i get the angle by doing a

sprite->getRotation();

and i have the point of my unit available, lets say it is (0,0)

i'm guessing i need to find a point on the line, but i don't know the math behind it.

is this even possible?


Solution

  • Given that you know the velocity of your bullet (pixels/second), let me assume you will call it v. That it takes s seconds to transverse your screen. And that x represents the position of your bullet at axis x (same to y variable), you can actualize both variables using this simple trigonometry:

    x = 0; // initial position. You say that it start at (0,0)
    y = 0;
    
    for (int i = 0; i < s; i++) {
       sleep(1);    // look in unistd.h
       x += v*cos(angle);  // include math.h
       y += v*sin(angle);  // angle in radian, of course
       // draw your sprite here, at (x, y)
    }