odd question ...floating/jumping..sorta

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#


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

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

float gravity=2;
float accy=gravity*2;
		
void loop(){	
	if(thrusting)spd.y-=accy*delta();				
	spd.y+=gravity*delta();
	pos.y+=spd.y*delta();
}

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?

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

http://www.krautsoft.com/cave.jpg

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:

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.

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