Game Development Community

CastRay returning Point3F/MatrixF

by Caleb · in Torque Game Engine · 07/22/2006 (4:16 pm) · 6 replies

I've posted lots of threads on getting collision on mounted images. I know there is most likely many ways to accomplish this so I'm going to be more spicific. I need to get castRay to return a location rather then a true or false value.

Doing something like MatrixF swordRayCol = CastRay(positionOfNodeOne, positionOfNodeTwo, NULL);. This is sure to make Visual C++ scream at since it can't do conversion from bool to MatrixF.

So as the name says, I need a way to get CastRay to return a Point3F or MatrixF value. anything is possible.........I think. If this can be done, I should have a pretty good way to do melee stuff. Even if this doesn't pan out, I can still think a half a dozen other stuff to use this on.

Thanks for any help you can give me.

#1
07/22/2006 (5:44 pm)
I while back I posted an C/C++ example on TDN called CastRayExample. I think it shows all you need to know.
#2
07/22/2006 (9:08 pm)
RayInfo ftw.
#3
07/23/2006 (5:41 am)
First rule in Torque dev: never start writing code and using functions blindly. Whenever you need to write new stuff, do a throughtful search in the source code for examples of the features you want to use.

A quick search for CastRay would reveal several examples on how to use it, and you would've get to know about the RayInfo parameter.
#4
07/23/2006 (5:27 pm)
Arg! I didn't realize Casting Rays would be so important when I was learning about it with DirectX.

Since rewriting another Ray Casting function that fits my needs would be kinda over the top, you think you could maybe show me how to modify the following to make it work?

void ShapeBase::RayCollision( F32 dt,U32 imageSlot)
{
   MountedImage& image = mMountedImageList[imageSlot];
   ShapeBaseImageData* imageData = image.dataBlock;

   //Node Positions of the Player's Ray Nodes
   MatrixF startTrans;
   getImageTransform( imageSlot, imageData->damageStartNode, &startTrans );
   MatrixF endTrans;
   getImageTransform( imageSlot, imageData->damageEndNode, &endTrans );
   
   //The positions
   VectorF vecStartA = startTrans.getPosition();
   VectorF vecEndA = endTrans.getPosition();



   //Get the Node Positions of the AI's Ray Nodes
   MatrixF startTakeTrans;   
   getImageTransform( imageSlot, imageData->damageTakeStartNode, &startTakeTrans );
   MatrixF endTakeTrans;
   getImageTransform( imageSlot, imageData->damageTakeEndNode, &endTakeTrans );

   //The positions
   VectorF vecStartB = startTakeTrans.getPosition();
   VectorF vecEndB = endTakeTrans.getPosition();
}

And in the same function I need to cast two rays, one for the player's sword and one for the AI's.
Then in script, call the function, it might need to be a consol function but I can do that later.

Maybe I should make it a bool function so if the ray collide, it would be true and if not then it could be false.
Anyway I just need help on the cast ray part for now.

Thanks again.
#5
07/23/2006 (5:53 pm)
Were you even listening? There are innumerable examples of castray already in the engine, use those for reference. But anyways, what you want is something like this.
//Node Positions of the Player's Ray Nodes
   MatrixF startTrans;
   getImageTransform( imageSlot, imageData->damageStartNode, &startTrans );
   MatrixF endTrans;
   getImageTransform( imageSlot, imageData->damageEndNode, &endTrans );
   
   //The positions
   Point3F startPos;
   startTrans.getColumn(3, &startPos);
   Point3F endPos;
   endTrans.getColumn(3, &endPos);

   //What types of objects you want to check for.
   static U32 swordMask = ShapeBaseObjectType;

   RayInfo swordRay;
   
   disableCollision();  //Just in case so it doesn't collide with itself

   if(gServerContainer.castRay(startPos, endPos, swordMask , &swordRay)){
   //Do stuff
   }

   enableCollision();
And you can use the swordRay to get things like position, normal, what was hit, etc.
#6
07/24/2006 (7:19 am)
Sorry, I was listening, I was just really busy yesterday and didn't have time to be on my computer so I just mad a quick post. Thanks for your help.

Thanks all!!!!