Unexpected behavior with LinearVelocity

Topics: Developer Forum, User Forum
Oct 21, 2011 at 1:03 AM

I'm writing an XNA game for the windows phone. I want the player (a fish) to be able to jump in and out of water with increasing height on each consecutive jump. Its working perfectly so far but the problem is that the height of the fish seems to be limited to about Y=100 based on my current gravity setting (0, -20). I can change gravity and the fish goes higher, but it seems unnatural. Here is my code:

 

	    
            //GRAVITY (no gravity under water surface)
            if (bodyFixture.Body.Position.Y >= ((GraphicsDevice.Viewport.Height / 2)/ppm))
            {
                world.Gravity = -Vector2.UnitY * 20f;
            }
            else
            {
                world.Gravity = new Vector2(0,0);
            }
	    //MOVEMENT(Redirects movement to correct angle and speed based on angle of impact with water, speed is incremented slowly)
            Vector2 direction = new Vector2(-(float)Math.Sin(bodyFixture.Body.Rotation * ppm), (float)Math.Cos(bodyFixture.Body.Rotation * ppm));
            
            if (bodyFixture.Body.Position.Y <= surfaceY)
            {
                bodyFixture.Body.LinearVelocity = direction * speed;
            }
            
            float distanceFromSurface = Math.Abs(bodyFixture.Body.Position.Y - surfaceY);
            float directionSlope = direction.Y / -direction.X;
            float velocitySlope = bodyFixture.Body.LinearVelocity.Y / bodyFixture.Body.LinearVelocity.X;

            //BOOST (If the fish lands at a good angle a speed boost is given, at a poor angle speed is reset)(this part isn't perfect but speed does increase on landings)
            if (TIMEOUT != true)
            {
                if (distanceFromSurface <= .6 & (Math.Abs(directionSlope + velocitySlope) < 3 | (Math.Abs(directionSlope) > 5 & Math.Abs(velocitySlope) > 5)) & (direction.Y < 0) & (bodyFixture.Body.LinearVelocity.Y < 0))
                {
                    speed += 10;
                    TIMEOUT = true;
                }
                else if (distanceFromSurface <= .6 & bodyFixture.Body.LinearVelocity.Y < 0 & (Math.Abs(directionSlope + velocitySlope) >= 3 | direction.Y >= 0))
                {
                    speed = 15;
                    TIMEOUT = true;
                }
            }
            if (distanceFromSurface > .6)
                TIMEOUT = false;

Based on observing the variables during gameplay, i've noticed that upon exiting the water (and reentering gravity) LinearVelocity.Y is reduced to about 60, no matter what speed it was traveling beforehand (100, 200, 300). Basically the body reaches a point that it cannot travel any higher no matter the speed it exits the water. Why is this occuring? Are there limits on velocity? does gravity bring everything down to a speed of 60? How can I create a workaround? Any help is greatly appreciated.

 

Oct 21, 2011 at 5:42 PM
Edited Oct 21, 2011 at 5:44 PM

yes there are limits. you have to scale physics down (~ 25-50)

hope this helps

Edit: why you do multiply the rotation with a value? (bodyFixture.Body.Rotation * ppm)