Computer Science Canada

Platform Help

Author:  B-Man 31 [ Mon Dec 14, 2009 10:24 pm ]
Post subject:  Platform Help

What is it you are trying to achieve?
Trying to get the ball to land on the platform


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


%MAKE A JUMPING GAME

class player
    import Input
    export all

    const gravity := 2
    const radius := 5
    const movespeed := 5
    const jumpspeed := 20

    type aPlayer :
        record
            x, y, xsp, ysp : real
        end record

    var user : aPlayer
    user.xsp := 0
    user.ysp := 0
    user.x := maxx div 2
    user.y := 0
    var chars : array char of boolean

    proc move
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) and user.y = radius then %JUMP
            user.ysp := jumpspeed
        end if
        if chars (KEY_LEFT_ARROW) then %MOVER LEFT
            user.xsp := -movespeed
        elsif chars (KEY_RIGHT_ARROW) then %MOVE RIGHT
            user.xsp := movespeed
        else
            user.xsp := 0
        end if

        %MOVE THE PLAYER
        user.ysp -= gravity
        user.x += user.xsp
        user.y += user.ysp

        %SET BOUNDERIES
        %RIGHT AND LEFT
        if user.x + radius > maxx then
            user.x := maxx - radius
        elsif user.x - radius < 0 then
            user.x := radius
        end if
        %UP AND DOWN
        if user.y + radius > maxy then
            user.y := maxy - radius
        elsif user.y - radius < 0 then
            user.y := radius
        end if
    end move

    proc draw
        drawoval (round (user.x), round (user.y), radius, radius, black)
    end draw

end player

class platform   
    inherit player
    import Math
    export all

    type aPlatform :
        record
            x1, x2, y, clr : int
        end record

    var pform : aPlatform

    proc drawplatform (X1, X2, Y, CLR : int)
        pform.x1 := X1 % RIGHT SIDE
        pform.x2 := X2 % LEFT SIDE
        pform.y := Y % HEIGHT
        pform.clr := CLR % COLOUR
        drawline (pform.x1, pform.y, pform.x2, pform.y, pform.clr)
    end drawplatform

%%%%%%%%%THIS PART HERE %%%%%%%%%%%%%%%%
    proc detect
        if Math.DistancePointLine (user.x, (user.y - radius), pform.x1, pform.y, pform.x2, pform.y) < 1 then % COLLISION DETECTION
            user.ysp := 0
            user.y := pform.y + radius             
         end if
    end detect

end platform


setscreen ("offscreenonly")

var aplayer : ^player
new player, aplayer

var aplatform : flexible array 1 .. 1 of ^platform
new platform, aplatform (upper (aplatform))


loop
    player (aplayer).move
    player (aplayer).draw
    platform (aplatform (upper (aplatform))).drawplatform (50, 100, 50, 255)
    platform (aplatform (upper (aplatform))).detect
    View.Update
    cls
    Time.DelaySinceLast (10)
end loop



Author:  mirhagk [ Mon Dec 14, 2009 11:40 pm ]
Post subject:  RE:Platform Help

look into linear collision detection

here

calculating distance does not work very well in instances like this one.

Author:  B-Man 31 [ Tue Dec 15, 2009 7:29 am ]
Post subject:  RE:Platform Help

Thats all fine and dandy, but i would like to know WHY it isnt working because ive used this before in a brick game and it worked perfectly.

Author:  B-Man 31 [ Tue Dec 15, 2009 10:12 am ]
Post subject:  Re: Platform Help

I Ended up solving it, here is the code for anyone who9 wishes to see it:

Turing:

%MAKE A JUMPING GAME

class platform
    export all

    type aPlatform :
        record
            x1, x2, y, clr : int
        end record

    var pform : aPlatform


    proc drawplatform (X1, X2, Y, CLR : int)
        pform.x1 := X1 % RIGHT SIDE
        pform.x2 := X2 % LEFT SIDE
        pform.y := Y % HEIGHT
        pform.clr := CLR % COLOUR
        drawline (pform.x1, pform.y, pform.x2, pform.y, pform.clr)
    end drawplatform

end platform

var aplatform : flexible array 1 .. 1 of ^platform
new platform, aplatform (upper (aplatform))

class player
    import Input, aplatform, platform, Math
    export all

    const gravity := 2
    const radius := 5
    const movespeed := 5
    const jumpspeed := 20

    type aPlayer :
        record
            x, y, xsp, ysp : real
        end record

    var user : aPlayer
    user.xsp := 0
    user.ysp := 0
    user.x := maxx div 2
    user.y := 0
    var chars : array char of boolean
    var t1 := 0

    proc move
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) and user.ysp < 1 and Time.Elapsed - t1 > 200 then  %JUMP
            t1 := Time.Elapsed
            user.ysp := jumpspeed
        end if
        if chars (KEY_LEFT_ARROW) then %MOVER LEFT
            user.xsp := -movespeed
        elsif chars (KEY_RIGHT_ARROW) then %MOVE RIGHT
            user.xsp := movespeed
        else
            user.xsp := 0
        end if

        %MOVE THE PLAYER
        user.ysp -= gravity
        user.x += user.xsp
        user.y += user.ysp

        %SET BOUNDERIES
        %RIGHT AND LEFT
        if user.x + radius > maxx then
            user.x := maxx - radius
        elsif user.x - radius < 0 then
            user.x := radius
        end if
        %UP AND DOWN
        if user.y + radius > maxy then
            user.y := maxy - radius
        elsif user.y - radius < 0 then
            user.y := radius
        end if
    end move

    proc draw
        drawoval (round (user.x), round (user.y), radius, radius, black)
    end draw

    proc detect
        for i : 1 .. (upper (aplatform)) %FOR EVERY PLATFROM
            if Math.DistancePointLine (user.x, (user.y - radius), platform (aplatform (i)).pform.x1, platform (aplatform (i)).pform.y, platform (aplatform (i)).pform.x2,
                    platform (aplatform (i)).pform.y) < 2.5 and user.ysp < 0 then
                user.y := platform (aplatform (i)).pform.y + radius
                user.ysp := 0
            end if
        end for
    end detect

end player

setscreen ("offscreenonly")

var aplayer : ^player
new player, aplayer

loop
    aplayer -> move
    aplayer -> draw
    aplatform (upper (aplatform)) -> drawplatform (50, 100, 50, 255)
    aplayer -> detect
    View.Update
    cls
    Time.DelaySinceLast (10)
end loop

Author:  DemonWasp [ Tue Dec 15, 2009 10:12 am ]
Post subject:  RE:Platform Help

Put this right above your collision detection line:
Turing:

        Text.Locate ( 1, 1 )
        put "(",user.x,",",user.y,")"


Move around. Oops. Those values didn't change, did they? Well, why not?!

Because your object design is wrong.

You have class platform inherit player. This establishes the following relationship: "All platforms are players". I sincerely hope that this is not what you intended.

The practical upshot is that in platform.detect(), platform is referencing the values of its own user variable, rather than those of the player variable you created elsewhere in your code. You will need to pass the platform the coordinates of the object it is (potentially) colliding with.

Author:  mirhagk [ Tue Dec 15, 2009 12:08 pm ]
Post subject:  RE:Platform Help

the collision detection is looking pretty good actually. It's been a while since I've used non-linear collision detection actually. Once you learn it you can't stop coding with it. (lol kinda like turing)

Author:  B-Man 31 [ Tue Dec 15, 2009 5:23 pm ]
Post subject:  RE:Platform Help

i know, i ended up solving it with help from a guy in my class who is much stronger in OOP than i am.

Author:  mirhagk [ Wed Dec 16, 2009 8:30 am ]
Post subject:  RE:Platform Help

I find OOP kinda strange. I mean obviously stuff like weapons and vehicles that could have sub-classes of it should be OOP but I find too many people just drool over OOP just because it's complicated.

Author:  DemonWasp [ Wed Dec 16, 2009 10:39 am ]
Post subject:  RE:Platform Help

At the high school level it's probably true that the complication of OOP is what drives a lot of people. Later on, however, OOP is actually a pretty decent framework for solving complicated problems. The difference is in scale: one would hope that most high school projects never exceed around 4000-5000 lines of code, while in the real world of programming, projects often exceed 1000 times those numbers. At that size, proper encapsulation and separation of responsibilities becomes critical.

Author:  mirhagk [ Wed Dec 16, 2009 11:40 am ]
Post subject:  RE:Platform Help

Yeah but in turing.... lol I just get pained when people make a module for someone to use, except they make it OOP, which just makes it harder for someone to use in their program.

Author:  B-Man 31 [ Thu Dec 17, 2009 9:21 am ]
Post subject:  RE:Platform Help

ok, im just using OOP to get practise with it, as ill be moving on to C++ next year for my programming class at school. ill be starting to learn over the summer to have a little bit of a head start, but because i feel i am stronger in turing, im am trying to get the basics down of OOP.


: