XNA Game Development Forums
2012/05/18 06:11:54 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Check if an object is facing another object..?  (Read 1900 times)
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« on: 2007/02/26 13:55:32 »

Hey all

My space dogfighting game is starting to take shape now... Wink

I'm trying to establish how to check if an object in space (represented by a vector3 for position and a quaternion for rotation) is facing a vector.

The function I've put together looks like this:

public static bool IsFacing(Vector3 point, XNAObject xnaObject)
        {
            XNAObject obj = new XNAObject();
           
            obj.Position = xnaObject.Position;
            obj.Rotation = xnaObject.Rotation;

            obj.LookAt(point, 1.0f);

            return Quaternion.Equals(obj.Rotation, xnaObject.Rotation) == true;
        }

Which, suprise, never returns true.  my XNAObject class just contains position (vector3) and rotation (quaternion) information, and various methods for translation and rotation.  I thought this would work - copy the object in question, force it to look at the vector, and then check if the quaternions match.  However this never returns true, even though my object is moving to a certain distance away from the camera (target vector) and then turning around to face it.

Additionally, it would be nice to incorporate a bit of "slippage" into this function, i.e. "Am I facing a vector, but it's OK to be out by 5 degress in any direction?)

I want to do this so that when enemy fighters spin round to chase down my ship, they can open fire.

Anyone got any ideas?

Cheers!
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #1 on: 2007/02/26 14:05:26 »

Hi Again

I've answered my own question - but thought I'd post the answer here in case it's of use to anyone:

public static bool IsFacing(Vector3 point, XNAObject xnaObject)
        {
            XNAObject obj = new XNAObject();
           
            obj.Position = xnaObject.Position;
            obj.Rotation = xnaObject.Rotation;

            obj.LookAt(point, 1.0f);

            obj.Rotation.Normalize();
            xnaObject.Rotation.Normalize();

            if ((obj.Rotation.X >= xnaObject.Rotation.X - 0.2f) &&
                 (obj.Rotation.X <= xnaObject.Rotation.X + 0.2f) &&
                 (obj.Rotation.Y >= xnaObject.Rotation.Y - 0.2f) &&
                 (obj.Rotation.Y <= xnaObject.Rotation.Y + 0.2f) &&
                 (obj.Rotation.Z >= xnaObject.Rotation.Z - 0.2f) &&
                 (obj.Rotation.Z <= xnaObject.Rotation.Z + 0.2f))
            {
                return true;
            }
            else { return false; }
        }

Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #2 on: 2007/02/26 14:07:14 »

And the LookAt function, for reference, although I'm pretty sure this came from the HM engine:

public virtual void LookAt(Vector3 target, float factor)
        {           
            Vector3 tminusp = target - objPosition;
            Vector3 ominusp = Vector3.Forward;

            tminusp.Normalize();

            float theta = (float)System.Math.Acos(Vector3.Dot(tminusp, ominusp));
            Vector3 cross = Vector3.Cross(ominusp, tminusp);
            string x = cross.X.ToString();
            string y = cross.Y.ToString();
            string z = cross.Z.ToString();
           
            cross.Normalize();
            if (cross.X.ToString().Contains("NaN")) cross.X = 0.0f;
            if (cross.Y.ToString().Contains("NaN")) cross.Y = 0.0f;
            if (cross.Z.ToString().Contains("NaN")) cross.Z = 0.0f;

            Quaternion targetQ = Quaternion.CreateFromAxisAngle(cross, theta);
            Slerp(targetQ, factor);
        }               
Logged
Tiago
Newbie
*
Offline Offline

Posts: 42


View Profile
« Reply #3 on: 2007/03/08 22:25:08 »

actually i think theres a better way. if you do a multiplication (fotgot if its dot or cross product) between the vector your facing at and the vector that goes from your position to the position you wanna check you can get the angle between the 2 vectors, sorry, i dont remember exactly the math involved in this but i hope this helps in some way.
Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #4 on: 2007/03/08 22:41:54 »

Its a dot product Smiley If the vector normals of the two faces are dot producted and the result is negative, then they are facing each other. Getting to know if they overlap at all (their outward projection from the face, like in radiosity models) is a bit more difficult.
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Tutorial 2 :: Scene Graph and Object Framework Tutorial Discussion « 1 2 » mikeschuld 18 10042 Last post 2008/07/31 22:50:22
by mikeschuld
Tutorial 10 :: Preparing for Object Culling, a Simple Octree Tutorial Discussion « 1 2 » mikeschuld 17 7358 Last post 2010/11/23 09:07:20
by Neil_Knight
MOVED: Terrain Object Show Off Board Chr0n1x 0 2317 Last post 2007/01/27 23:23:52
by Chr0n1x
Parenting Camera to an Object Hazy Mind XNA Engine Requests rstackhouse 4 3096 Last post 2007/03/14 06:13:57
by 5parrowhawk
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.652 seconds with 19 queries.