Tilebased slope collision

E

elix4

Guest
Hey guys,

I am curently programming a 2d platformer. For collision detection I am using tile based collision. The problem iam facing at the moment is slope collision with upsidedown slopes.

i somtimes clip into them and i dont know why... :/
maybe someone could help me with that.
Here is my current collision code:

var tilemap;
var bbox_side;
var on_floor;
var p1;
var p2;
var floor_dist;

tilemap = layer_tilemap_get_id(layer_get_id("tile_collide"));
on_floor = in_tile(tilemap, x, bbox_bottom+1) >= 0 && in_tile(tilemap, x, bbox_bottom+1) < global.tilesheet_width * 2;

//walk
xaxis_speed = (o_input.right - o_input.left) * WALK_SPEED;

//jump
if((on_floor || (in_tile(tilemap, bbox_right, bbox_bottom+1) >= 0) || (in_tile(tilemap, bbox_left, bbox_bottom+1) >= 0)) && o_input.jump){
yaxis_speed = JUMP_SPEED;
on_floor = false;
}
yaxis_speed += GRAVITY;


xaxis_speed_fraction = xaxis_speed - floor(xaxis_speed);
xaxis_speed -= xaxis_speed_fraction;
yaxis_speed_fraction = yaxis_speed - floor(yaxis_speed);
yaxis_speed -= yaxis_speed_fraction;


if(xaxis_speed > 0) bbox_side = bbox_right; else bbox_side = bbox_left;
p1 = tilemap_get_at_pixel(tilemap, bbox_side + xaxis_speed, bbox_top);
p2 = tilemap_get_at_pixel(tilemap, bbox_side + xaxis_speed, bbox_bottom);
if(tilemap_get_at_pixel(tilemap, x, bbox_bottom) > 1 && (yaxis_speed >= 0 || on_floor)) p2 = 0;
if(tilemap_get_at_pixel(tilemap, x, bbox_top) > 1) p1 = 0;
if((p2 == 1) || (p1 == 1)){
if(xaxis_speed > 0){
x = x - (x % TILE_SIZE) + TILE_SIZE - 1 -(bbox_right - x);
}else{
x = x - (x % TILE_SIZE) - (bbox_left - x);
}
xaxis_speed = 0;
xaxis_speed_fraction = 0;
}
x += xaxis_speed;

//vertical collision
if(yaxis_speed > 0) bbox_side = bbox_bottom; else bbox_side = bbox_top;
p1 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_side + yaxis_speed);
p2 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_side + yaxis_speed);
if(tilemap_get_at_pixel(tilemap, x, bbox_top + yaxis_speed) > global.tilesheet_width * 2){
p1 = 0;
p2 = 0;
}
if(tilemap_get_at_pixel(tilemap, x, bbox_bottom + yaxis_speed) <= 1){
if((p2 == 1) || (p1 == 1)){
if(yaxis_speed > 0){
y = y - (bbox_bottom % TILE_SIZE) + TILE_SIZE - 1;
}else{
y = y - (y % TILE_SIZE) - (bbox_top - y);
}
yaxis_speed = 0;
yaxis_speed_fraction = 0;
}
}

y += yaxis_speed;

//ceiling slopes
if(tilemap_get_at_pixel(tilemap, x, bbox_top) >= global.tilesheet_width * 2){
floor_dist = in_tile(tilemap, x, bbox_top);
show_debug_message(floor_dist);
if(floor_dist <= 0){
y -= floor_dist;
if(yaxis_speed < 0){
yaxis_speed = 0;
yaxis_speed_fraction = 0;
}
}

}

//floor slopes
floor_dist = in_tile(tilemap, x, bbox_bottom);
if(tilemap_get_at_pixel(tilemap, x, bbox_bottom) < global.tilesheet_width * 2 && floor_dist >= 0){
y -= floor_dist + 1;
yaxis_speed = 0;
yaxis_speed_fraction = 0;
floor_dist = -1;
}

if(on_floor){
y += abs(floor_dist)-1;
if(bbox_bottom % TILE_SIZE == TILE_SIZE - 1){
if(tilemap_get_at_pixel(tilemap, x, bbox_bottom + 1) > 1){
y += abs(in_tile(tilemap, x, bbox_bottom + 1));
}
}
}

xaxis_speed += xaxis_speed_fraction;
yaxis_speed += yaxis_speed_fraction;
 
E

elix4

Guest
var tile_index = tilemap_get_at_pixel(argument0, argument1, argument2);

if(tile_index > 0){
if(tile_index = 1) return argument2 % TILE_SIZE;
if(tile_index >= global.tilesheet_width && tile_index < global.tilesheet_width * 2){
var floor_height = global.height_table_floor[(argument1 % TILE_SIZE) + ((tile_index-global.tilesheet_width) * TILE_SIZE)];
return (argument2 % TILE_SIZE) - floor_height;
}
if(tile_index >= global.tilesheet_width * 2 && tile_index < global.tilesheet_width * 3){
var floor_height = global.height_table_ceil[(argument1 % TILE_SIZE) + ((tile_index - global.tilesheet_width * 2) * TILE_SIZE)];
return (argument2 % TILE_SIZE) - floor_height;
}
}
return - (TILE_SIZE - (argument2 % TILE_SIZE));

this is wthat the in_tile function does. it basicly returns the offset to the floor in pixel from the x and y position you gave it.
 
Top