Q
Queble
Guest
So I'm making a game where the player needs to collide with walls and stuff, right?
I watched every tutorial online, but there are none that show what i want (a player that can collide with tiles using his bounding box)
So I came up with this code, and the X dimension collisions work most of the time, but it is completely random and idk why it doesn't work
if anyone can spot the reason, or tell me how i can detect tiles with the bounding box and move accordingly using my xSpeed and ySpeed, that'd be much appreciated
EDIT: what this code is doing is: detecting if the bounding box (bbox) will collide with the tile layer (collisionmap) in the next frame of the game.
if so, the player will move into the current most adjacent tile ( snap to a 32x32 grid) and stop it's move speed.
Thanks!
-Queble
I watched every tutorial online, but there are none that show what i want (a player that can collide with tiles using his bounding box)
So I came up with this code, and the X dimension collisions work most of the time, but it is completely random and idk why it doesn't work
if anyone can spot the reason, or tell me how i can detect tiles with the bounding box and move accordingly using my xSpeed and ySpeed, that'd be much appreciated
GML:
//xSpeed and ySpeed is the velocity in each direction that the player is moving
//Horizontal Tiles
if (xSpeed < 0){
if (tilemap_get_at_pixel(collisionmap, bbox_left + xSpeed, bbox_top)) or (tilemap_get_at_pixel(collisionmap, bbox_left + xSpeed, bbox_bottom)){
x += bbox_left mod TILE_SIZE;
if (sign(xSpeed) == 1) x += TILE_SIZE - 1;
xSpeed = 0;
}
}
if (xSpeed > 0){
if (tilemap_get_at_pixel(collisionmap, bbox_right + xSpeed, bbox_top)) or (tilemap_get_at_pixel(collisionmap, bbox_right + xSpeed, bbox_bottom)){
x -= bbox_right mod TILE_SIZE;
if (sign(xSpeed) == 1) x += TILE_SIZE - 1;
xSpeed = 0;
}
}
//Horizontal Move commit
x += xSpeed;
//Vertical Tiles
if (ySpeed < 0){
if (tilemap_get_at_pixel(collisionmap, bbox_right + ySpeed, bbox_top)) or (tilemap_get_at_pixel(collisionmap, bbox_left + ySpeed, bbox_top)){
y += bbox_top mod TILE_SIZE;
if (sign(ySpeed) == 1) y += TILE_SIZE - 1;
ySpeed = 0;
}
}
if (ySpeed > 0){
if (tilemap_get_at_pixel(collisionmap, bbox_right + ySpeed, bbox_bottom)) or (tilemap_get_at_pixel(collisionmap, bbox_left + ySpeed, bbox_bottom)){
y -= bbox_bottom mod TILE_SIZE;
if (sign(ySpeed) == 1) y += TILE_SIZE - 1;
ySpeed = 0;
}
}
//Vertical Move commit
y += ySpeed;
if so, the player will move into the current most adjacent tile ( snap to a 32x32 grid) and stop it's move speed.
Thanks!
-Queble