Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Asteroids

  • 31-03-2007 02:39PM
    #1
    Registered Users, Registered Users 2 Posts: 86,683 ✭✭✭✭


    I have the ship rotating ok and it moves...but the direction it moves in is never predictable: just anytime you rotate the ship it seems to pick a new random vector to accelerate on. :mad:
    struct vector{
            float x;
            float y;
    };
    
    //return the vector v about an angle,
    vector Rotate(vector v, float angle){
            vector temp;
    
            temp.x=v.x*cos(angle) - v.y*sin(angle);
            temp.y=v.x*sin(angle) + v.y*cos(angle);
    
    
            return temp ;
    }
    
    // return a vector multiplied by a scalar
    vector Mult(vector v, float s){
            vector temp;
    
            temp.x=v.x*s;
            temp.y=v.y*s;
    
            //do math
            return temp;
    }
    //return the addition of two vectors
    vector Add(vector a, vector b){
            vector c;
            c.x=a.x+b.x;
            c.y=a.y+b.y;
    
            //do math
            return c;
    }
    
    
    //////////////////////////////////////////////////////////////////////////////////////
    //Ship
    //////////////////////////////////////////////////////////////////////////////////////
    // the ship struct will store info about our space ship
    const int SHIP_NUM_POINTS=4; //number of points in ship's polygon
    struct Ship{
            vector points[SHIP_NUM_POINTS]; //points of ship's polygon
            vector position;        // ships location
            vector velocity;        // direction the ship is currently moving
            vector direction;       // direction ship is currently pointing
            float rateOfAccel;      // how fast can we accelerate
            float friction;         // how fast do we slow down
            float rotation;         // how fast do we spin
            int radius;             // the size of the circle which contains the ship
            bool alive;             // whether the ship is alive or dead
    }theShip;                       // our global ship structure
    
    void InitShip(Ship* ship){
            //initialise the ships position, velocity and polygon points
            ship->points[0].x=0; //points of ship's polygon (Rotational Origin)
            ship->points[0].y=0; //
            ship->points[1].x=15; //points of ship's polygon (Port Fin)
            ship->points[1].y=10; //
            ship->points[2].x=0; //points of ship's polygon (Prow/Gunmount)
            ship->points[2].y=-30; //
            ship->points[3].x=-15; //points of ship's polygon (starboard Fin)
            ship->points[3].y=10; //
    
            ship->direction.x= 1;
            ship->direction.y= 0;
    
            ship->velocity.x =0;
            ship->velocity.y =0;
    
            ship->rateOfAccel = 0.1;
    
            ship->position.x=100;
            ship->position.y=100;
            ship->rotation = 0.0; //in float Radians
    }
    
    void UpdateShip(Ship* ship){
            // update the ship for the next frame
            /* things to do
            1. check left/right keys and turn(rotate) ship
            2. check up key and accelarate ship
            3. set new position based on current velocity
            4. wrap around edges
            */
    
    
            if(theKeys.up==true)
            {
                    //ship->position.x++;
                    //ship->position.y++; //**MODIFY: SET ACCELERATION VECTOR
                    ship->rateOfAccel += 0.001;
                    ship->velocity = Add(ship->velocity, ship->direction); //change direction of movement
            }
            else
            {
                    //ship->rateOfAccel -= 0.001;
            }
    
            if(theKeys.left==true)
            {
                    ship->rotation -= 0.15;
                    ship->direction = Rotate(ship->direction, ship->rotation);
    
            }
            if(theKeys.right==true)
            {
                    ship->rotation += 0.15;
                    ship->direction= Rotate(ship->direction, -ship->rotation);
    
            }
    
    
    
    }
    
    void DrawShip(Ship* ship){
    
            // draw the ships polygon at current position
            vector pos;
    
    
    
        //    pos=Rotate(ship->direction,ship->rotation);
        //    pos=Mult(pos,ship->rateOfAccel);
         //   pos=Add(pos,ship->position);
            ship->velocity = Mult(ship->velocity,ship->rateOfAccel);//Magnitude calculation
            ship->position = Add(ship->position,ship->velocity);//position change
    
    
    
    
            pos=Rotate(ship->points[0],ship->rotation);
            pos=Add(pos,ship->position);
            Form1->Canvas->MoveTo(pos.x, pos.y);
    
            //pos=Rotate(ship->points[1],ship->rotation);
            //pos=Add(pos,ship->position);
            //Form1->Canvas->LineTo(pos.x, pos.y);
            
            pos=Rotate(ship->points[2],ship->rotation);
            pos=Add(pos,ship->position);
            Form1->Canvas->LineTo(pos.x, pos.y);
    
            //pos=Rotate(ship->points[3],ship->rotation);
            //pos=Add(pos,ship->position);
            //Form1->Canvas->LineTo(pos.x, pos.y);
    
            //pos=Rotate(ship->points[0],ship->rotation);
            //pos=Add(pos,ship->position);
            //Form1->Canvas->LineTo(pos.x, pos.y);
    
    
    
           // pos=Rotate(ship->direction,ship->rotation);
           // pos=Add(
    
    
    
    
    }
    

    any tips would be great, thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    I've only had a quick look so I could be wrong, but I don't think you should be accumulating both the rotation and the direction when you press left or right; either reset the direction each time you press the key and work out the full rotation, or think of rotation as just being a per-frame value, which is 0 unless you press a key. I'd recommend the former so you don't lose precision after a whole load of rotations.


Advertisement