Jump to content
  • Advertisement
Sign in to follow this  
ontheheap

grrr... asteroids is kicking my butt =(

This topic is 5468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, so I decided to make an asteroids clone as my third game (thinking it would be really simple). Now I am totally stuck as to how I am supposed to rotate the ship. I'm using SDL and rotozoom to rotate the image. Here is what I have so far: - to move in the direction ship is facing - ship.xvel -= sinf(ship.angle) * THRUSTER_FORCE; ship.yvel -= cosf(ship.angle) * THRUSTER_FORCE; - to move in opposite direction - ship.xvel += sinf(ship.angle) * THRUSTER_FORCE; ship.yvel += cosf(ship.angle) * THRUSTER_FORCE; (note, THRUSTER_FORCE == 1.5) And that works fine (I see no reason why it wouldn't) And to change the angle I'm doing this: ship.angle += ROTATION_SPEED; and ship.angle -= ROTATION_SPEED; depending on if the player presses left or right. (note, ROTATION_SPEED == 1, ie 1 degree of rotation) And here is how I'm updating the ships x/y coords: ship.x += ship.xvel; ship.y += ship.yvel; From looking at these I would think they would work, but the results I'm getting are not even close to what it should be doing! What am I missing / doing wrong? I understand the math, but I don't think I'm using it right or something. - heap

Share this post


Link to post
Share on other sites
Advertisement
Perhaps your angle value needs some modulus :) Also, perhaps SDL doesn't rotate around the center, but (0, 0) of your sprite. Dunno though.

Thermo

Share this post


Link to post
Share on other sites
Hrm, I don't know about the modulus, but the fact that sdl is rotating around 0,0 and not the center makes the output I'm getting make more sense. Even when I don't use rotozoom (I'm just using the bitmap to see how the thrust corresponds with the direction, and it still looks like crap).

Here, I'll post the relevant source:

My ship class (just the declaration)

class Ship {

public:
Ship(float x, float y, float velX, float velY, int newSpeed, float newAngle);
~Ship();

void setX(float x);
float getX();

void setY(float y);
float getY();

void setSpeed(int newSpeed);
int getSpeed();

void setAngle(float newAngle);
float getAngle();

void setVelX(float velX);
float getVelX();

void setVelY(float velY);
float getVelY();


private:
float xpos;
float ypos;
int speed;
float xvelocity;
float yvelocity;
float angle;
};



Source for user input and movement of ship


if(keys[SDLK_UP])
{
if(ship->getVelX() + ship->getVelY() > -10)
{
ship->setVelX(ship->getVelX() - sinf(ship->getAngle()) * THRUSTER_FORCE);
ship->setVelY(ship->getVelY() - cosf(ship->getAngle()) * THRUSTER_FORCE);
}
}
if(keys[SDLK_DOWN])
{
if(ship->getVelX() + ship->getVelY() < 10)
{
ship->setVelX(ship->getVelX() + sinf(ship->getAngle()) * THRUSTER_FORCE);
ship->setVelY(ship->getVelY() + cosf(ship->getAngle()) * THRUSTER_FORCE);
}
}

if(keys[SDLK_LEFT])
{
ship->setAngle(ship->getAngle() - ROTATION_SPEED);
}

if(keys[SDLK_RIGHT])
{
ship->setAngle(ship->getAngle() + ROTATION_SPEED);
}



Any help would be greatly appreciated!!

Share this post


Link to post
Share on other sites
I think its supposed to limit the speed of the ship...

however thats not the way to do it
should be something like this though....

if(keys[SDLK_UP])
{
ship->setVelX(ship->getVelX() - sinf(ship->getAngle()) * THRUSTER_FORCE);
ship->setVelY(ship->getVelY() - cosf(ship->getAngle()) * THRUSTER_FORCE);

// If max speed has been breached
float speed = ship->getVelX()*ship->getVelX() + ship->getVelY()*ship->getVelY();
if(speed > MAX_SPEED * MAX_SPEED)
{
// Take the sqrt to find the actual speed
speed = sqrt(speed);
ship->setVelX(ship->getVelX()*MAX_SPEED/speed);
ship->setVelY(ship->getVelY()*MAX_SPEED/speed);
}
}

Share this post


Link to post
Share on other sites
Also you have ROTATION_SPEED=1
Since sinf and cosf take arguments in radians (I think), this is way too large. Try something like PI/180.

Share this post


Link to post
Share on other sites
Oh yea! I forgot that cos/sin functions in C/C++ take radians and not degrees. That makes a huge difference with the amount of rotation, but now rotozoom is tearing up my image. I don't think I'm going to use rotozoom, because I really don't like it very much. Instead I'm going to use 12 images and just have the ship rotate 30 degrees at a time. If that looks too choppy I'll keep going down until it looks ok. Thanks.

- heap

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!