Thread: Angle of Trajectory?

  1. #1
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310

    Angle of Trajectory?

    I'm trying to create a bullet class that stores the bullets current position (x,y,z) and its trajectory. Traditionally I've used 3 variables called xtraject, ytraject, and ztraject where each loop xtraject was added to x and same for y/z. So in order to get the bullet travelling upper right at a 45 degree angle xtraject and ytraject would be 1. I'm want to have bullets that can fire at arbitrary angles, perhaps just have an angle variable (maybe I would need 2?) to control which way it goes and then a speed to say how fast its moving.

    Do I use polar coordinates to do this? What about quaternions?

    Thanks,

    //napKIN
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You really need to learn how to use the search button, buddy.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Although that post is really not about the same thing. There is another post here about bullets that covers it better.

    Essentially:


    Code:
    #define PI 3.14159
    #define DEGTORAD(angle) ((double)angle*PI/180.0)
    
    struct point2D
    {
      double x;
      double y;
    };
    
    struct Bullet
    {
       point2D Pos;
       point2D Vel;
       double speed;
       int active;
    };
    
    void FireBullet(point2D Start,int angle,double speed,Bullet *newbullet)
    {
      newbullet->x=Start.x;
      newbullet->y=Start.y;
      newbullet->vel.x=cos(DEGTORAD(angle));
      newbullet->vel.y=sin(DEGTORAD(angle));
      newbullet->speed=speed;
      newbullet->active=TRUE;
    }
    
    //Assuming we have an array of Bullet objects called Bullets
    void UpdateBullets
    {
      for (int i=0;i<NumBullets;i++)
      {
         if (Bullets[i].active)
         {
            Bullets[i].x+=(Bullets[i].vel.x*Bullets[i].speed);
            Bullets[i].y+=(Bullets[i].vel.y*Bullets[i].speed);
         }
       }
    }
    This is only for 2D, for 3D you must do a little more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sine (Sin) Algorithm Help
    By StaticKyle in forum C Programming
    Replies: 52
    Last Post: 05-13-2008, 08:37 AM
  2. Object not moving to angle
    By mike_g in forum C Programming
    Replies: 1
    Last Post: 06-22-2007, 09:30 AM
  3. Tenchu 2 Remake - Camera angle?
    By cboard_member in forum Game Programming
    Replies: 3
    Last Post: 01-02-2007, 09:47 PM
  4. Help with Programming Assignment
    By JHaney in forum C++ Programming
    Replies: 18
    Last Post: 11-08-2005, 03:45 AM
  5. Working out the tangent of an angle in c++
    By Robert602 in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 06:34 PM