Computer Science Canada

Platform Help Needed

Author:  pttptppt [ Mon Mar 06, 2017 10:20 pm ]
Post subject:  Platform Help Needed

What is it you are trying to achieve?
I am making a platform game (think Mario). I want the player to be able to jump from platform to platform.


What is the problem you are having?
When the player is underneath a platform, he automatically jumps on top of the platform even if it's too high. I dont want that.


Describe what you have tried to solve this problem
I've tried making it so if cy > InvisiPlatform2... That way only when he can actually jump onto that platform, he does. Problem is that didn't work


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

Turing:


% some constants, tweak this to your liking
const GROUND_HEIGHT := 5
const RUN_SPEED := 10
const JUMP_SPEED := 30
const GRAVITY := 2

%Platform
const Platformx1 := 100
const Platformy1 := 100
const Platformx2 := 200
const Platformy2 := 100
const InvisiPlatformx1 := 100
const InvisiPlatformy1 := 107
const InvisiPlatformx2 := 200
const InvisiPlatformy2 := 107

%Platform2
const Platform2x1 := 300
const Platform2y1 := 250
const Platform2x2 := 400
const Platform2y2 := 250
const InvisiPlatform2x1 := 300
const InvisiPlatform2y1 := 257
const InvisiPlatform2x2 := 400
const InvisiPlatform2y2 := 257

%end of my revisions
var win := Window.Open ("graphics:640;480,offscreenonly")
var chars : array char of boolean

% player position and velocity
var cx, cy : int
var velx, vely : real

cx := 20
velx := 0
cy := 400
vely := 0
const radius := 5

loop
    Input.KeyDown (chars)

    % to make the player move
    if chars (KEY_LEFT_ARROW) and cx >= 11 then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) and cx <= maxx - 11 then
        velx := RUN_SPEED
    else
        velx := 0
    end if

    % if cx >= maxx - 5 then
    %        cx := maxx - 5
    %    elsif cx <= 5 then
    %        cx := 5
    %    end if

    % remember, in order to jump you MUST be on the ground when pressing UP
    if chars (KEY_UP_ARROW) and cy = GROUND_HEIGHT then
        vely := JUMP_SPEED
    end if
    %again follow the above if statement if the movable box is on the ground then so make it the platform to allow jumping from platform
    if chars (KEY_UP_ARROW) and cy = InvisiPlatformy2 and cx > InvisiPlatformx1 and cx < InvisiPlatformx2 then
        vely := JUMP_SPEED
    end if
    %end of my revisions

    if chars (KEY_UP_ARROW) and cy = InvisiPlatform2y2 and cx > InvisiPlatform2x1 and cx < InvisiPlatform2x2 then
        vely := JUMP_SPEED
    end if
    % subtract your "gravity" constant from the velocity EACH frame
    vely -= GRAVITY
    cx += round (velx)
    cy += round (vely)

    % simple "collision" check. just makes sure the player stays above ground
    if cy < GROUND_HEIGHT then
        cy := GROUND_HEIGHT
        vely := 0
    end if

    %so if movable box isn't on the ground and he is in between box then set his y position to the height of the platform
    if cy not= GROUND_HEIGHT and cy < InvisiPlatformy2 and cx + radius > InvisiPlatformx1 and cx < InvisiPlatformx2 then
        cy := InvisiPlatformy2
        vely := 0
    end if

    if cy not= GROUND_HEIGHT and cy < InvisiPlatform2y2 and cx + radius > InvisiPlatform2x1 and cx < InvisiPlatform2x2 then
        cy := InvisiPlatform2y2
        vely := 0
    end if


    Draw.ThickLine (Platformx1, Platformy1, Platformx2, Platformy2, 5, black)
    Draw.ThickLine (Platform2x1, Platform2y1, Platform2x2, Platform2y2, 5, black)
    Draw.FillOval (cx, cy, radius, radius, red)

    View.Update
    delay (15)
    Draw.Cls
end loop




Please specify what version of Turing you are using
Latest

Author:  pttptppt [ Tue Mar 07, 2017 7:13 pm ]
Post subject:  RE:Platform Help Needed

Anyone??? PLease

Author:  Insectoid [ Thu Mar 09, 2017 12:36 pm ]
Post subject:  RE:Platform Help Needed

You're on the right track. What is the condition for jumping on a platform? Can you jump 'through' platforms from underneath like mario? Or do you bounce off?

It helps to say exactly what you want to happen in plain old english before turning it into code. Think about all the different cases that might come up and try to fit them into a generalized solution. There's lots of different ways so I'll just give you a few hints and let you figure it out from there.

A platform is just usually just a rectangle. Rectangular collision is pretty simple, right? But really, the platform is only the top line of the rectangle- you can't land on the bottom line, that just wouldn't make sense! If you hit the bottom line or the sides, you don't land, you just bounce off!

So how do you know if you hit the top line? There's a few ways. You can use ordinary line collision, which is identical to rectangular collision where y1 = y2. You could check the player's velocity- you can't hit the top line if you're going up, and you can't hit the bottom line if you're going down. Probably a combination of the two is best.

You may run into a problem where characters sometimes go right through the platform without even noticing- this may be due to high speed. If the platform is 10 pixels thick, and the player moves 20 pixels per frame, the collision may happen between frames and therefore not register. There's a number of ways to solve this too, but I wouldn't bother unless it actually affects your game.

Author:  pttptppt [ Thu Mar 09, 2017 3:32 pm ]
Post subject:  Re: RE:Platform Help Needed

Insectoid @ Thu Mar 09, 2017 12:36 pm wrote:
You're on the right track. What is the condition for jumping on a platform? Can you jump 'through' platforms from underneath like mario? Or do you bounce off?

It helps to say exactly what you want to happen in plain old english before turning it into code. Think about all the different cases that might come up and try to fit them into a generalized solution. There's lots of different ways so I'll just give you a few hints and let you figure it out from there.

A platform is just usually just a rectangle. Rectangular collision is pretty simple, right? But really, the platform is only the top line of the rectangle- you can't land on the bottom line, that just wouldn't make sense! If you hit the bottom line or the sides, you don't land, you just bounce off!

So how do you know if you hit the top line? There's a few ways. You can use ordinary line collision, which is identical to rectangular collision where y1 = y2. You could check the player's velocity- you can't hit the top line if you're going up, and you can't hit the bottom line if you're going down. Probably a combination of the two is best.

You may run into a problem where characters sometimes go right through the platform without even noticing- this may be due to high speed. If the platform is 10 pixels thick, and the player moves 20 pixels per frame, the collision may happen between frames and therefore not register. There's a number of ways to solve this too, but I wouldn't bother unless it actually affects your game.


I get what you're saying but it just isn't working. Ive tried putting different conditions but the end result is always simply i can't jump when im on a platform and if im under a platform, independant of the height of the platform, it'll jump to directly to the platform.
It'd be helpful if you could look at my code and provide examples of what youre saying. For example, youre telling me to use line collision but i already am..

Please help soon because exam period is coming so i wont be able to code during that time

Author:  Insectoid [ Fri Mar 10, 2017 10:40 am ]
Post subject:  RE:Platform Help Needed

Let's translate this code to english to see what it's actually doing:
code:
if cy not= GROUND_HEIGHT and cy < InvisiPlatformy2 and cx + radius > InvisiPlatformx1 and cx < InvisiPlatformx2 then


This says "If we are in the air, lower than the platform, and between the sides of the platform then put us on top of the platform"

That is a literal translation of that line of code to english. That is what this code does. Is that what you want the code to do? Can you think of a situation that satisfies this condition where we do not want to be moved to the top of the platform?

Can you re-word that english statement to more accurately reflect the behavior you want?

Here's another small hint- What is the ground? For you, it's just a number GROUND_HEIGHT, and needs all its own special code to handle. What if you made the ground just a platform, like everything else? That way, you have to write exactly zero code to handle the ground. You never have to check it. Just make a platform at the bottom of the screen and your existing platform code will automatically handle it.

Author:  pttptppt [ Fri Mar 10, 2017 10:56 am ]
Post subject:  Re: RE:Platform Help Needed

Insectoid @ Fri Mar 10, 2017 10:40 am wrote:
Let's translate this code to english to see what it's actually doing:
code:
if cy not= GROUND_HEIGHT and cy < InvisiPlatformy2 and cx + radius > InvisiPlatformx1 and cx < InvisiPlatformx2 then


This says "If we are in the air, lower than the platform, and between the sides of the platform then put us on top of the platform"

That is a literal translation of that line of code to english. That is what this code does. Is that what you want the code to do? Can you think of a situation that satisfies this condition where we do not want to be moved to the top of the platform?

Can you re-word that english statement to more accurately reflect the behavior you want?

Here's another small hint- What is the ground? For you, it's just a number GROUND_HEIGHT, and needs all its own special code to handle. What if you made the ground just a platform, like everything else? That way, you have to write exactly zero code to handle the ground. You never have to check it. Just make a platform at the bottom of the screen and your existing platform code will automatically handle it.


Thanks for the reply. Like i said in my original post when i said what i tried to change, i changed that line to be cy > InvisiPlatformy2. However, when I tried that, the program ended up being even more broken. Can you try running that to see what i mean?

Author:  pttptppt [ Fri Mar 10, 2017 4:02 pm ]
Post subject:  RE:Platform Help Needed

Here I've changed the code a bit. Now the problems are:
1. i can only stay on the platform if i have jumped from anywhere but under the platform. In other words, i can only stay on platform if my starting x is not= the the x of the platform

2. If i do manage to jump on the platform, if i jump again ill sink through the platform. This is probably for the same reason that problem #1 is happening

My code:
Turing:


% some constants, tweak this to your liking
const GROUND_HEIGHT := 5
const RUN_SPEED := 10
const JUMP_SPEED := 30
const GRAVITY := 2

%Platform
const Platformx1 := 100
const Platformy1 := 100
const Platformx2 := 200
const Platformy2 := 100
const InvisiPlatformx1 := 100
const InvisiPlatformy1 := 107
const InvisiPlatformx2 := 200
const InvisiPlatformy2 := 107

%Platform2
const Platform2x1 := 300
const Platform2y1 := 250
const Platform2x2 := 400
const Platform2y2 := 250
const InvisiPlatform2x1 := 300
const InvisiPlatform2y1 := 257
const InvisiPlatform2x2 := 400
const InvisiPlatform2y2 := 257


var onplatform1 : boolean := false

%end of my revisions
var win := Window.Open ("graphics:640;480,offscreenonly")
var chars : array char of boolean

% player position and velocity
var cx, cy : int
var velx, vely : real

cx := 20
velx := 0
cy := 400
vely := 0
const radius := 5

loop
    Input.KeyDown (chars)

    % to make the player move
    if chars (KEY_LEFT_ARROW) and cx >= 11 then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) and cx <= maxx - 11 then
        velx := RUN_SPEED
    else
        velx := 0
    end if

    % if cx >= maxx - 5 then
    %        cx := maxx - 5
    %    elsif cx <= 5 then
    %        cx := 5
    %    end if

    % remember, in order to jump you MUST be on the ground when pressing UP
    if chars (KEY_UP_ARROW) and cy = GROUND_HEIGHT then
        vely := JUMP_SPEED
    end if
    %again follow the above if statement if the movable box is on the ground then so make it the platform to allow jumping from platform
    if chars (KEY_UP_ARROW) and cy = InvisiPlatformy2 and cx > InvisiPlatformx1 and cx < InvisiPlatformx2 then
        vely := JUMP_SPEED
    end if
    %end of my revisions

    if chars (KEY_UP_ARROW) and cy = InvisiPlatform2y2 and cx > InvisiPlatform2x1 and cx < InvisiPlatform2x2 then
        vely := JUMP_SPEED
    end if
    % subtract your "gravity" constant from the velocity EACH frame
    vely -= GRAVITY
    cx += round (velx)
    cy += round (vely)

    % simple "collision" check. just makes sure the player stays above ground
    if cy < GROUND_HEIGHT then
        cy := GROUND_HEIGHT
        vely := 0
    end if

    %so if movable box isn't on the ground and he is in between box then set his y position to the height of the platform
    % if cy not= GROUND_HEIGHT and cy < InvisiPlatformy2 and cx + radius > InvisiPlatformx1 and cx < InvisiPlatformx2 then
    %     cy := InvisiPlatformy2
    %     vely := 0
    % end if
    %
    % if cy not= GROUND_HEIGHT and cy < InvisiPlatform2y2 and cx + radius > InvisiPlatform2x1 and cx < InvisiPlatform2x2 then
    %     cy := InvisiPlatform2y2
    %     vely := 0
    % end if

    % if cx > Platformx2 and cx < Platformx1 and cy < Platformy1 then
    if onplatform1 = true then
        if chars (KEY_UP_ARROW) and cx <= Platformx2 and cx >= Platformx1 and cy > Platformy1 - 6 and cy < Platformy1 + 6 then
            vely := JUMP_SPEED
            cy := InvisiPlatformy1
            %elsif cx < Platformx1 or cx > Platformx2 then
        % else
        %     onplatform1 := false
        end if
end if
    if cx <= Platformx2 and cx >= Platformx1 and cy > Platformy1 - 6 and cy < Platformy1 + 6 then
        vely := 0
        cy := InvisiPlatformy1
        onplatform1 := true
    end if
    %  end if


    Draw.ThickLine (Platformx1, Platformy1, Platformx2, Platformy2, 5, black)
    Draw.ThickLine (Platform2x1, Platform2y1, Platform2x2, Platform2y2, 5, black)
    Draw.FillOval (cx, cy, radius, radius, red)

    View.Update
    delay (15)
    Draw.Cls
end loop




: