flyinian
Member
Hey all. I am currently attempting to get this tile collision to work but, haven't yet. However, i believe I managed to narrow what might be breaking the code for me. When I disable the "else" statements within the "if" statements the player collides with the tile and i am able to move left and right. However, when i enable the "else" statements, the player instantly goes down vertically and off to the right...I also go vertically down and off to the right when the entire code is active. I have been stuck on this code for many hours into attempting to get this code to work and I decided to try my hand here. Ill be going at it once again today and see if i can narrow it down further.
My player is 32 x 32 and the tiles are 64 x 64. So it can't be the player size being to big and bypassing the collision checking.
Here is the collision code:
Here is the variable code for the player:
Thank you.
My player is 32 x 32 and the tiles are 64 x 64. So it can't be the player size being to big and bypassing the collision checking.
Here is the collision code:
Code:
var dx = move_speed * (keyboard_check(vk_right) - (keyboard_check(vk_left)));
var dy = v_speed;
v_speed += grav;
//vertical
y += dy;
if ( dy > 0)
{
//down
var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom) & tile_index_mask;
var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom) & tile_index_mask;
if (t1 != 0 || t2 != 0)
{
y = ((bbox_bottom & ~ 63) -1) - sprite_bbox_bottom;
v_speed = 0;
}
}
else
{
///up
var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_top) & tile_index_mask;
var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_top) & tile_index_mask;
if(t1 != 0 || t2 = 0)
{
y = ((bbox_top + 64) & ~63) - sprite_bbox_top;
v_speed = 0;
}
}
///___________________
x += dx;
///right
if ( dx > 0)
{
var t1 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_top) & tile_index_mask;
var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom) & tile_index_mask;
if (t1 != 0 || t2 != 0)
{
x = ((bbox_right & ~ 63) -1) - sprite_bbox_right;
}
}
else
//left
{
var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_top) & tile_index_mask;
var t2 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom) & tile_index_mask;
if(t1 != 0 || t2 = 0)
{
x = ((bbox_left + 64) & ~63) - sprite_bbox_left;
}
}
Here is the variable code for the player:
Code:
move_speed = 8;
jump_impulse = 21;
grav = 0.75;
v_speed = 0;
//tilemap information
var l = layer_get_id("collision_map");
tilemap = layer_tilemap_get_id(l);
//sprite info
sprite_bbox_left = sprite_get_bbox_left(sprite_index) - sprite_get_xoffset(sprite_index);
sprite_bbox_right = sprite_get_bbox_right(sprite_index) - sprite_get_xoffset(sprite_index);
sprite_bbox_bottom = sprite_get_bbox_bottom(sprite_index) - sprite_get_yoffset(sprite_index);
sprite_bbox_top = sprite_get_bbox_top(sprite_index) - sprite_get_yoffset(sprite_index);
Thank you.