Author Topic: Easiest way to detect side of brick with projectile::onCollision  (Read 2924 times)

/title
Wanting to detect the side of the brick that the projectile is hitting, in order to properly rotate a static object that is being spawned.

Check the last argument of onCollision
It's a normal vector, that is, a vector perpendicular to the surface it hit, so on a cuboid brick it will either be "-1 0 0",  "1 0 0",  "0 -1 0",  "0 1 0",  "0 0 -1",  or "0 0 1" and you can just build a small lookup table with which represents which direction ("0 0 1" would be the top surface, "0 0 -1" the bottom surface, etc)
It won't be quite that simple for bricks with round/diagonal edges in the collision mesh
« Last Edit: February 12, 2015, 09:23:28 PM by Headcrab Zombie »

Thanks, I believe I can pull off some decent maths for round/curved meshes if it returns somewhere between -1 to 1

if it returns somewhere between -1 to 1

Even if it didn't, you could use vectorNormalize( vec ) to normalize the length to 1.

I need a second pair of eyes to look over the simple math I did, East/West works fine and dandy, but the other 4 sides make the shape point North
Code: [Select]
//-1 0 0 = 0 0 1 90 East
//1 0 0 = 0 0 1 270 West

//0 -1 0 = 0 0 1 0 South
//0 1 0 = 0 0 1 180 North

//0 0 1 = 1 0 0 90 Up
//0 0 -1 = 1 0 0 270 Down

function returnRotation(%normal)
{
if(getWord(%normal,0) !$= 0)
{
%number = getWord(%normal,0);
%rot = %number * 90 + 180;
%rot = "0 0 1" SPC %rot;
return %rot;
}
if((getWord(%normal,1) !$= 0) && (getWord(%normal,2) $= 0))
{
%number = getWord(%normal,1);
%rot = %number * 90 + 90;
%rot = "0 0 1" SPC %rot;
return %rot;
}
if(getWord(%normal,2) !$= 0)
{
%number = getWord(%normal,2);
if(%number > 0)
{
%rot = %number * 90;
%rot = "1 0 0" SPC %rot;
return %rot;
}
if(%number < 0)
{
%number = %number * "-1";
%rot = %number * 90 + 180;
return %rot;
}
}
}

Just calculate the angle directly from the normal instead of using set conditions:

function returnTransform(%position, %normal) {
    return %position SPC getWords(MatrixCreateFromEuler(vectorScale(%normal, $pi / 180)), 3, 6);
}


Edit: corrected 180 / $pi to $pi / 180. Damn radian conversions.
« Last Edit: February 13, 2015, 06:53:15 PM by $trinick »

Just calculate the angle directly from the normal instead of using set conditions:
Yeah ideally I would have told him that but I didn't know the math