Jump to content


Photo

Collision with Zelda-type movement


  • Please log in to reply
2 replies to this topic

#1 Lolligirl

Lolligirl

    HEYYEYAAEYAAAEYAEYAA

  • GMC Member
  • 175 posts
  • Version:GM:Studio

Posted 03 March 2012 - 05:32 PM

Hello! I have a problem with my o_Player object correctly colliding with my o_Block object. Here is my current code: http://pastebin.com/bYVCDnNy

I searched for past topics but I can only find a method using x += xspeed, etc. instead of what would fit with my code, which includes completely free diagonal movement in a 16x16 grid. Could anyone help me with this?

  • 0

#2 Follomania

Follomania

    Unum Mirabile

  • GMC Member
  • 445 posts
  • Version:GM:Studio

Posted 03 March 2012 - 07:01 PM

Since I'm not entirely sure of what your problem is with the colliding, here's what I would suggest for handling the collisions.
Here's your code change a little bit to check for collisions before moving, and a little more compact:
    if (move == 0)
    {
        image_speed = 0;
        image_index = 1;
        spd= 0;
    }
    if (move == 1)
    {
        image_speed = .15;
        spd= 1.5;
    }
    vspeed = 0
    hspeed = 0
    // Key pressed.
    if (keyboard_check(ord('W')))
    {
        if (instance_place(x, y - spd, o_Block) == noone)
        {
            move = 1;
            vspeed -= spd
            sprite_index = WALK_BACK;
        }
    }
     
    if (keyboard_check(ord('A')))
    {
        if (instance_place(x - spd, y, o_Block) == noone)
        {
            move = 1;
            hspeed -= spd
            sprite_index = WALK_LEFT;
        }
    }
       
    if (keyboard_check(ord('S')))
    {
        if (instance_place(x, y + spd, o_Block) == noone)
        {
            move = 1;
            vspeed += spd;
            sprite_index = WALK_FRONT;
        }
    }
       
    if (keyboard_check(ord('D')))
    {
        if (instance_place(x + spd, y, o_Block) == noone)
        {
            move = 1;
            hspeed += spd
            sprite_index = WALK_RIGHT;
        }
    }
     
    // Key released.
    if (keyboard_check_released(ord('W')))
    {
        move = 0;
    }
     
    if (keyboard_check_released(ord('A')))
    {
        move = 0;
    }
       
    if (keyboard_check_released(ord('S')))
    {
        move = 0;
    }
       
    if (keyboard_check_released(ord('D')))
    {
        move = 0;
    }

Then for your end step, put this:
if (instance_place(x,y,o_Block) != noone){
    x = xprevious
    y = yprevious
    move_to_block = 1
    distance = 0
    while (move_to_block == 1){
        x += lengthdir_x(.5,direction)
        y += lengthdir_y(.5,direction)
        distance += .5
        if (instance_place(x,y,o_Block) != noone){
            x -= lengthdir_x(.5,direction)
            y -= lengthdir_y(.5,direction)
            move_to_block = 0
        }
        if (distance > speed){
            move_to_block = 0
        }
    }
    if (instance_place(x + hspeed, y, o_Block) != noone){
        hspeed = 0
    }
    if (instance_place(x,y+vspeed, o_Block) != noone){
        vspeed = 0
    }
}

This second bit of code will check to see if you are colliding. If you are colliding, we jump back to our previous x and y coordinates, and then slowly move in the direction until we are right next to the block. We then set the hspeed or vspeed to 0 if they will cause a collision the next step.

Hope this helps with your problem. :)

EDIT:
Here's a quick example I put together to show you how to use that code. It all works nicely (which I was glad as I wrote it on the spot with no color-coded happiness).

Edited by Follomania, 03 March 2012 - 07:19 PM.

  • 1

#3 Lolligirl

Lolligirl

    HEYYEYAAEYAAAEYAEYAA

  • GMC Member
  • 175 posts
  • Version:GM:Studio

Posted 04 March 2012 - 02:07 AM

Thank you so much, Follomania! It works brilliantly. :)

Edited by Lolligirl, 04 March 2012 - 06:13 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users