Help! Platformer Trouble!

Posted by Infinity_Plus on Nov. 11, 2010, 12:35 p.m.

EDIT: Fixed Jumping Problem, Having Collision Problems! Changed Source Code below!

GMK: Link

I'm at work and decided to create a Platformer in my down time. Well, since I'm not at home, I don't have any of my source code and notes that I would usually have, so I'm going to ask 64D this rather embarrassing question: How do I get my damned character to jump? Here's the two scripts that I'm sure are screwing things up, both run in the Step Event:

scr_plr_movement:

//Horizontal Movement

if keyboard_check(vk_right) = true
{
    if hspeed < max_speed{hspeed += accel_speed;}
    else {hspeed = max_speed;}
}
else if keyboard_check(vk_left) = true
{
    if hspeed > -max_speed{hspeed -= accel_speed;}
    else {hspeed = -max_speed;}
}
else if keyboard_check(vk_left) = false and keyboard_check(vk_right) = false and place_free(x,y+1) = false
{
    if abs(hspeed) < 2 {hspeed = 0}
    else if hspeed < 0 {hspeed += accel_speed*1.25;}
    else if hspeed > 0 {hspeed -= accel_speed*1.25;}
}

//Vertical Movement

if keyboard_check_released(vk_up) = true and place_free(x,y+1) = true
{
    gravity_direction = 270;
    gravity = 1;
}
else if keyboard_check(vk_up) = true and place_free(x,y+1) = false
{
    vspeed -= 12;
}
else if place_free(x,y+1) = true and vspeed <= 0
{
    gravity = .75;
}

and

scr_plr_solid_collision:

if place_free(x+hspeed,y+vspeed) = false
{
    hspeed = 0;
    vspeed = 0;
}

if place_free(x,y+1) = false
{
    gravity = 0;
}

if place_free(x+hspeed,y) = false
{
    if hspeed > 0 {move_contact_solid(0,0);}
    else if hspeed < 0 {move_contact_solid(180,0);}
    hspeed = 0;
}
}

My player moves fine left and right and jumps, but his collision detection is choppy and he has a tendency to get stuck in blocks if he jumps. I'll post the GMK in a moment and provide a link if anyone wants to take a closer look.

Thanks for any help in advance!

Comments

DesertFox 13 years, 5 months ago

Here is a possibility.

I am assuming that you are running scr_plr_movement first, and scr_plr_solid_collision after it.

In scr_plr_movement, you have:

else if keyboard_check(vk_up) = true and place_free(x,y+1) = false
{
    vspeed -= 12;
}

And in scr_plr_solid_collision, you have:

if place_free(x,y+1) = false
{
    gravity = 0;
    vspeed = 0;
    move_contact_solid(270,0);
}

So…

If you run scr_plr_movement, and then scr_plr_solid_collision, what happens?

If the if statement from scr_plr_movement is true, so is the if statement from scr_plr_solid_collision. In essence, every time you jump, you immediately set vspeed to zero afterwards, thus negating your jump.

As I said, this is assuming that scr_plr_movement occurs before scr_plr_solid_collision.

Infinity_Plus 13 years, 5 months ago

Yes, I run scr_plr_movement() before scr_plr_collision(). Those were the two lines I've been looking at, too. I know I have an effective collision system at home, but I was hoping to get this out of the way before I get home.

Reversing the order (calling scr_plr_collision first) allows me to jump, but fails at collision detection. I can force my way into blocks, and if I jump, I get stuck about 12 pixels into the floor before landing.

noshenim 13 years, 5 months ago

I turned scr_plr_solid_collision with this and it worked well:

if place_free(x,y+vspeed) = false
{
    gravity = 0;
    move_contact_solid(-90,vspeed);
    vspeed=0;
}

if place_free(x+hspeed,y) = false
{
    if hspeed > 0 {move_contact_solid(0,hspeed);}
    else if hspeed < 0 {move_contact_solid(180,hspeed);}
    hspeed = 0;
}

Infinity_Plus 13 years, 5 months ago

Very nice, _Player_! That works just as I wanted it to. I'm probably going to replace my scr_solid_collision script with yours, if that's alright. I'll be sure to credit you for that :D

I'll have to tweak it a bit for collisions above the player, but that's minor work