• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED]Collision Errors Persisting

T

Treecase86

Guest
Hey, back for the second thread of the day. I'm making a Sonic engine. So, among other things, the vertical movement won't work. The jumping stops after a few frames, the gravity won't even apply, etc. Please check over my code to see what's wrong. I know that it'll be a long list. Please note I haven't fully implemented many things(i.e slopes, loops, the like).
Code:
///Variables
grounded = false
xpos = 14       //Sonic's origin(x)
ypos = 19       //Sonic's origin(y)
xsp = 0         //Sonic's horizontal speed
ysp = 0         //Sonic's vertical speed
gsp = 0         //Sonic's ground speed (Important with angles)
slope = 0       //the current slope that Sonic's on
angle = 0       //current angle(ground)
//Constants
acc = 0.046875          //acceleration
dec = 0.5               //deceleration
fric = 0.046875         //friction(same as acc)
top = 6                 //top speed
air = 0.09375           //air (x) acceleration
jmp = -6.5              //vertical acceleration
topJmp = 6              //top xsp in air
grav = 0.21875          //gravity
slp = 0.125             //slope?
slprollup = 0.078125    //acc when rolling up slope
slprolldown = 0.3125    //acc going down
fall = 2.5              //???
Code:
///Step event\\\

///Movement
//directions
right = keyboard_check(vk_right) or keyboard_check(ord('D'))
left  = keyboard_check(vk_left ) or keyboard_check(ord('A'))
space  = keyboard_check_pressed(vk_space)

//acceleration
if abs(gsp) < top
{
    if (right)
    {
        gsp += acc
        image_xscale = 1
    }
    else if (left)
    {
        gsp -= acc
        image_xscale = -1
    }
}

//deceleration
if right or left
{
    // if the opposite directional button from where
    // we are moving is pressed, then we decelerate
    if (gsp > 0 and left)
    {
        gsp -= dec
    }   
    if (gsp < 0 and right)
    {
        gsp += dec
    }
}
// decelerate if no buttons are pressed
else
{
    gsp -= fric * sign(gsp)
    if abs(gsp) < fric
    {
        gsp = 0
    }
}

//ground speed
/*xsp = gsp * cos(ang)
ysp = gsp *- sin(ang)
*/

///Collision
//checking for floor

///Jumping
var grounded;
grounded = tile_layer_find(1,x,y)

if grounded == false
{
    //if off of ground, apply gravity
    ysp += grav
}
else
{
    //otherwise, ysp = 0
    ysp = 0
}

if (space and grounded)
{
    //if jumping, you've jumped
    grounded = false
    ysp += jmp
}

//application
xsp = gsp
xpos += xsp
ypos += ysp
x += xsp
y += ysp
///Animations
if gsp == 0
{
    //if no gsp, sprite = standing
    sprite_index = sprSonic
}

//Running
else if gsp < top
{
    sprite_index = sprSonWalk
    image_speed = gsp
}

if gsp >= top
{
    sprite_index = sprSonRun
    image_speed = gsp
}

if ysp != 0
{
    sprite_index = sprSonJump
}
 

TheouAegis

Member
Well that last block "if ysp != 0" won't work once you implement the commented-out sloped speed line. I had that issue trying to work this kind of code out too.

You said it works for a frame or two, so my only idea is that maybe what you think is empty space might actually have an "empty" tile. But with tile_layer_find, even an "empty" tile is still a tile.

Also, you declare grounded as a local variable, so the part where you set grounded=false is junk code because grounded is technically false every step unless a tile is found at (x,y).

Also, since all you do is set vsp to 0 when grounded is true, you're not actually snapping out of the collision that it finds. If there's a tile at (x,y) and you set vsp to 0, then next step and the step after there will continue to be a tile at (x,y) because Sonic is in the ground now. It's possible Sonic might actually be 2, 3, or even 5.9999999999 pixels in the ground by the time he stops (once it works).
 
Top