Note that there are some explanatory texts on larger screens.

plurals
  1. POXNA 4.0 3D Collision
    text
    copied!<p><br> I've been trying to work on a 3d forward-runner game (like temple run) in XNA 4.0. <br> I'm hitting a bit of a brick wall, so any help would be much appreciated! <br> Currently, I'm using my own method for collision detection, which requires the dimensions for each model to be hard-coded into the collision method. I've tried using the code from Microsoft, directly below, but it always returns false:</p> <pre><code> static bool CheckForCollisions(Entity c1, Entity c2) { for (int i = 0; i &lt; c1.body.Meshes.Count; i++) { // Check whether the bounding boxes of the two cubes intersect. BoundingSphere c1BoundingSphere = c1.body.Meshes[i].BoundingSphere; c1BoundingSphere.Center += c1.position; for (int j = 0; j &lt; c2.body.Meshes.Count; j++) { BoundingSphere c2BoundingSphere = c2.body.Meshes[j].BoundingSphere; c2BoundingSphere.Center += c2.position; if (c1BoundingSphere.Intersects(c2BoundingSphere)) { return true; } } } return false; } </code></pre> <p>This was taken and modified very slightly from, <a href="http://msdn.microsoft.com/en-us/library/bb203906.aspx" rel="nofollow">Here</a> My Entity class goes as follows.</p> <p>Code of mine which I think is relevant would be:</p> <pre><code> public class Entity { public int rowID; public Model body; public Vector3 position; public float rotation = 0f; public float rotatePerFrame = 0f; protected internal float toRight = 0; protected internal float toLeft = 0; protected internal float forward = 0; protected internal float back = 0; protected internal float bottom = 0; protected internal float top = 0; public void setDimensions(float right, float left, float front, float back, float top, float bottom) { this.toRight = right; this.toLeft = left; this.forward = front; this.back = back; this.top = top; this.bottom = bottom; } public Entity RotateEnt(Entity e,float degrees)//Psuedo-only accurate to 90 degrees. { float actual = MathHelper.ToDegrees(degrees); switch ((int)actual) { case 0: break; case 90: // float temp = e.forward; // e.forward = e.toLeft; // e.toLeft =e.back ; // e.back = e.toRight; // e.toRight = temp; float temp = e.forward; e.forward = e.toRight; e.toRight = e.back; e.back = e.toLeft; e.toLeft = temp; break; case 180: e.forward = e.back; e.back = e.forward; e.toRight = e.toLeft; e.toLeft = e.toRight; break; default: //case: 270 e.toRight = e.forward; e.back = e.toRight; e.toLeft = e.back; e.forward = e.toLeft; break; } return e; } public bool Collides(Entity e) { Entity c1 = RotateEnt(this, this.rotation); Entity c2 = RotateEnt(e, e.rotation); float myRightest = c1.position.X + c1.toRight; float myLeftest = c1.position.X - c1.toLeft; float hisRightest = c2.position.X + c2.toRight; float hisLeftest = c2.position.X - c2.toLeft; if(Collides1D(myLeftest, myRightest, hisLeftest, hisRightest)) { float myTop = c1.position.Y + c1.top; float myBottom = c1.position.Y - c1.bottom; float hisTop = c2.position.Y + c2.top; float hisBottom = c2.position.Y - c2.bottom; if (Collides1D(myBottom, myTop, hisBottom, hisTop)) { float myBack = c1.position.Z - c1.forward; float myForward = c1.position.Z + c1.back; float hisBack = c2.position.Z - c2.forward; float hisForward = c2.position.Z + c2.back; if (Collides1D(myBack, myForward, hisBack, hisForward)) { return true; } } } return false; } } static bool Collides1D(float left1, float right1, float left2, float right2) { if (left1 &gt;= left2 &amp;&amp; left1 &lt;= right2) return true; if (right1 &gt;= left2 &amp;&amp; right1 &lt;= right2) return true; if (left2 &gt;= left1 &amp;&amp; left2 &lt;= right1) return true; if (right2 &gt;= left1 &amp;&amp; right2 &lt;= right1) return true; return false; } </code></pre> <p>My own method has been screwing up also, when trying to rotate models.<br> Ideally, it would be good to know what is wrong with the code from Microsoft, so that I can use it wherever, without worrying about hard-coding in object dimensions.<br> If anyone can see a fast fix to my own basic collision detection method, that would also be great.<br> I've looked at the Reimers tutorials, but I'm not getting them at the moment, maybe I've been staring at my own code for too long...<br> Any other information you want, I can try and supply. I can upload the models also, if that's the problem. I'm using models from Maya, exported as FBX. I'm using MSVS 2010. Thanks very much!<br> Jack</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload