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

Username:   Password: 
 RegisterRegister   
 Collisions with a platform
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Adaub7282




PostPosted: Sun Feb 15, 2009 1:42 pm   Post subject: Collisions with a platform

Turing:

%Constants
const gravity :int := 2
%Variables
var x := 20
var y := 20
var y_velocity := 0
var keys : array char of boolean
var platx1, platx2, platy1, platy2 : int
platx1 := 100
platy1 := 20
platx2 := 300
platy2 := 30
%Graphics
View.Set("offscreenonly")
%Loop
loop
    Input.KeyDown  (keys)
    if keys (KEY_UP_ARROW) and y <=20 then %jump
        y_velocity := 30
    end if
    if keys (KEY_LEFT_ARROW) and x > 0 then %move left
        x -= 5
    end if
    if keys (KEY_RIGHT_ARROW) and x < maxx then%move right
        x += 5
    end if
   
    if y = platy2 and x >= platx1 and x <= platx2 then %attempt at making the red platform solid
        y := 40
        y_velocity := 0
    end if
       
    y_velocity -= gravity %making gravity relevant
    y += y_velocity %making y_velocity relevant

    if y <= 20 then %making the ground solid
        y := 20
        y_velocity := 0
    end if


   
    Draw.FillOval (x, y, 10, 10, blue) %player
    Draw.FillBox (0, 0, maxx, 10, green) %ground
    Draw.FillBox (platx1, platy1, platx2, platy2, red) %platform
    View.Update
    delay (20)
    cls
end loop


Alright, the above code is an attempt at a simple program. All it is supposed to do is have a moveable ball, that can jump and land on a platform. The platform, however, is still not "solid" and the ball passes through. I have read through my code several times and can't find out what I have done wrong. Help would be much appreciated. On a side note, how do I make the screen move with the character? Example: Mario for the NES.
Sponsor
Sponsor
Sponsor
sponsor
Scott




PostPosted: Sun Feb 15, 2009 9:14 pm   Post subject: Re: Collisions with a platform

Your math is making the ball jump through the platform. See your ball is moving 2 at a time, that way it passes right through the 1 pixel you are checking for..
Instead of
code:
y = platy2

try
code:
y <= platy2 and y >= platy1

That way it will stop if it passes through your 10 pixel thick platform, not just the 1 pixel thick surface. With this you are stopping and the ball will be half in the platform, try and make it so the ball stops when its edges hit to make up for the help we gave you. Good luck Smile

edit : better explanation.
Adaub7282




PostPosted: Sun Feb 15, 2009 9:56 pm   Post subject: RE:Collisions with a platform

Thank you, I wasn't sure if it would still pass throught the correct pixel, it would just spend 1/2 the time at that pixel then if gravity were set to 1.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: