Public Group

# 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.

## 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 on other sites
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 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 on other sites
if (ship->getVelX() + ship->getVelY() > -10)...

what is this supposed to do?

##### 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 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 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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 16
• 13
• 14