- Home /
Getting the cross product of vertices
Having problems with the cross product:
I am trying aim my player to the most nearby face of a cube so I wrote a script that detects the 3 most nearby vertices and then gives back the cross product between those vertices.
My problem is that when I start the game it places the player perfectly on the face but once I start moving the player my cross product function give back a vector of (0.0f,0.0f,0.0f). And no matter what I do the cross product funtion will always keep returning (0.0f,0.0f,0.0f);
Here is a demonstration of the problem:
https://www.youtube.com/watch?v=IDB3XC7ViuU&feature=youtu.be
At first it works fine, untill I move the player then it doesn't do anything anymore because NormalTri() doesn't return a vector with a magnitude higher then 0.
This is the funtion where the problem lies:
Vector3 NormalTri()
{
Vector3 infinity = new Vector3(Mathf.Infinity,Mathf.Infinity,Mathf.Infinity);
Vector3[] tri = new Vector3[]{infinity,infinity,infinity};
int[] prevIndexes = new int[]{-1,-1,-1};
for(int i = 0; i < 3; i++)
{
float magnitude = Mathf.Infinity;
int saveIndex = 0;
for(int ii = 0; ii < groundVerts.Length; ii++)
{
bool doubleCheck = false;
foreach(int p in prevIndexes)
{
if(ii == p)
{
doubleCheck = true;
}
}
if((groundVerts[ii] - obj.position).magnitude < magnitude && !doubleCheck)
{
magnitude = (groundVerts[ii] - obj.position).magnitude;
saveIndex = ii;
}
}
prevIndexes[i] = saveIndex;
tri[i] = groundVerts[saveIndex];
}
Vector3 sideA = tri[1] - tri[0];
Vector3 sideB = tri[2] - tri[0];
return Vector3.Cross(sideA,sideB).normalized;
}
groundVerts = The verts of the cube that keep being refreshd every update.
obj = the transform of the player.
Ground verts and obj are alway correctly stored so the problem cannot be that the is info missing.(tested that)
Also as a side question:
How do I know if my cross product points outwards of the cube and not inwards?
Cross product will always follow the "right hand rule". So if A x B points outward B X A will point the opposite direction. But if that's not what you are looking for, perhaps this is... Adding either vertex used to the crossproduct, then subtract the origin of the cube (crossprod + vertex - cubeorigin). The result will yield a vector with magnitude this is : LARGER than the (vertex - cubeorigin) if it's pointing outward, and S$$anonymous$$ALLER that the (vertex-cubeorigin), if pointing inward. You could probably even use model coordinates, rather than world, to eli$$anonymous$$ate that cubeorigin subtraction. In this case though, it would probably be wise to normalize both the vertex and cossproduct_result before adding them.How do I know if my cross product points outwards of the cube and not inwards?
Your answer
Follow this Question
Related Questions
Animating clip with transitions 2 Answers
Steps to determine cause of AOT Cross Compile Failed 1 Answer
Can't move a "cross platform input" object. 0 Answers
Adjusting GUI 1 Answer
[SOLVED] How to set an object's rotation parallel to a wall. 1 Answer