Computer Science Canada

Having Trouble Implementing Gravity

Author:  JakeTakeLake [ Mon Feb 11, 2013 5:08 pm ]
Post subject:  Having Trouble Implementing Gravity

What is it you are trying to achieve?
realistic gravity


What is the problem you are having?
my sprite is jumping really fast, then coming back down slowly. i want it to be the same


Describe what you have tried to solve this problem
ive tried using tutorials but im still confused as to how to implement it into my code. i know how to make the sprite fall back down, but its the initial jump im confused about, i dont know how to make it smooth, without allowing them to only jump once when y=0


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


setscreen ("graphics:700;400,offscreenonly")

%VARIABLES
%Images
var background :int := Pic.FileNew ("pics\\background.bmp")
var walk_backward :int := Pic.FileNew ("pics\\walk_backward.bmp")
var walk_forward :int := Pic.FileNew ("pics\\walk_forward.bmp")
var stand :int := Pic.FileNew ("pics\\stand.bmp")
var jump :int := Pic.FileNew ("pics\\jump.bmp")
var kick :int := Pic.FileNew ("pics\\kick.bmp")
var punch :int := Pic.FileNew ("pics\\punch.bmp")
var block :int := Pic.FileNew ("pics\\block.bmp")
%Sprites
var block_sprite :int := Sprite.New (block)
var punch_sprite :int := Sprite.New (punch)
var kick_sprite :int := Sprite.New (kick)
var jump_sprite :int := Sprite.New (jump)
var stand_sprite :int := Sprite.New (stand)
var walk_forward_sprite :int := Sprite.New(walk_forward)
var walk_backward_sprite :int := Sprite.New(walk_backward)
%Other
var chars : array char of boolean
var x,y: int :=0
var xvelocity,yvelocity: real :=1
var gravity: real := 1.25
var ground: boolean
var centered: boolean :=false



loop
    Input.KeyDown (chars)

%KEYS

        if chars ('w') and y=0 then
            yvelocity+= 40
        end if
        if chars ('d') then
            xvelocity+= 3
        end if
        if chars ('a') then
            xvelocity-= 3
        end if
       
%PHYSICS       
% Simulates gravity.     
        if y > 0 then
            yvelocity:= yvelocity - gravity
           
% Makes the ground.           
        elsif yvelocity < 0 then
            y:=0
        end if
       
        x:= round(xvelocity)
        y:= round(yvelocity)

%SPRITES
Sprite.SetPosition (block_sprite,x,y,centered)
Sprite.SetPosition (punch_sprite,x,y,centered)
Sprite.SetPosition (kick_sprite,x,y,centered)
Sprite.SetPosition (jump_sprite,x,y,centered)
Sprite.SetPosition (stand_sprite,x,y,centered)
Sprite.SetPosition (walk_forward_sprite,x,y,centered)
Sprite.SetPosition (walk_backward_sprite,x,y,centered)
Sprite.SetHeight (block_sprite,1)
Sprite.SetHeight (punch_sprite,1)
Sprite.SetHeight (kick_sprite,3)
Sprite.SetHeight (jump_sprite,2) 
Sprite.SetHeight (stand_sprite,1)
Sprite.SetHeight (walk_forward_sprite,2)
Sprite.SetHeight (walk_backward_sprite,2)
       
       
%Sprite if then statements
         
        if y=0 then
            Sprite.Show (stand_sprite)
        elsif chars('w') then
            Sprite.Hide (stand_sprite)
        end if
       
        if chars ('d') then
            Sprite.Show (walk_forward_sprite) Sprite.Hide (punch_sprite)
        else
            Sprite.Hide (walk_forward_sprite)
        end if
       
        if chars ('a') then
            Sprite.Show (walk_backward_sprite) Sprite.Hide (punch_sprite)
        else
            Sprite.Hide (walk_backward_sprite)
        end if
       
        if y > 0 then
            Sprite.Show (jump_sprite) Sprite.Hide (walk_forward_sprite) Sprite.Hide (walk_backward_sprite) Sprite.Hide (block_sprite) 
        else
            Sprite.Hide (jump_sprite)
        end if
       
        if y > 0 and chars ('g') then
            Sprite.Show (kick_sprite) Sprite.Hide (jump_sprite) Sprite.Hide (walk_forward_sprite) Sprite.Hide (walk_backward_sprite)
        else
            Sprite.Hide (kick_sprite)
        end if
       
        if y=0 and chars ('g') then
            Sprite.Show (punch_sprite) Sprite.Hide (walk_forward_sprite) Sprite.Hide (walk_backward_sprite) Sprite.Hide (stand_sprite)
        else
            Sprite.Hide (punch_sprite)
        end if
       
        if chars ('h') then
            Sprite.Show (block_sprite) Sprite.Hide (walk_forward_sprite) Sprite.Hide (walk_backward_sprite) Sprite.Hide (stand_sprite)
        else
            Sprite.Hide (block_sprite)
        end if
%PERFORMANCE
        Pic.Draw(background,0,0,0)
        View.Update
        delay(20)
        cls
end loop



Please specify what version of Turing you are using
4.1.1

Author:  Insectoid [ Mon Feb 11, 2013 6:30 pm ]
Post subject:  RE:Having Trouble Implementing Gravity

code:
% Makes the ground.           
        elsif yvelocity < 0 then
            y:=0
        end if
       
        x:= round(xvelocity)
        y:= round(yvelocity)


This doesn't do what you think it does.

Also, instead of having loads of sprites, just use one and change the picture on it with Sprite.ChangePic() or Sprite.Animate().

Also also, here's your actual problem:
code:
if chars ('w') and y=0 then
            yvelocity+= 40
        end if


If you hold W, the character will jump 40 pixels per frame. If you limit it so the character only jumps one time when W is pressed, your problem should be solved.

Author:  JakeTakeLake [ Mon Feb 11, 2013 7:57 pm ]
Post subject:  Re: RE:Having Trouble Implementing Gravity

Insectoid @ Mon Feb 11, 2013 wrote:


This doesn't do what you think it does.

Also, instead of having loads of sprites, just use one and change the picture on it with Sprite.ChangePic() or Sprite.Animate().

Also also, here's your actual problem:
code:
if chars ('w') and y=0 then
            yvelocity+= 40
        end if


If you hold W, the character will jump 40 pixels per frame. If you limit it so the character only jumps one time when W is pressed, your problem should be solved.


I did limit it. W only works when the sprite is at y=0.

Thanks for the advice though. I'll start using sprite.changepic

Author:  Insectoid [ Mon Feb 11, 2013 8:53 pm ]
Post subject:  RE:Having Trouble Implementing Gravity

Oh yes, I didn't see that. It's even in the code I linked. Lemme go slap myself in the face.

Think about this. Your screen is 400 pixels high. Your initial jump velocity is 40 pixels, and gravity is 1.25. Your delay is 20ms. The character reaches the top of the screen in 11 frames, taking 1/5th of a second. It takes 32 frames for velocity to reach zero (the top of the jump) and 64 frames to hit the ground again, which takes 1.2 seconds.

How do you fix this? Just tweak your gravity and jump velocity variables until it looks good. Otherwise, your code is just about fine.

Author:  JakeTakeLake [ Tue Feb 12, 2013 4:16 pm ]
Post subject:  Re: RE:Having Trouble Implementing Gravity

Insectoid @ Mon Feb 11, 2013 wrote:
Oh yes, I didn't see that. It's even in the code I linked. Lemme go slap myself in the face.

Think about this. Your screen is 400 pixels high. Your initial jump velocity is 40 pixels, and gravity is 1.25. Your delay is 20ms. The character reaches the top of the screen in 11 frames, taking 1/5th of a second. It takes 32 frames for velocity to reach zero (the top of the jump) and 64 frames to hit the ground again, which takes 1.2 seconds.

How do you fix this? Just tweak your gravity and jump velocity variables until it looks good. Otherwise, your code is just about fine.


What you're saying makes sense, but that's not what happens when I run it; it's hard to explain. I tried tweaking the variables, but the sprites disappear. I'm going to attach my new code (I took your advice with sprite.changepic) and I'll attach a zip file of my whole program, so you can see my problem.

Turing:

setscreen ("graphics:700;400,offscreenonly")

%VARIABLES
%Images
var background :int := Pic.FileNew ("pics\\background.bmp")
var walk_backward :int := Pic.FileNew ("pics\\walk_backward.bmp")
var walk_forward :int := Pic.FileNew ("pics\\walk_forward.bmp")
var stand :int := Pic.FileNew ("pics\\stand.bmp")
var jump :int := Pic.FileNew ("pics\\jump.bmp")
var kick :int := Pic.FileNew ("pics\\kick.bmp")
var punch :int := Pic.FileNew ("pics\\punch.bmp")
var block :int := Pic.FileNew ("pics\\block.bmp")
var sprite_action :int := Sprite.New (stand)

%Other
var chars : array char of boolean
var x,y: int :=0
var xvelocity,yvelocity: real :=1
var gravity: real := 1.25
var ground: boolean
var centered: boolean :=false



loop
    Input.KeyDown (chars)

%KEYS

        if chars ('w') and y=0 then
            yvelocity+= 40
        end if
        if chars ('d') then
            xvelocity+= 3
        end if
        if chars ('a') then
            xvelocity-= 3
        end if
       
%PHYSICS       
% Simulates gravity.     
        if y > 0 then
            yvelocity:= yvelocity - gravity
        end if
 
%Sprite Location       
        x:= round(xvelocity)
        y:= round(yvelocity)

%SPRITES
Sprite.SetPosition (sprite_action,x,y,centered)
Sprite.SetHeight (sprite_action,1)
Sprite.Show (sprite_action)     
%Sprite if then statements       
       
        if y=0 then
            Sprite.ChangePic (sprite_action, stand)
        end if
       
        if chars ('d') then
            Sprite.ChangePic (sprite_action, walk_forward)
        end if
       
        if chars ('a') and y=0 then
            Sprite.ChangePic (sprite_action, walk_backward)
        end if
       
        if y > 0 then
            Sprite.ChangePic (sprite_action, jump)
        end if
       
        if y > 0 and chars ('g') then
            Sprite.ChangePic (sprite_action, kick)
        end if
       
        if y=0 and chars ('g') and y=0 then
            Sprite.ChangePic (sprite_action, punch)
        end if
       
        if chars ('h') and y=0 then
            Sprite.ChangePic (sprite_action, block)       
        end if
%PERFORMANCE
        Pic.Draw(background,0,0,0)
        View.Update
        delay(20)
        cls
end loop

Author:  Insectoid [ Tue Feb 12, 2013 4:44 pm ]
Post subject:  RE:Having Trouble Implementing Gravity

code:
x:= round(xvelocity)
        y:= round(yvelocity)


This is a problem. Can you figure out what it is?

Author:  JakeTakeLake [ Tue Feb 12, 2013 5:11 pm ]
Post subject:  Re: RE:Having Trouble Implementing Gravity

Insectoid @ Tue Feb 12, 2013 wrote:
code:
x:= round(xvelocity)
        y:= round(yvelocity)


This is a problem. Can you figure out what it is?


well i need it to round the the numbers because the coords are integers. i'm not sure why that would make the character jump up fast and fall back down slowly

Author:  Dreadnought [ Tue Feb 12, 2013 7:03 pm ]
Post subject:  Re: Having Trouble Implementing Gravity

If interpreted in normal language the statements read "set position equal to velocity". Is that what they should read?

Author:  JakeTakeLake [ Wed Feb 13, 2013 5:21 pm ]
Post subject:  Re: RE:Having Trouble Implementing Gravity

Insectoid @ Tue Feb 12, 2013 wrote:
code:
x:= round(xvelocity)
        y:= round(yvelocity)


This is a problem. Can you figure out what it is?


Dreadnought @ Tue Feb 12, 2013 wrote:
If interpreted in normal language the statements read "set position equal to velocity". Is that what they should read?


ohhh, i had to add it. thanks guys


: