School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,124 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,998 people online right now. Registration is fast and FREE... Join Now!




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

 

[Visual C++][Dark GDK] 2D Game / Controling Ship In 0 Gravity, Trying To Fix The Calculation Of Position In 2D Space + 0 Gravity

vikkman

30 Oct, 2009 - 08:34 AM
Post #1

New D.I.C Head
*

Joined: 29 Oct, 2009
Posts: 5

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:
IPB Image
EXAMPLE OF WHAT I WISH TO ACHIEVE:
IPB Image

MY while(LoopGDK()) Contents:
CODE

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 Oct, 2009 - 08:57 AM

User is offlineProfile CardPM
+Quote Post


Aeternalis

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

30 Oct, 2009 - 01:38 PM
Post #2

D.I.C Regular
***

Joined: 13 Jul, 2009
Posts: 273



Thanked: 25 times
My Contributions
Is the "UP" key your thrust?

Hmm misread your code.. looking at it further.


Aet

This post has been edited by Aeternalis: 30 Oct, 2009 - 01:51 PM
User is offlineProfile CardPM
+Quote Post

vikkman

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

30 Oct, 2009 - 02:19 PM
Post #3

New D.I.C Head
*

Joined: 29 Oct, 2009
Posts: 5

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.
User is offlineProfile CardPM
+Quote Post

L0CKnL0aD7

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

30 Oct, 2009 - 04:30 PM
Post #4

New D.I.C Head
*

Joined: 30 Oct, 2009
Posts: 4


My Contributions
I don't really understand your question, it works fine no? wink2.gif you can perfectly move your ship in an arc trajectory.
User is offlineProfile CardPM
+Quote Post

jbeme

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

30 Oct, 2009 - 10:35 PM
Post #5

New D.I.C Head
*

Joined: 30 Oct, 2009
Posts: 21



Thanked: 1 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

vikkman

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

31 Oct, 2009 - 02:30 AM
Post #6

New D.I.C Head
*

Joined: 29 Oct, 2009
Posts: 5

QUOTE(L0CKnL0aD7 @ 30 Oct, 2009 - 04:30 PM) *

I don't really understand your question, it works fine no? wink2.gif 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 Oct, 2009 - 02:31 AM
User is offlineProfile CardPM
+Quote Post

L0CKnL0aD7

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

31 Oct, 2009 - 04:56 AM
Post #7

New D.I.C Head
*

Joined: 30 Oct, 2009
Posts: 4


My Contributions
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.gif I'll hopefully get one in time. tongue.gif
User is offlineProfile CardPM
+Quote Post

vikkman

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

31 Oct, 2009 - 05:32 AM
Post #8

New D.I.C Head
*

Joined: 29 Oct, 2009
Posts: 5

I'll sure stick my eyes to this thread untill you, me or anyone else finds a solution.
User is offlineProfile CardPM
+Quote Post

TyVeryMuch

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

2 Nov, 2009 - 11:29 PM
Post #9

New D.I.C Head
Group Icon

Joined: 24 Mar, 2007
Posts: 33



Thanked: 2 times
Dream Kudos: 50
My Contributions
nvm my bad

This post has been edited by TyVeryMuch: 3 Nov, 2009 - 05:58 AM
User is offlineProfile CardPM
+Quote Post

vikkman

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

3 Nov, 2009 - 05:31 AM
Post #10

New D.I.C Head
*

Joined: 29 Oct, 2009
Posts: 5

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

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: 3 Nov, 2009 - 05:32 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:05PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month