3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: Pedrodx's Radar Resource  (Read 2460 times)

« on: August 21, 2008, 09:53:51 PM »
Credit for this Radar Resource rightfully go to Pedrodx, But there are still a few of my lines mixed in there ;) 
I added a couple lines to disable the radar return sprite when it leaves the boundary of the radar base sprite..
This radar gives impressive results, but the user needs to keep in mind that a few things will need to be adjusted for this to work properly in your Project:

The Values of returnx and returny  will need to be changed to correspond to the screen location of your base sprite,  these floats coordinate the player object and the radar base sprite as relative center points...

scaleFactor  should be changed depending on your world size....

The last two lines of code turn off the radar return sprite when it leaves the boundary of the radar base sprite... This will have to be tweaked according to the size of your world...( a hint to help out with this is to attach the relativeLocation.z to a value print object to determine the proper setting)

Quote

//
//   Pedrodx's   Radar Script
//    This script is intended to allow
//      Radar Functionality
//OBJ_22= player object
//OBJ_0= Target Object
//OBJ_44= Radar Return Sprite
//

Vector3 crossProduct(Vector3 v1, Vector3 v2) {
   Vector3 v;
   v.x = v1.y * v2.z - v1.z * v2.y;
   v.y = v1.z * v2.x - v1.x * v2.z;   
   v.z = v1.x * v2.y - v1.y * v2.x;
   return v;
}

   Vector3 radarReturn;
   Vector3 playerLocation;
   Vector3 targetLocation;
    Vector3 relativeLocation;
   Quaternion playerOrientation;
   Quaternion spriteOrientation;
   float x;
   float y;
   float z;
 
   //the center x and y coords of the radar return (from Sprite)
   const float returnx = -11.35f;  // H coord of Base sprite
   const float returny = -8.15f;  // Y coord of Base sprite
   const float scaleFactor = 25; //Change this value to scale the radar to your world size...
void Main ()
{
   iObjectOrientation(OBJ_22, playerOrientation);
   
   iObjectLocation(OBJ_0,targetLocation); //get target location
   iObjectLocation(OBJ_22,playerLocation); //get player location
   
   relativeLocation.x=targetLocation.x - playerLocation.x;
   relativeLocation.y=0;
   relativeLocation.z=targetLocation.z - playerLocation.z;
   
   //check out for target left or right relative to the player
   Vector3 playerRotationZ;
   iVectorRotate(playerRotationZ, Vector3(0, 0, 1), playerOrientation);
   
   //desconsider the camera pitch
   playerRotationZ.y = 0;

   //new: normalizes the playerRotation vector... fuzzy logic...
   float przLength = iVectorLength(playerRotationZ);
   if (przLength > 0)
      playerRotationZ /= przLength;

   Vector3 cpLR = crossProduct(playerRotationZ, relativeLocation);
   OUT_66 = cpLR.x;
   OUT_88 = cpLR.y;
   OUT_110 = cpLR.z;

   //check out for target front or behind relative to the player
   Vector3 playerRotationX;
   iVectorRotate(playerRotationX, Vector3(1, 0, 0), playerOrientation);

   //desconsider the camera pitch
   playerRotationX.y = 0;

   //new: normalizes the playerRotation vector... fuzzy logic...
   float prxLength = iVectorLength(playerRotationX);
   if (prxLength > 0)
      playerRotationX /= prxLength;

   Vector3 cpFB = crossProduct(playerRotationX, relativeLocation);
   
   //cpXX is as if it was a "distance" value...
   radarReturn.x = returnx + cpLR.y / scaleFactor;
   radarReturn.y = returny - cpFB.y / scaleFactor;

   iObjectLocationSet(OBJ_44,radarReturn); //set radar return location

   if (relativeLocation.z > 90) OUT_44 = 0;///These two lines turn off the Radar Return when It leave the boundaries of the Base Sprite
   else (OUT_44=1);

}
« Last Edit: August 21, 2008, 11:33:54 PM by archangel »
« Reply #1 on: August 21, 2008, 11:27:08 PM »
yeah, man!! Here are my "Compass orientation manager"... It orientates a compass to point towards a target... Please, note the code is basically the same, but the variables were renamed to fit my code (the idea behind this all is that we have cpXX.y as rigid offsets (sorry if I'm expressing wrong). But, sure, this is the old trial and error math...
Code: [Select]
Vector3 crossProduct(Vector3 v1, Vector3 v2) {
   Vector3 v;
   v.x = v1.y * v2.z - v1.z * v2.y;
   v.y = v1.z * v2.x - v1.x * v2.z;
   v.z = v1.x * v2.y - v1.y * v2.x;
   return v;
}

void Main ()
{
   Vector3 playerLocation, opponentLocation;
   iObjectLocation(OBJ_66, playerLocation);
   iObjectLocation(OBJ_0, opponentLocation);
   Vector3 player2Opponent = opponentLocation - playerLocation;
   //desconsider the pitch
   player2Opponent.y = 0;
   Quaternion playerOrientation;
   iObjectOrientation(OBJ_66, playerOrientation);
   //check out for target left or right relative to the player
   Vector3 playerRotationZ;
   iVectorRotate(playerRotationZ, Vector3(0, 0, 1), playerOrientation);
   //desconsider the pitch
   playerRotationZ.y = 0;
   //new: normalizes the playerRotation vector... fuzzy logic...
   float przLength = iVectorLength(playerRotationZ);
   if (przLength > 0)
      playerRotationZ /= przLength;
   Vector3 cpLR = crossProduct(playerRotationZ, player2Opponent);   
   //check out for target front or behind relative to the player
   Vector3 playerRotationX;
   iVectorRotate(playerRotationX, Vector3(1, 0, 0), playerOrientation);
   //desconsider the pitch
   playerRotationX.y = 0;
   //new: normalizes the playerRotation vector... fuzzy logic...
   float prxLength = iVectorLength(playerRotationX);
   if (prxLength > 0)
      playerRotationX /= prxLength;
   Vector3 cpFB = crossProduct(playerRotationX, player2Opponent);
   //builds the actual rotation
   Vector3 spriteRotation;
   spriteRotation.x = cpLR.y;
   spriteRotation.y = -cpFB.y;
   spriteRotation.z = 0;
   Quaternion spriteOrientation;
   iQuaternionLookAt(spriteOrientation, spriteRotation, Vector3(0, 0, -1));
   iObjectOrientationSet(OBJ_22, spriteOrientation);
}
« Reply #2 on: December 08, 2008, 11:41:29 PM »
Now, we have a function that calculates the cross product...
The crossProduct() call could/should be replaced with a iVectorCross() call...
 ;)
« Reply #3 on: January 19, 2010, 12:05:38 PM »
I was wondering if someone knew how to modify this to get the rotation around the X axis. I've made a few attempts myself, but I just don't have a firm enough grasp of the math involved to figure out how to change it to do what I need. And btw, this is a great resource!
Pages: [1]