NimonoSolenze
Member
I've been trying to make a projectile that will bounce off of enemies in the opposite direction- bouncing off things is working, but I've been massively struggling to figure out how to check what side of the instance it's likely impacting. Here's the current code from my GetSideCollide() function:
At some locations, the projectile will merely pass through due to returning -1 (which is my default return just so there's SOMETHING it returns), since there's some edge cases here- if the projectile is completely within the enemy's hitbox on left or right, for example, it can never bounce off from above or below because the hitbox for it isn't outside the enemy's hitbox. I can add a check for that easily, but then it doesn't solve the edge case of topcheck being less than leftcheck and rightcheck somehow, despite the fact that visually, it has clearly impacted the top of the hitbox, not the left and right sides. Anyone have any recommendations for what I could do instead to fix this? Am I going to have to do a ray loop to check from the prior frame's position to see which side of the bbox it was to have hit? That seems to be the 'easiest' way to do it, but it feels like there MUST be some easier, less intensive way...
I based it on information I got from a 2-year-old thread here, but there was VERY few details on how to do it other than "well just check if this value is bigger or smaller than this other value, and if so, it likely collided there!" It seemed to be for actually finding the point of impact, but what I need is not the point of impact, but the SIDE of impact. I can't simply use angles to find the corners because enemy hitboxes won't be just perfect squares, there'll be rectangles, too.
In case it helps: The projectile is a 4x4 hitbox moving at 12 speed in one of the diagonal angles only. The enemy I'm using to test is a perfect square that's 15x15.
Code:
// Obtain the side of collision. 0 = left; 1 = right; 2 = top; 3 = bottom;
var leftA = argument0; // edges of area to check for a collision from
var rightA = argument1;
var topA = argument2;
var bottomA = argument3;
var leftB = argument4; // edges of area to check for a collision with
var rightB = argument5;
var topB = argument6;
var bottomB = argument7;
var leftcheck = abs(leftB - leftA); // distance between sides of collision areas
var rightcheck = abs(rightB - rightA);
var topcheck = abs(topB - topA);
var bottomcheck = abs(bottomB - bottomA);
if(rightA >= leftB && leftA <= leftB) // left side collision check
{
if((leftcheck >= topcheck && topA < topB) || (leftcheck >= bottomcheck && bottomA > bottomB)) // if this side of impact has more area left over than other relevant sides...
{
return 0; // return that there was a collision on the left side
}
}
if(leftA <= rightB && rightA >= rightB) // right side collision check
{
if((rightcheck >= topcheck && topA < topB) || (rightcheck >= bottomcheck && bottomA > bottomB)) // if this side of impact has more area left over than other relevant sides...
{
return 1; // return that there was a collision on the right side
}
}
if(bottomA >= topB && topA <= topB) // top side collision check
{
if((topcheck >= leftcheck && leftA < leftB) || (topcheck >= rightcheck && rightA > rightB)) // if this side of impact has more area left over than other relevant sides...
{
return 2; // return that there was a collision on the top side
}
}
if(topA <= bottomB && bottomA >= bottomB) // bottom side collision check
{
if((bottomcheck >= leftcheck && leftA < leftB) || (bottomcheck >= rightcheck && rightA > rightB)) // if this side of impact has more area left over than other relevant sides...
{
return 3; // return that there was a collision on the bottom side
}
}
return -1;
I based it on information I got from a 2-year-old thread here, but there was VERY few details on how to do it other than "well just check if this value is bigger or smaller than this other value, and if so, it likely collided there!" It seemed to be for actually finding the point of impact, but what I need is not the point of impact, but the SIDE of impact. I can't simply use angles to find the corners because enemy hitboxes won't be just perfect squares, there'll be rectangles, too.
In case it helps: The projectile is a 4x4 hitbox moving at 12 speed in one of the diagonal angles only. The enemy I'm using to test is a perfect square that's 15x15.