#### Archived

This topic is now archived and is closed to further replies.

# need help with moving ship

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

## Recommended Posts

Hi ya. I m pretty new to programming and i need help with my asteriods game. Im having trouble with moving my ship in a straight line in the direction the ship is facing and i would like to know how to do some sort of acceration. I can move it in the different directions but its not always in a stright line and the speed of the ship varies. Below are the functions for move and rotating the ship. Any help at all would be brill thanks.Please try and keep it simple int cShip::TurnShipLeft() { if(ShipAngle == 0) { ShipAngle = ShipAngle + 360; } //Change Ship angle to reflect the turn ShipAngle -= 10; //the amount of degrees we have turned return 10; } int cShip::TurnShipRight() { //Change Ship angle to reflect the turn ShipAngle += 10; //the amount of degrees we have turned return 10; } void cShip::DrawShip() { int iX, iY; int ClipX = 0; int ClipY = 0; switch(ShipAngle) { case 0: case 10: case 20: case 30: case 40: case 50: case 60: case 70: case 80: case 360: if(ShipAngle == 360) ShipAngle = 0; ClipX = ((ShipAngle)/10) * 80; ClipY = 0; break; case 90: case 100: case 110: case 120: case 130: case 140: case 150: case 160: case 170: ClipX = ((ShipAngle-90)/10) * 80; ClipY = 80; break; case 180: case 190: case 200: case 210: case 220: case 230: case 240: case 250: case 260: ClipX = ((ShipAngle-180)/10) * 80; ClipY = 160; break; case 270: case 280: case 290: case 300: case 310: case 320: case 330: case 340: case 350: ClipX = ((ShipAngle-270)/10) * 80; ClipY = 240; break; } m_ship.Draw(GetMainApp()->m_pBackBuffer,XPos,XPos,ClipX,ClipY,80,80); } void cShip::MoveShip() { //Dont have them all dont. There must be a far more efficent way switch(ShipAngle) { case 10: case 20: case 30: YPos = YPos - 5; XPos = XPos + (ShipAngle/10); break; case 40: case 50: YPos = YPos - 4; XPos = XPos + (ShipAngle/10); break; case 60: case 70: case 80: YPos = YPos - 3; XPos = XPos + (ShipAngle/10); break; case 90: XPos = XPos + 5; break; case 100: YPos = YPos + ((ShipAngle/10)/10); //YPos = YPos + 1; XPos = XPos + 5; break; case 110: YPos = YPos + 1; XPos = XPos + 5; break; case 120: YPos = YPos + 2; XPos = XPos + 5; break; case 130: YPos = YPos + 3; XPos = XPos + 6; break; case 140: YPos = YPos + 4; XPos = XPos + 6; break; case 180: YPos = YPos + 5; break; case 270: XPos = XPos - 5; break; case 280: YPos = YPos - 3; XPos = XPos - 8; break; case 290: YPos = YPos - 3; XPos = XPos - 7; break; case 300: YPos = YPos - 3; XPos = XPos - 6; break; case 310: YPos = YPos - 4; XPos = XPos - 5; break; case 320: YPos = YPos - 4; XPos = XPos - 4; break; case 330: YPos = YPos - 5; XPos = XPos - 3; break; case 340: YPos = YPos - 5; XPos = XPos - 2; break; case 350: YPos = YPos - 5; XPos = XPos - 1; break; case 0: case 360: YPos = YPos - 5; break; } }

##### Share on other sites
Wow, i know you are a beginer, but your code is horrible. let me try to help you.

First of all what is your education level, have you heard of sin and cos? If not research them. Also instead of doing lots of case statements that flow into each other, just do like this.

while (angle >= 360){   angle -= 360;}while (angle < 0){   angle += 360;}if (angle < 90){  do this;}else if (angle < 180){  do this;}else if (angle < 270){  od this;}else{  do this;}

Having a turn left and turn right function is just a bad idea, so do like this.

ship::turn(int direction ){  angle += direction * 10;}

##### Share on other sites
i think he''s having the trouble with drawing the ship at such-and-such an angle, not getting the angle so much (at first glance at least).

If that''s the case, what i would do is have an array (or vector) of 36 sprites (one for each 10 deg interval):

Sprite sprites[36];

Then, when you need to know which one to use, instead of switch(angle)''ing, you just do this (psuedo-code):

BlitSprite( sprites[ int(ships_angle / 10) ] );

and voila! no more stupid case statements. In general, if you find yourself using switch a lot, there usually is a better way.

I''m not sure if it''s a viable opion for realtime games, but you might also want to look into a rotozoom function of some kind that would rotate the actual graphic by some amount instead of using pre-rendered sprites (but i''d use the prerendered sprites personally :-).

Here is how i get particle movement with gravity for a particle system i made. I''m just pasting it here to give a general idea:

		// get time and number of pixels to add since the last time		float time_from_birth = time - birth_tick - (interval*n);		int pixels =  int( (int)speed * (int)time_from_birth ) / 1000;		//std::cout << "Step " << n << ": time from birth = " << time_from_birth << ", pixels = " << pixels << "\n";		if (pixels < 0) { break; }		// basic right-angle trig		//int tempangle = angle % 90;		int xoff = int( cos( DegToRad(angle) ) * pixels );		int yoff = int( sin( DegToRad(angle) ) * pixels );		// adjust for "gravity"		yoff += int (  pow( (float)weight, time_from_birth/1000.0 )  );		int x = startx + xoff;		int y = starty + yoff;

##### Share on other sites
Look through this stuff

//add a velx and vely float to your cShip class, make sure they are initialized to zeroint cShip::TurnShip(int value){  ShipAngle += value;  // - for left, + for right  while (ShipAngle >= 360){ShipAngle -= 360;}  while (ShipAngle < 0){ShipAngle += 360;}  return value; //not sure why u need this}void cShip::DrawShip(){int iX, iY;int ClipX = 0;int ClipY = 0;if (ShipAngle < 90) //im not sure what all this clip stuff means, but it can probably be replaced with sin and cos work{  ClipX = ((ShipAngle)/10) * 80;  ClipY = 0;}else if (ShipAngle < 180){ClipX = ((ShipAngle-90)/10) * 80;ClipY = 80;}else if (ShipAngle < 270){ClipX = ((ShipAngle-180)/10) * 80;ClipY = 160;}elsebes{ClipX = ((ShipAngle-270)/10) * 80;ClipY = 240;}m_ship.Draw(GetMainApp()->m_pBackBuffer,XPos,XPos,ClipX,ClipY,80,80);}void cShip::ThrustShip(float amount){  velx += sin(ShipAngle) * amount; //you will have to convert shipangle to radians most likely here  vely += cos(ShipAngle) * amount; //same here}void cShip::UpdatePositiong(float deltatime){  xpos += velx * deltatime;  ypos += vely * deltatime;}

And brush up on your math skills, programming is very mathematical (not always in the traditional sense), expecialy game programming.

##### Share on other sites
i can draw the ship at every angle no prob. The clipx and clipy are the position on my bitmap that contains every single angle that the ship is in.

##### Share on other sites
I understand that, but you asked about acceleration, i threw in what i believe to be more proper coding. Look through the rest of it and you should find your answers. If I changed some crutial part, ignore it.

##### Share on other sites
well I would simplify turning to this:

#define TURN_LEFT -10
#define TURN_RIGHT 10

int Ship::turn(int deg)
{
shipAngle =(shipAngle+deg)%360;
return deg;
}

movement can all be done using cos and sin.

-----------------------------------------------------
Writer, Programer, Cook, I'm a Jack of all Trades
Current Design project: Ambitions Slave

[edited by - TechnoGoth on April 14, 2004 1:38:59 AM]

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

• 9
• 11
• 15
• 21
• 26