# GMS 2Problem with Tile collision when using speed + direction for movement.

#### Chris Livermore

##### Member

hi Gamemakers,

Im having some real trouble with tile collision detection, I am using the Shaun Spalding Tile Collision tutorial and trying to addapt it for my own game (which is a little different from the game Shaun was working on).

Basically my players Direction is determined by the mouse's X and Y Co-ordinates.

This then feeds into the player Object and the speed of the player is determined by globle.thrust making it so that the player always moves forward in the direction it is facing.

the problem I think I may be having is while chekcing the bounding boxes, currently I am working out what bounding boxes to check by working out the angle at whcih the direction is facing 0-360 (note that i have worked this out as absolute as I was having issues with the direction going into - values when a full 360 had been achieved.

if anyone has any ideas from looking at my code that would be great!

FYI the collision tiles I am using are 64x64, the player objects sprite is also 64x64.

I am currently seeing the ship get stuck when travelling upwards, bad or no collision when travelling left, bad or no collision when travelling right and bad or no collision when travelling down....

Code:
``````speed = global.thrust;
var bbox_side;
var AbsoluteDirection = abs(direction);

if (AbsoluteDirection >= 0 && AbsoluteDirection <= 89.99999999)
{//Facing Right
bbox_side = bbox_right;
}else if (AbsoluteDirection >= 90 && AbsoluteDirection <= 179.9999999)
{//Facing Down
bbox_side = bbox_bottom;
}else if (AbsoluteDirection >= 180 && AbsoluteDirection <= 269.9999999)
{//Facing Left
bbox_side = bbox_left;
}else if (AbsoluteDirection >= 270 && AbsoluteDirection <= 359.9999999)
{//Facing Facing up
bbox_side = bbox_top;
}

if (tilemap_get_at_pixel(collisionTileMap, bbox_side+speed, bbox_top) != 0) || (tilemap_get_at_pixel(collisionTileMap, bbox_side+speed, bbox_bottom) != 0)
{
speed = 0;
global.thrust = 0;
if (AbsoluteDirection >= 0 && AbsoluteDirection <= 89.99999999)
{//Facing Right
x = x - (x mod 64) + 63 - (bbox_right - x);
}else if (AbsoluteDirection >= 90 && AbsoluteDirection <= 179.9999999)
{//Facing Down
y = y - (y mod 64) - 63 - (bbox_bottom - y);
}else if (AbsoluteDirection >= 180 && AbsoluteDirection <= 269.9999999)
{//Facing Left
x = x + (x mod 64) - 63 - (bbox_left - x);
}else if (AbsoluteDirection >= 270 && AbsoluteDirection <= 359.9999999)
{//Facing Facing up
y = y + (y mod 64) + 63 - (bbox_top - y);
}
}``````
MAx speed as standard is about 11 which may be why we are shotting through the collision tiles? I am also getting stuck on tiles and generally not detecting the tiles at all.

thanks for taking the time to read this post, if you need any further information ill be happy to provide!

#### TheouAegis

##### Member
You are using the built-in variable speed. So just use Shaun's code with hspeed and vspeed.

#### Chris Livermore

##### Member
I'm not sure that would work though (correct me if i am wrong) as my controls are different, the point direction is setup to the mouse cursor and you press "W" to accelerate so the craft only really has the option speed up.

#### TheouAegis

##### Member
I will say it again:
You are using the built-in variable speed. So just use Shaun's code with hspeed and vspeed.

You aren't using hsp and vsp, or hspd and vspd, or hmov and vmov, or xmov and ymov, or xspd and yspd. You are using speed, which means all your motion vectors are dictated by hspeed and vspeed. It doesn't matter what your direction is or how you set it.

#### Chris Livermore

##### Member
I will say it again:
You are using the built-in variable speed. So just use Shaun's code with hspeed and vspeed.

You aren't using hsp and vsp, or hspd and vspd, or hmov and vmov, or xmov and ymov, or xspd and yspd. You are using speed, which means all your motion vectors are dictated by hspeed and vspeed. It doesn't matter what your direction is or how you set it.
Sorry, Yes you are right, After much playing and trying to get it to work (my way I have given up and moulded my code to be the same as Shaun's

Code:
``````var AbsoluteDirection = abs(direction);
var hsp, vsp, calchsp, calcvsp;
calchsp = 0;
calcvsp = 0;

if (AbsoluteDirection >= 315 || AbsoluteDirection < 45 )
{//Facing Right
calchsp = 1;
}else if (AbsoluteDirection >= 45 && AbsoluteDirection < 135)
{//Facing Down
calcvsp = 1;
}else if (AbsoluteDirection >= 135 && AbsoluteDirection < 225)
{//Facing Left
calchsp = -1;
}else if (AbsoluteDirection >= 225 && AbsoluteDirection < 315)
{//Facing Facing up
calcvsp = - 1;
}else
{
calchsp = 0;
calcvsp = 0;
}
hsp = calchsp * speed;
vsp = calcvsp * speed;

//Horizontal Check
if (hsp > 0) bbox_side = bbox_right;
else bbox_side = bbox_left;
if (tilemap_get_at_pixel(collisionTileMap,bbox_side+hsp,bbox_top) !=0) || (tilemap_get_at_pixel(collisionTileMap,bbox_side+hsp,bbox_bottom) !=0)
{
if (hsp > 0) x = x - (x mod 64) + 63 - (bbox_right - x) ;
else x = x - (x mod 64) - (bbox_left - x) ;
speed = 0;
global.thrust = 0;

}

//Vertical Check
if (hsp > 0) bbox_side = bbox_bottom;
else bbox_side = bbox_top;
if (tilemap_get_at_pixel(collisionTileMap,bbox_side+vsp,bbox_left) !=0) || (tilemap_get_at_pixel(collisionTileMap,bbox_side+vsp,bbox_right) !=0)
{
if (vsp > 0) y = y - (y mod 64) + 63 - (bbox_bottom - y);
else y = y - (y mod 64) - (bbox_top - y);
speed = 0;
global.thrust = 0;

}``````
its still a bit clunky though (See the vieo below)

"
"