Wile94
Member
Hi, I'm working on a platformer (if you saw my posts i'm always working on a platformer ). Currently I'm looking the best way to perform some collisions without running the place_meeting and instance_place function constantly. I coded a moving platform using the next lines of code:
Is heavy coded but is working really well so far. As you can see, is the Player/Instance checking for the nearest platform and not the other way around. But now I wanna code a static fading platform (those that when you land on top will fade after a short period of time and you will fall) and I don't know if make the player/instance check when is near a platform or the platform do the checking with those instance.
I have this concern because the player/instance won't be colliding with that platform per se, in fact it will detect in that region of the ds_grid that there are cells with a "PLATFORM" value and land on top of them.
So my question is, how much running instance_nearest cost performance wise? It would be better to just use with() and make a distance check instead? If I do it that way my script would end up like this
I've already tested it and I didn't see any difference in my fps.
Code:
movingPlatformID = noone;
phyMovPlatform = false;
Code:
//Save half sprite sizes
var midw = sprite_width >> 1;
var midh = sprite_height >> 1;
if (onGround)
{
if (!phyMovPlatform) movingPlatformID = noone; else check_movplat_position();
}
else
{
//Check for moving platform distance
if (!phyMovPlatform) check_movplat_nearest();
}
//Check Y Collision
var vdir = sign(verVel);
var vvround = math_round_dir(verVel * DT,vdir);
if (vvround != 0)
{
//{
//ds_grid collision code here
//}
//Check for moving platform
if (!phyMovPlatform) if (vvround > 0)
{
var land_on_movplat = place_meeting_movplat();
//Land on platform
if (land_on_movplat)
{
y = movingPlatformID.bbox_top - midh;
verVel = 0;
phyGround = true;
phyMovPlatform = true;
}
}
}
Code:
//PLATFORMS ALREADY MOVED IN BEGIN STEP EVENT
//Save platform movement
var platform_hmove = (movingPlatformID.moveType == 0 || movingPlatformID.moveType == 2);
var platform_vmove = (movingPlatformID.moveType == 1 || movingPlatformID.moveType == 2);
//horizontal
if (platform_hmove)
{
//Match last object position with platform
var xadd = movingPlatformID.x - movingPlatformID.xprevious;
x += xadd;
//Check ds_grid wall collision
var check_wall = ds_grid_get_cell_at(bbox_left,
bbox_top,
bbox_right,
bbox_bottom,
gridID,CELL_WALL);
//Collide with walls
if (check_wall)
{
x = math_round_dir(x,-sign(xadd));
x -= xadd;
}
}
//vertical
if (platform_vmove)
{
//Match last object position with platform
var yadd = movingPlatformID.y - movingPlatformID.yprevious;
y += yadd;
//Check ds_grid wall collision
var check_wall = ds_grid_get_cell_at(bbox_left,
bbox_top,
bbox_right,
bbox_bottom,
gridID,CELL_WALL);
//Collide with walls
if (check_wall)
{
//If movement is circular, avoid top collision bugging
if(movingPlatformID.moveType == 2)
{
//most likely to hit on top
var ydir = movingPlatformID.pathYDir;
if (!ydir)
{
var cell_to_snap = (bbox_top div CELL)+1;
y = ds_grid_snap(y,ydir,cell_to_snap,sprite_height >> 1);
}
}
y = math_round_dir(y,-sign(yadd));
y -= yadd;
}
}
//Check if still on top of the platform
var on_platform = check_box_on_box(bbox_left,bbox_right,
bbox_bottom+1,bbox_bottom+1,
movingPlatformID.bbox_left,
movingPlatformID.bbox_right,
movingPlatformID.bbox_top,
movingPlatformID.bbox_top);
//Continue sticking to platform / Reset parameters
phyMovPlatform = on_platform;
if (!phyMovPlatform) movingPlatformID = noone;
exit;
Code:
//Check for nearest moving platform and get an ID
movingPlatformID = instance_nearest(x,y,objMovPlatform);
if (movingPlatformID)
{
//Check if a minimum distance is met
var min_dist = CELL * (movingPlatformID.image_xscale+1);
var dist_to_platform = (point_distance(x,y,movingPlatformID.x,movingPlatformID.y) < min_dist);
if (!dist_to_platform) movingPlatformID = noone;
}
exit;
Code:
//Check if desired moving platform is on meeting range
var r = false;
if (movingPlatformID)
{
//Check if object is beneath platform
var platform_below = bbox_bottom < movingPlatformID.bbox_top+1;
//Already touchig the platform?
if (place_meeting(x,y,movingPlatformID))
{
//check if platform was already below in the previous step
var ydiff = (movingPlatformID.y - movingPlatformID.yprevious);
platform_below = bbox_bottom < (movingPlatformID.bbox_top+1)-ydiff;
}
//Parameters are met
if (platform_below)
{
//Check the collision
if (place_meeting(x,y+(verVel * DT + 1),movingPlatformID))
{
//Return ID just in case (this line would eventually not be needed?)
movingPlatformID = instance_place(x,y+(verVel * DT + 1),movingPlatformID);
//Confirm collision
if (movingPlatformID.canLand) r = true;
}
}
}
return r;
exit;
Is heavy coded but is working really well so far. As you can see, is the Player/Instance checking for the nearest platform and not the other way around. But now I wanna code a static fading platform (those that when you land on top will fade after a short period of time and you will fall) and I don't know if make the player/instance check when is near a platform or the platform do the checking with those instance.
I have this concern because the player/instance won't be colliding with that platform per se, in fact it will detect in that region of the ds_grid that there are cells with a "PLATFORM" value and land on top of them.
So my question is, how much running instance_nearest cost performance wise? It would be better to just use with() and make a distance check instead? If I do it that way my script would end up like this
Code:
//Check for nearest moving platform and get an ID
movingPlatformID = noone;
with(objMovPlatform)
{
var min_dist = CELL * (image_xscale+1);
var dist_to_instance = (point_distance(x,y,other.x,other.y) < min_dist);
if (dist_to_instance)
{
other.movingPlatformID = id;
break;
}
}
exit;
I've already tested it and I didn't see any difference in my fps.
Last edited: