Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  odd question ...floating/jumping..sorta  (Read 1797 times)
0 Members and 1 Guest are viewing this topic.
Offline Halvie

Junior Newbie





« Posted 2008-08-24 07:24:14 »

Hey, I know this is an odd question, but I have asked on other forums and haven't gotten much of a response. The game I am working on is actually in XNA, but seeing that the game I am wondering about is written in Java I was hoping someone might be able to help out a bit. I am trying to make a game that has a similar flying mechanic to ( http://www.terabytelabs.net/shyguys-cave-of-death/#comments). My problem is that when I release the spacebar the ship will continue to climb up for a bit before starting to descend....and overall it doesn't seem to be nearly as snappy as the shyguy game.


If it would be any help here is my current code...again in C#

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
if (currentState.IsKeyDown(Keys.Space) == true)
            {

                sPos.Y += .4f;
                velocity = 2;





            }




            if (currentState.IsKeyDown(Keys.Space) == false)
            {
                sPos.Y -= .8f;
                velocity = -1;
            }

        }

        public void Update(GameTime theGameTime)
        {
            KeyboardState aCurrentKeyboardState = Keyboard.GetState();

            spritePosition.X = spritePosition.X + 5;
            velocity -= acceleration * (float)theGameTime.ElapsedGameTime.TotalSeconds;


            sPos.Y -= velocity * (float)theGameTime.ElapsedGameTime.TotalSeconds;

           
            spritePosition.Y -= sPos.Y;

            Movement(aCurrentKeyboardState);






        }



Obviously I don't expect to get a response written in C#...just a general idea about what I might be doing wrong.

Thanks for the help,
Troy
Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #1 - Posted 2008-08-24 08:08:47 »

I think you made it more complicated than it has to be and this code snippet can help you.


1  
2  
3  
4  
5  
6  
7  
8  
float gravity=2;
float accy=gravity*2;
     
void loop(){  
   if(thrusting)spd.y-=accy*delta();            
   spd.y+=gravity*delta();
   pos.y+=spd.y*delta();
}

lots of sillystupid games: http://www.emaggame.com
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2008-08-24 18:06:15 »

Yeah, you really just want to think about acceleration and velocity, you never want to deal directly with position. Although you don't do it entirely, you do when (I think) you're accounting for gravity, which is the root of your problem.

Notice how in Hansdekampf's response he had the speed changed by gravity and the position changed by the speed. This is basically what I'm talking about, although let me explain why:

1) If you adjust the location manually, you can't get gradual increases and decreases of velocity.
2) The actual world doesn't work that way - to get the best looking physics, emulate real physics.
3) Your program will not work for different computers. If your FPS changes, it's going to go faster or slower.

So, here's what you want to do (including what you're already doing):
1) Give the helicopter a current velocity for X and Y.
2) Every timestep, measure how long it has been since the last timestep. If it's been too short, have your thread yield until a certain minimum interval has been reached. Then, determine a value (delta) that is a fraction of your target framerate. If you're current framerate is faster, delta will be < 1, whereas if it's slower, delta is > 1.
3) Apply the acceleration of gravity times delta to the Y component of your helicopter's velocity.
4) Apply the acceleration of the button presses times delta to the appropriate components of the helicopter's velocity.
5) Move your helicopter its resulting velocity times delta.

Make sense?

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #3 - Posted 2008-08-24 19:26:07 »

I made a little game with code from above (CaveCopter, but more ufo than copter).
One nice thing is that you can use the same gravity (or a different one for levels/planets) for all objects and just need to play with the acceleration of your ship, in my game it is simply twice the gravity (like above).


lots of sillystupid games: http://www.emaggame.com
Offline Hansdampf

Senior Devvie


Projects: 3


too offending?


« Reply #4 - Posted 2008-08-26 13:46:53 »

I couldn't answer because your inbox seems to be full..
the delta() function returns the time in seconds for one loop update, in my case:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
private float lastdelta=0;
private float time=time();
float delta(){
    return lastdelta; //lastdelta is measured elsewhere
}

static float time(){
   return YourTimer.getCurrentTime(); //System/Language dependant
}


void mainLoop(){
   lastdelta=time()-time;
   time=time();
   loop();
}

//u can just use
float delta(){
    return 1f/60; //if u are sure your game steadily runs at 60 fps
}


sorry for potential typos etc.

lots of sillystupid games: http://www.emaggame.com
Offline Halvie

Junior Newbie





« Reply #5 - Posted 2008-08-27 01:47:36 »

Thanks for the help Hansdampf ! For some reason I can only get one pm at a time.

Now that the movement is pretty much done with I have to start with the background and collision. Know of any tutorials or something like that that might help?

Thanks,
Troy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (65 views)
2014-11-26 15:22:04

toopeicgaming1999 (58 views)
2014-11-26 15:20:36

toopeicgaming1999 (10 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (28 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!