Jump to content


Photo

Need help with collisions


  • Please log in to reply
2 replies to this topic

#1 legobear154

legobear154

    GMC Member

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

Posted 06 June 2012 - 04:59 PM

I am trying to create tile collisions for my top down shooter game. I am using a modified version of Rixeno's move_step() script found here http://gmc.yoyogames...howtopic=367659.
I have modified it slightly to work with tiles, but I have encountered a problem.
First, here is my code:

var i,move_check;
for (i = abs(xspeed); i > 0; i -= 1){
    if (i < 0) {
        i = 0;
    }
    move_check = sign(xspeed)*i;
    if !(check_tile_collision(x+move_check,y,direction)){
        x += move_check;
        break;
    }
    if !(check_tile_collision(x+move_check,y-i,direction)){
        y -= i/2;
    }
    if !(check_tile_collision(x+move_check,y+i,direction)){
        y += i/2;
    }  
}
for (i = abs(yspeed); i > 0; i -= 1){
    if (i < 0) {
        i = 0;
    }
    move_check = sign(yspeed)*i;
    if !(check_tile_collision(x,y+move_check,direction)){
        y += move_check;
        break;
    }
    if !(check_tile_collision(x-i,y+move_check,direction)){
        x -= i/2;
    }
    if !(check_tile_collision(x+i,y+move_check,direction)){
        x += i/2;
    }
}
xspeed is the speed your player travels in the x direction. yspeed is the speed your player travels in the y direction.

check_tile_collision:
tile_x = x + lengthdir_x(argument0,argument2);
tile_y = y + lengthdir_y(argument1,argument2);
return(tile_layer_find(1000000,tile_x,tile_y));

The problem is that the tile_x and tile_y values go to above 100 when I check for collisions. I am not sure why that is though.

Thanks,
Legobear154

Edited by legobear154, 06 June 2012 - 05:09 PM.

  • 0

#2 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 07 June 2012 - 01:02 AM

1. the script is adding x and y on its own, so you're supposed to put the relative coordinates sans x and y. E.g.
check_tile_collision(move_check, 0, direction)
in place of check_tile_collision(x+move_check, y, direction).

2. the script itself doesn't look right. Usually, you must put the same pair of arguments for both of lengthdir_x and lengthdir_y. I guess it is trying to rotate the point around the player, right? Then it should be calculated as described in this tutorial.

However, your actual movement doesn't seem to take account of direction at all. Probably, those trigonometry is totally unnecessary and you could simply do:
return tile_layer_find(1000000, x + argument0, y + argument1);
And call it without putting direction.
if !(check_tile_collision(move_check, 0)) ...

  • 0

#3 legobear154

legobear154

    GMC Member

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

Posted 07 June 2012 - 01:59 AM

1. the script is adding x and y on its own, so you're supposed to put the relative coordinates sans x and y. E.g.

check_tile_collision(move_check, 0, direction)
in place of check_tile_collision(x+move_check, y, direction).

2. the script itself doesn't look right. Usually, you must put the same pair of arguments for both of lengthdir_x and lengthdir_y. I guess it is trying to rotate the point around the player, right? Then it should be calculated as described in this tutorial.

However, your actual movement doesn't seem to take account of direction at all. Probably, those trigonometry is totally unnecessary and you could simply do:
return tile_layer_find(1000000, x + argument0, y + argument1);
And call it without putting direction.
if !(check_tile_collision(move_check, 0)) ...


Thank you! I forgot to post this code though that tells where direction is coming from:

image_angle = point_direction(x,y,mouse_x,mouse_y);
if keyboard_check(vk_shift){
    sp = runspeed;
}
else if !keyboard_check(vk_shift){
    sp = movespeed;
}

if (keyboard_check(vk_up)){
    yspeed = -sp;
    yoffset = -1;
}
if (keyboard_check(vk_down)){
    yspeed = sp;
    yoffset = 1;
}
if (keyboard_check(vk_left)){
    xspeed = -sp;
    xoffset = -1;
}
if (keyboard_check(vk_right)){
    xspeed = sp;
    xoffset = 1;
}

direction = point_direction(x,y,x+xoffset,y+yoffset);
xoffset = 0;
yoffset = 0;

I should also say that the player rotates separate of the direction the player is looking, meaning the player could be looking left of itself (mouse is to the left of the sprite) but the player can move in any direction. I tend to over complicate things at times probably, so any optimizations are welcome as well Posted Image The players point of origin is in the center of it, and its radius is 16px and its a circle currently, but I would like it to collide in a square so when I change the sprites to make it look more like a player I wont have to mess with the things too much.

I hope thought things cleared up what I am trying to achieve. I will try your suggestions immediately.

Thanks,
Legobear154
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users