JernejL

24/03/2010, 07:20 PM

First of all, discard the "UP" vector, the only vector of use currently is the FRONT vector.

The FRONT vector is simply a unit length (1 unit) vector direction in which the player is aiming from the camera position.

How to make sense of this..:

Basically, if you add camera front vector to the camera position vector, you will get another point in game space which is exactly 1 unit apart from the player and is in the direction where the player is looking at.

That camera target point can be then moved forward and backwards to have a point of reference against a specific target and see if the player is aiming towards it.

Example usage:

1. calculate distance from camera position to target check position (say a specific object)

2. rescale the front vector so it's length will be the distance to object to check, THIS IS VERY EASY - Since the vector is always of 1.0 length you can simply multiply the front vector XYZ values by the distance from #1, we will call this vector a tempcamtarget vector.

3. Now since you have the tempcamtarget point that is of known distance away from the target point and towards what the player is looking at, you can do a simple distance check from the tempcamtarget to the object position, the distance returned will be how far off from target object the player is looking, so you can know if he's looking at a certain object, or looking away from it.

EXAMPLE CODE:

Float:DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ, Float:ObjX, Float:ObjY, Float:ObjZ, Float:FrX, Float:FrY, Float:FrZ) {

new Float:TGTDistance;

// get distance from camera to target

TGTDistance = floatsqroot((CamX - ObjX) * (CamX - ObjX) + (CamY - ObjY) * (CamY - ObjY) + (CamZ - ObjZ) * (CamZ - ObjZ));

new Float:tmpX, Float:tmpY, Float:tmpZ;

tmpX = FrX * TGTDistance + CamX;

tmpY = FrY * TGTDistance + CamY;

tmpZ = FrZ * TGTDistance + CamZ;

return floatsqroot((tmpX - ObjX) * (tmpX - ObjX) + (tmpY - ObjY) * (tmpY - ObjY) + (tmpZ - ObjZ) * (tmpZ - ObjZ));

}

The result of the function is how close to the object centre the player is looking at, use it like - if value is < 1.0 then the player is looking at the object within target diameter of 2.0 units (for 1.0 radius.. diameter is 2.0)

Code is untested, but compiles and should work.

The FRONT vector is simply a unit length (1 unit) vector direction in which the player is aiming from the camera position.

How to make sense of this..:

Basically, if you add camera front vector to the camera position vector, you will get another point in game space which is exactly 1 unit apart from the player and is in the direction where the player is looking at.

That camera target point can be then moved forward and backwards to have a point of reference against a specific target and see if the player is aiming towards it.

Example usage:

1. calculate distance from camera position to target check position (say a specific object)

2. rescale the front vector so it's length will be the distance to object to check, THIS IS VERY EASY - Since the vector is always of 1.0 length you can simply multiply the front vector XYZ values by the distance from #1, we will call this vector a tempcamtarget vector.

3. Now since you have the tempcamtarget point that is of known distance away from the target point and towards what the player is looking at, you can do a simple distance check from the tempcamtarget to the object position, the distance returned will be how far off from target object the player is looking, so you can know if he's looking at a certain object, or looking away from it.

EXAMPLE CODE:

Float:DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ, Float:ObjX, Float:ObjY, Float:ObjZ, Float:FrX, Float:FrY, Float:FrZ) {

new Float:TGTDistance;

// get distance from camera to target

TGTDistance = floatsqroot((CamX - ObjX) * (CamX - ObjX) + (CamY - ObjY) * (CamY - ObjY) + (CamZ - ObjZ) * (CamZ - ObjZ));

new Float:tmpX, Float:tmpY, Float:tmpZ;

tmpX = FrX * TGTDistance + CamX;

tmpY = FrY * TGTDistance + CamY;

tmpZ = FrZ * TGTDistance + CamZ;

return floatsqroot((tmpX - ObjX) * (tmpX - ObjX) + (tmpY - ObjY) * (tmpY - ObjY) + (tmpZ - ObjZ) * (tmpZ - ObjZ));

}

The result of the function is how close to the object centre the player is looking at, use it like - if value is < 1.0 then the player is looking at the object within target diameter of 2.0 units (for 1.0 radius.. diameter is 2.0)

Code is untested, but compiles and should work.