[Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Trying To Fix The Calculation Of Position In 2D Space + 0 Gravity

Page 1 of 1

9 Replies - 4430 Views - Last Post: 03 November 2009 - 06:31 AM Rate Topic: -----

#1 vikkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-October 09

[Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Post icon  Posted 30 October 2009 - 09:34 AM

Hy, my name is viKKmaN.
After 5 days of continous reading the dark gdk tutorials + documentation i managed to get to this project:
- 2D Space Game
- Implemented 0 Gravity Myself
- Ship Moving Forward
- Ship Turning Left / Right

My problem is with calculating the velocity after spinning the ship.

EXAMPLE OF WHAT HAPPENS:
Posted Image
EXAMPLE OF WHAT I WISH TO ACHIEVE:
Posted Image

MY while(LoopGDK()) Contents:
float PosX = 0, PosY = 0, VelocityX = 0, VelocityY = 0, ShipAngle = 0, ShipAngleR = 0;
dbLoadImage("ship.bmp", 1);
dbSprite(2, PosX, PosY, 1);
while(LoopGDK())
	{
		if(dbEscapeKey())
			break;
		if(dbSpaceKey())
		{
			VelocityX = 0;
			VelocityY = 0;
		}
		if(dbUpKey())
		{
			ShipAngleR = ShipAngle;
			if(VelocityX > -2)
			{
				VelocityX -=  Speed/100;
			}
			if(VelocityY > -2)
			{
				VelocityY -=  Speed/100;
			}
		}
		if(dbRightKey())
		{
			ShipAngle += Turning/10;
			if(ShipAngle > 360 || ShipAngle < -360)
				ShipAngle = 0;
		}
		if(dbLeftKey())
		{
			ShipAngle -= Turning/10;
			if(ShipAngle > 360 || ShipAngle < -360)
				ShipAngle = 0;
		}
		PosX += cos((ShipAngleR+90)*(PI/180))*(VelocityX);
		PosY += sin((ShipAngleR+90)*(PI/180))*(VelocityY);
		dbSprite(2, PosX, PosY, 2);
		dbOffsetSprite(2, dbSpriteWidth(2)/2, dbSpriteHeight(2)/2);
		dbRotateSprite(2, ShipAngle);		
		dbSync();
	}


This post has been edited by vikkman: 30 October 2009 - 09:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

#2 Aeternalis   User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 30 October 2009 - 02:38 PM

Is the "UP" key your thrust?

Hmm misread your code.. looking at it further.


Aet

This post has been edited by Aeternalis: 30 October 2009 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 vikkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-October 09

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 30 October 2009 - 03:19 PM

Yes. The up key is my thrust, left/right for steering and as you can see the program simulates 0 gravity. My problem is with calculating the new thrust after changing the ships angle, the new ship angle difering from the direction the ship was originaly heading. As i sayd the thrust remains maximum (2) if i change the direction of the ship. I don't know how to calculate the new thrust based on the diference of angles, that is why i am asking for help.
Was This Post Helpful? 0
  • +
  • -

#4 L0CKnL0aD7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-October 09

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 30 October 2009 - 05:30 PM

I don't really understand your question, it works fine no? ;) you can perfectly move your ship in an arc trajectory.
Was This Post Helpful? 0
  • +
  • -

#5 jbeme   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 30-October 09

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 30 October 2009 - 11:35 PM

You would first need to track the current angle of the ship. Then figure out the sideways force being applied to make it turn. Then write an equation figuring out how the side force affects the forward momentum. If I understand your problem.
Was This Post Helpful? 0
  • +
  • -

#6 vikkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-October 09

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 31 October 2009 - 03:30 AM

View PostL0CKnL0aD7, on 30 Oct, 2009 - 04:30 PM, said:

I don't really understand your question, it works fine no? ;) you can perfectly move your ship in an arc trajectory.

Yes, it moves perfectly in an arc trajectory when i press "UP" + "LEFT"/"RIGHT" keys at the same time.
But as you can see i added a variable ShipAngleR which i use for retrieving the new ship angle when i press "UP".
This means i can move my ship in the direction it's headed, than rotate it however i want using "LEFT"/"RIGHT" keys(without afecting the course of the ship), than press thrust to move in the new direction.

Leme give you a hint. I am trying to recreate the 0 gravity simulated in the game "star sonata" if any of you played it, and the controling there.

This post has been edited by vikkman: 31 October 2009 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

#7 L0CKnL0aD7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-October 09

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 31 October 2009 - 05:56 AM

Ah, now I get your problem:P

What you should do:
Store the vector of your old velocity and combine it with your new velocity vector, but I cant relay find a good solution for this. :crazy: I'll hopefully get one in time. :P
Was This Post Helpful? 0
  • +
  • -

#8 vikkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-October 09

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 31 October 2009 - 06:32 AM

I'll sure stick my eyes to this thread untill you, me or anyone else finds a solution.
Was This Post Helpful? 0
  • +
  • -

#9 TyVeryMuch   User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 24-March 07

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 03 November 2009 - 12:29 AM

nvm my bad

This post has been edited by TyVeryMuch: 03 November 2009 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#10 vikkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-October 09

Re: [Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity

Posted 03 November 2009 - 06:31 AM

Fixed it. But now i have a diferent problem. Setting the maximum speed for the ship. & keeping it on screen.
Here's the code:

while(LoopGDK())
	{
		if(dbEscapeKey())
			break;
		if(dbControlKey()) // This is for stopping the ship in emergency times
		{
			if(VelocityX < -0.5)
				VelocityX += Thrust/100;
			else
				if(VelocityX > 0.5)
					VelocityX -= Thrust/100;
				else
					VelocityX = 0;
			if(VelocityY > 0.5)
				VelocityY -= Thrust/100;
			else
				if(VelocityY < -0.5)
					VelocityY += Thrust/100;
				else
					VelocityY = 0;
		}
		if(dbUpKey())
		{
			ShipAngle = ShipAngleNew;
				VelocityX -= sin((ShipAngle+180)*(PI/180))*(Thrust/100);
				VelocityY += cos((ShipAngle+180)*(PI/180))*(Thrust/100);
		}
		if(dbRightKey())
		{
			ShipAngleNew += Turning/10;
			if(ShipAngleNew > 360)
				ShipAngleNew -= 360;
		}
		if(dbLeftKey())
		{
			ShipAngleNew -= Turning/10;
			if(ShipAngleNew < -360)
				ShipAngleNew += 360;
		}
		PosX += VelocityX;
		PosY += VelocityY;
		dbSprite(2, PosX, PosY, 2);
		dbRotateSprite(2, ShipAngleNew);
		dbSync();
	}


This post has been edited by vikkman: 03 November 2009 - 06:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1