Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Gravity and jumping
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hamid14




PostPosted: Sat Nov 28, 2009 3:58 pm   Post subject: Gravity and jumping

What is it you are trying to achieve?
Trying to make a picture "jump" up, then come down. Basically, im trying to make gravity.


What is the problem you are having?
Cant get the gravity to work right.


Describe what you have tried to solve this problem
I've tried making the y decrease after it reaches the max height for the jump, doesnt work.


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

Turing:

View.Set ("offscreenonly")
var x,y : int := 100
var chars : array char of boolean
loop
View.Update
cls
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
y :=y + 5
end if
if y >= 200 then
y := y - 5
end if
if y >= 100 and y <=105 then
y := 100
end if
end loop



Please specify what version of Turing you are using
4.1
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Sat Nov 28, 2009 4:29 pm   Post subject: Re: Gravity and jumping

Turing:

var y : int := 100
loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        y :=y + 5
    end if
    %STUFF
    if y >= 100 and y <=105 then
        y := 100
    end if
    %DRAW CHARACTER
end loop

based on those two conditions, will the value of y ever be anything other than 100 when the character is drawn?
highly




PostPosted: Sat Nov 28, 2009 5:03 pm   Post subject: Re: Gravity and jumping

Turing:

View.Set ("offscreenonly")
var x,y : int := 100
var chars : array char of boolean
loop
View.Update
cls
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
y :=y + 5
end if
if y >= 200 then
y := y - 5
end if
if y >= 100 and y <=105 then
y := 100
end if
end loop

gravity is a force, and is a constant. In this case, g(gravity) will always be a negative value acting on your character. But it wouldn't effect the y position of your character directly, it would effect that characters velocity. so y += yvel, and yvel is what is effected by keypresses

Turing:

%declaring variables
var x, y : int
var vx, vy : int
var g : int
var f : int
var j : boolean
x := 100
y := 200
vx := 0 % x velocity
vy := 0 % y velocity
g := 1 % g is gravity
j := true % boolean for whether or not "jump" is allowed
var chars : array char of boolean
loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and (j = true) then
        vy := 10 % accelerates upward to a jump
        j := false % so that it doesn't fly into space
    end if
    if y <= 200 then % 200 is where out "ground" is
        j := true
        vy := (vy * -1) - 1 % Bounce effect. Should be (vy*-1)*0.5, but that equation didn't work
        y := 200 % this means if our characetr is below "ground", then he gets put back on top of the ground
    end if
    if y > 200 then % this if makes it so you cannot jump in mid-air
        j := false
    end if
    if chars (KEY_RIGHT_ARROW) then % increases the x velocity
        vx += 5
    else
        if chars (KEY_LEFT_ARROW) then %decreases the x velocity
            vx -= 5
        else
            if chars (KEY_DOWN_ARROW) then % makes it so you can "stop" the character if going to fast, again I couldn't do friction becaue the equation didn't comply
                vx := 0
            end if
        end if
    end if
    if j = false then % this is our gravity effect. If he can't jump, it means he is above the ground. If he is above the ground, gravity would apply to him
        vy -= g
    end if
    x += vx % stating to move x velocity space son the x axis
    y += vy % same except for y
    drawoval (x, y, 4, 4, black) % drawing oval
    delay (50) % delay. if 0 everything goes whack.
    cls

end loop



again, these are just some of the basics for applying physics in coding. It's very sloppy and incomplete because I'm not completely familiar with Turing's math operations.
Kharybdis




PostPosted: Sat Nov 28, 2009 11:54 pm   Post subject: RE:Gravity and jumping

darn it, i have a complete simulation somewhere on my computer that has a very good explanation of how everything works and has an example of it... just need to find it. Sorry, writing this very late at night Sad.

MetaChief wrote it, you should ask him for it. I know it helped me a lot when doing elementary physics coding.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: