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

Username:   Password: 
 RegisterRegister   
 acceleration
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BenLi




PostPosted: Tue Sep 26, 2006 4:32 pm   Post subject: acceleration

I thought this simple piece of code may help anyone starting to do some games or graphics in turing. This program accelerates a circle around the screen. Controls are arrow button and the spacebar.

Features:
-bounce off the walls
-spacebar to stop all velocity
-friction

Note: no attempt at actual physics has been made

code:

setscreen ("graphics:800;600,offscreenonly")

var x, y : int
var vx, vy : real := 0
var key : array char of boolean
x := 400
y := 300

loop
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then
        vy += .3
    elsif key (KEY_DOWN_ARROW) then
        vy -= .3
    end if
    if key (KEY_RIGHT_ARROW) then
        vx += .3
    elsif key (KEY_LEFT_ARROW) then
        vx -= .3
    end if
    if key (' ') then
        vx := 0
        vy := 0
    end if
    x += round (vx)
    y += round (vy)

    if x > maxx then
        vx := -vx
    elsif x < 0 then
        vx := -vx
    end if
    if y > maxy then
        vy := -vy
    elsif y < 0 then
        vy := -vy
    end if
    if abs (vx) = vx then
        vx -= .1
    elsif (vx) not= vx then
        vx += .1
    end if
    if abs (vy) = vy then
        vy -= .1
    elsif (vy) not= vy then
        vy += .1
    end if
    drawfilloval (x, y, 25, 25, blue)
    View.Update
    cls
end loop
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Fri Sep 29, 2006 3:53 am   Post subject: (No subject)

ok nothing works for me, i press a direction key the balls gets stuck on the left side.

Super glitch each time i run this.
BenLi




PostPosted: Fri Sep 29, 2006 7:48 am   Post subject: (No subject)

can someone check this out? It runs perfectly for me.

Are you on an uber fast computer? Perhaps decrease the acceleration values?
ZeroPaladn




PostPosted: Fri Sep 29, 2006 11:49 am   Post subject: (No subject)

the ball glitches out every time. gets stuck in the walls and whatnot. other than that, i dont understand the code, which in turn means i cant help you fix this.
War_Caymore




PostPosted: Fri Sep 29, 2006 1:31 pm   Post subject: (No subject)

pretty cool little game, hold the down htton after letting it jump around and it will glitch into the end of the screen. mabey thickening the colsion at the ends of the screen might prevent the ball from sticking but god only knows what more harm that will do.

A better idea.

code:
if x > maxx then
        vx := -vx
    elsif x < 0 then
        vx := -vx
    end if
    if y > maxy then
        vy := -vy
    elsif y < 0 then
        vy := -vy
    end if


instead of setting the boundries to maxx and maxy, set it to th radius of the ball

code:
if x > maxx - 25 then
        vx := -vx
    elsif x < 0 + 25 then
        vx := -vx
    end if
    if y > maxy - 25 then
        vy := -vy
    elsif y < 0 + 25 then
        vy := -vy
    end if

       


that might keep the ball inside the screen and it will not go half out, and glithc.

Nevermind.... this only lengthens the time it takes to glith, so it is now mroe preventable. i did what i could with what little skills i have.
Cervantes




PostPosted: Fri Sep 29, 2006 5:16 pm   Post subject: (No subject)

Cervantes makes his first large post in too long!

War_Caymore wrote:
instead of setting the boundries to maxx and maxy, set it to th radius of the ball

code:
if x > maxx - 25 then
        vx := -vx
    elsif x < 0 + 25 then
        vx := -vx
    end if
    if y > maxy - 25 then
        vy := -vy
    elsif y < 0 + 25 then
        vy := -vy
    end if


that might keep the ball inside the screen and it will not go half out, and glithc.

Nevermind.... this only lengthens the time it takes to glith, so it is now mroe preventable. i did what i could with what little skills i have.

That does nothing but change the place at which the ball gets stuck. If you want to prevent the ball from sticking, we first need to understand why it sticks.

It sticks because somtimes when you bounce against the wall, the numbers work out in the following way: frame 1, you're just outside the wall; frame 2, you're as far inside the wall as can be possible; frame 3, you're still inside the wall because you didn't bounce back far enough in one frame. Why didn't you bounce back far enough? Because a) the retarding code slowed you down (that code that moves the velocity of the ball closer to zero) and b) you are probably still holding the arrow key to move you towards the edge.

What's the solution? The easiest one would be to just step through an extra frame of movement when you hit the wall, by adding a x += vx and y += vy in the respective if statements. Or maybe you should add two steps? Who knows. It would all depend on many constants such as acceleration rate from the arrow keys and deceleration rate, as well as max speed.

The other way to solve the problem would be to do some math to predict when a collision with the wall will occur, before it actually occurs. You can then do some more math to determine where exactly you should be after the collision. This approach won't be limited by how many frames per second you are getting. If you like math, I suggest you try this out.

Now, to clean the code in a few places.

code:
var x, y : int
var vx, vy : real := 0

Make x and y reals, and round when drawing. Rounding during your calculations allows for rounding errors to grow from a microscopic scale to potentially a macroscopic scale.

code:
    if x > maxx then
        vx := -vx
    elsif x < 0 then
        vx := -vx
    end if

First, let's insert the extra step:
code:

    if x > maxx then
        vx := -vx
        x += vx
    elsif x < 0 then
        vx := -vx
        x += vx
    end if

Now, we're doing the same action if either condition is true. Let's just use or:
code:

    if x > maxx or x < 0 then
        vx := -vx
        x += vx
    end if


Next, the retarding of motion could be done differently. Instead of
code:

    if abs (vx) = vx then
        vx -= .1
    elsif (vx) not= vx then
        vx += .1
    end if

we could use
code:

vx *= 0.99

This means that the ball no longer slows down linearly. It mimicks things like wind resistance, which increases with the square of the velocity, much better.

Finally, stick a delay in there. It's very much needed for anyone with a decently fast computer.

Here's the complete code with all the changes I suggested:
code:

setscreen ("graphics:800;600,offscreenonly")

var x, y : real
var vx, vy : real := 0
var key : array char of boolean
x := 400
y := 300

loop
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then
        vy += .3
    elsif key (KEY_DOWN_ARROW) then
        vy -= .3
    end if
    if key (KEY_RIGHT_ARROW) then
        vx += .3
    elsif key (KEY_LEFT_ARROW) then
        vx -= .3
    end if
    if key (' ') then
        vx := 0
        vy := 0
    end if
    x += vx
    y += vy

    if x > maxx or x < 0 then
        vx := -vx
        x += vx
    end if
    if y > maxy or y < 0 then
        vy := -vy
        y += vy
    end if
    vx *= 0.99
    vy *= 0.99
    drawfilloval (round (x), round (y), 25, 25, blue)
    View.Update
    delay (10)
    cls
end loop
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: