Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 30 December 2007 - 05:32 PM
Posted 30 December 2007 - 09:25 PM
Posted 31 December 2007 - 08:48 AM
Quote:You don't need to take the magnitude of any vector; the projections themselves are already scalars.
Original post by Trox
So then I just continue what I am doing and take the magnitude of the calculated projection vectors :) Thanks.
Posted 31 December 2007 - 11:58 AM
In general its a vector times a scalar divided by a scalar which returns a vector. That is why I am confused. I know that a vector projection returns a vector. I also know that the vector projection formula isn't simply the dot product of two vectors. In all the code examples I've found the dot product is being used instead of the vector projection formula above. Why?
[ A * (A dot B) ] / ( [ magnitute(A) ] ^ 2 )
/**
* Foreach edge of the polygons
* {
* I) Find the axis perpendicular to the edge
* a) Take the left hand normal
* II) Project both polygons onto that axis
* Instantiate min and max values for each polygon
* Foreach polygon
* {
* Foreach vertice on the polygon
* {
* a) Project the vertice onto the axis
* b) Take the length of the projection
* c) Compare the current min and max values to the length of the projection, keeping the better corresponding values
*
* Overlap conditions
*
* _______ _______ Projection Shadow
* |-----|
* |-----| No Overlap
* A B C D
*
* _______ _______ Projection Shadow
* |-----|
* |-----| No Overlap
* C D A B
*
* __________________ Projection Shadow
* |----------|
* |----------| Overlap
* A C B D
*
* __________________ Projection Shadow
* |----------|
* |----------| Overlap
* C A D B
* }
* }
* III) If the projections don't overlap, the polygons don't intersect (exit the loop)
* a) If the min or max of one polygon is between the min and max of the other polygon, then a collision exists
* }
**/
private void SeparatingAxisTheorem(Polygon a, Polygon b)
{
bool exitLoops = false;
Polygon[] polygons = new Polygon[]{a, b};
foreach (Polygon p in polygons)
{
Vector[] edges = p.Edges;
foreach (Vector edge in edges)
{
Vector axis = edge.LeftOrthogonal;
const int numberOfPolygons = 2;
float[] polygonMin = new float[numberOfPolygons];
float[] polygonMax = new float[numberOfPolygons];
for (int i = 0; i < numberOfPolygons; i++)
{
polygons[i].ProjectOnTo(axis, ref polygonMin[i], ref polygonMax[i]);
}
/**
* For the above examples
* A = polygonMin[0]
* B = polygonMax[0]
* C = polygonMin[1]
* D = polygonMax[1]
**/
System.Diagnostics.Debug.Print(polygonMin[0] + ", " + polygonMax[0] + " " + polygonMin[1] + ", " + polygonMax[1]);
if (
(polygonMin[0] < polygonMax[0] && polygonMax[0] < polygonMin[1] && polygonMin[1] < polygonMax[1])
||
(polygonMin[1] < polygonMax[1] && polygonMax[1] < polygonMin[0] && polygonMin[0] < polygonMax[0])
)
{
// No collision exists, exit the loop
exitLoops = true;
MessageBox.Show("Collision not found!");
}
if (exitLoops)
break;
}
if (exitLoops)
break;
}
// A collision was found!
MessageBox.Show("Collision found!");
}
public void ProjectOnTo(Vector v, ref float min, ref float max)
{
min = 1f;
max = -1f;
if(needEdgeUpdate)
CreateEdges();
foreach (Vector vertice in m_Edges)
{
Vector projectedVector = vertice.VectorProjectOnto(v);
float projectedLength = projectedVector.Length;
if (projectedLength < min)
min = projectedLength;
else if (projectedLength > max)
max = projectedLength;
}
}
public float Length
{
get { return (float)Math.Sqrt(m_X * m_X + m_Y * m_Y); }
}
public Vector LeftOrthogonal
{
get { return new Vector(-m_Y, m_X); }
}
public Vector VectorProjectOnto(Vector v)
{
return v.ScalarMultiply(this.DotProduct(v) / v.Length * v.Length);
}
public float DotProduct(Vector v)
{
return m_X * v.X + m_Y * v.Y;
}
Posted 31 December 2007 - 12:10 PM
Range ProjectPolygon(Vector verts[], Vector axis)And that's really all there is to it.
{
float min = FLOAT_MAX;
float max = -FLOAT_MAX;
for (each Vector in verts) {
float dot = dot_product(vert, axis);
min = minimum(min, dot);
max = maximum(max, dot);
}
return Range(min, max);
}
Posted 31 December 2007 - 12:41 PM
Quote:Oops, sorry - by the time I got to the end of the thread I had forgotten the mention of scalar vs. vector projections in your original post :)
Original post by Trox
That is exaclty what the confusion was. Thats why my first post mentioned the difference between the scalar and the vector projections. Thanks for the help.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.