Game Development Community

Getting coordinates by raycasting and terrain?

by Nic Biondi · in Torque Game Engine · 01/23/2004 (11:02 pm) · 11 replies

Setup:

I hope the solution to this problem will help others with similar issues

I have been trying to find a raycasting function that will return the coordinates of the ground where my mouse is pointing. I have the vector math, object masking, and static object creation down, mined from:

http://www.garagegames.com/docs/torque.sdk/tutorials/tutorial04/ - interaction and vector math
http://www.garagegames.com/mg/forums/result.thread.php?qt=15699 - deploying static object

thanx to martin, and james

Problem:

I am not sure what function to use to cast a ray that will return the position of the terrain that it hits. I'm assuming that there is a function similar to ContainerRayCast. so far My code looks something like this:
function serverCmdinteract(%client)
{ 
   //get player position
   %player = %client.player;
   
   //vector math
   %eye = %player.getEyeVector();
   %vec = vectorScale(%eye, 400);

   %startPoint = %player.getEyeTransform();
   %endPoint = VectorAdd(%startPoint,%vec);
   %searchMasks = $TypeMasks::TerrainObjectType;

   [b]
   //this code looks like junk
   %object = ContainerRayCast (%startPoint, %endPoint, %searchMasks, %player);
   %targetPosition=%object.getPosition();
   
   //somehow mine information from the object??
   %x = getWord(%targetPosition, 0);
   %y = getWord(%targetPosition, 1);
   %z = getWord(%targetPosition, 2);
   [/b]
   
   //create static object
    %item = new StaticShape(Flag)
    {
      position = localclientconnection.camera.getPosition();    
      rotation = "1 0 0 0";    
      datablock  = Flag;
      echo("onAdd");
    
    }
    %item.setTransform("%x %y %z");

#1
01/23/2004 (11:15 pm)
It may possibly be that what containerRayCast is returning is not what you are expecting. True, the first word of %object is an object reference, however, the next three words are the coordinates at the point of intersection of the ray with the terrain (or in other cases whatever the ray hit). If I am correct, you can replace the bold faced code in your example with the following:

%object = ContainerRayCast(%startPoint,%endPoint,%searchMasks, %player);
echo(">>>" @ %object);
%x = getWord(%object,1);
%y = getWord(%object,2);
%z = getWord(%object,3);

Note that the "echo" is just there so you can check out the contents of %object in the console.log if you want.
#2
01/23/2004 (11:16 pm)
ContainerRayCast returns a bunch of parameters. I believe if you just call it without any parameters TorqueScript will error and give you some documentation how to really call it and more importantly what it returns.
#3
01/23/2004 (11:25 pm)
I have containerRayCast because I was using it in the #4 torque tutorial, but I thought maybe there is a better function. however, I am only 2 weeks old and this may be an invalid assumption. I will try both of your suggestions. I appriciate the help.
#4
01/23/2004 (11:34 pm)
I have containerRayCast because I was using it in the #4 torque tutorial, but I thought maybe there is a better function. however, I am only 2 weeks old and this may be an invalid assumption. I will try both of your suggestions. I appriciate the help.
#5
01/23/2004 (11:53 pm)
I have containerRayCast because I was using it in the #4 torque tutorial, but I thought maybe there is a better function. however, I am only 2 weeks old and this may be an invalid assumption. I will try both of your suggestions. I appriciate the help.
#6
01/24/2004 (12:09 am)
Wow! spamming my own thread. I am a retard.
Mark, your advice works like a charm. I am now having a wierd problem where my code crashes (read: doesnt load). Maybe you, or someone as smart as you could shed some light on this matter.

I have narrowed the crashing code down to a couple lined depicted in BoLd:
function serverCmdinteract(%client)
{ 
   //get player position
   %player = %client.player;
   
   %eye = %player.getEyeVector();
   %vec = vectorScale(%eye, 400);

   %startPoint = %player.getEyeTransform();
   %endPoint = VectorAdd(%startPoint,%vec);
   %searchMasks = $TypeMasks::TerrainObjectType;

   //cast ray
   %object = ContainerRayCast(%startPoint,%endPoint,%searchMasks, %player); 
   echo(">object created>>" @ %object);
   
   //get
   %x = getWord(%object,1);
   %y = getWord(%object,2);
   %z = getWord(%object,3);
   
   
    //create static object
 [b]%item = new StaticShape(Flag)
    {
      position = localclientconnection.camera.getPosition();    
      rotation = "1 0 0 0";    
      datablock  = Flag;
    
    }[/b]
    %item.setTransform("%x %y %z");
}

do you know why this position function would crash when trying to create this object. (note setTransform is not the problem)
#7
01/24/2004 (12:50 am)
function serverCmdinteract(%client)
{ 
   //get player position
   %player = %client.player;
   
   %eye = %player.getEyeVector();
   %vec = vectorScale(%eye, 400);

   %startPoint = %player.getEyeTransform();
   %endPoint = VectorAdd(%startPoint,%vec);
   %searchMasks = $TypeMasks::TerrainObjectType;

   //cast ray
   %object = ContainerRayCast(%startPoint,%endPoint,%searchMasks, %player); 
   echo(">object created>>" @ %object);
   
   //get
   %x = getWord(%object,1);
   %y = getWord(%object,2);
   %z = getWord(%object,3);
   
   
    //create static object
 %item = new StaticShape(Flag)
    {
      position = localclientconnection.camera.getPosition();    
      rotation = "1 0 0 0";    
      datablock  = Flag;
    
    }[b];[/b]
    %item.setTransform("%x %y %z");
}
may make a difference ;)
#8
01/24/2004 (1:13 am)
Ahahah left out the semicolon. I noticed that just a minute ago. Good eye stefan, thanx
#9
01/24/2004 (1:17 am)
My new problem.. yes, the mind reels that there could be more, is that when I place my flag, it doesnt seem to go where my vector hits. My simple, and uneducated thoughts are that one of two things are hapening:

either I am building the transform out of order
(I thought I read that torque rewrote there commands to go against OpenGL and for the more intuitive x,y,x format).

Or I am missing some theory ont the vector/transform math, ie: world to object transform?
#10
01/24/2004 (1:21 am)
Err, oops, didnt recognize that...
what you want is:
%newPos = %x SPC %y SPC %z;
%item.setTransform(%newPos);
or
%item.setTransform(%x SPC %y SPC %z);

you are setting the pos to the STRING "%x %y %z" ;)
#11
01/24/2004 (1:29 am)
Thank you Thank you Thank you. Stefan, Mark, you guys are the best!

In conclusion, here is the code to point at the ground and spawn a static data object, in this case the flag object from the "Torque Getting Started Toutorial".

function serverCmdinteract(%client)
{ 
   //get player position
   %player = %client.player;
   
   %eye = %player.getEyeVector();
   %vec = vectorScale(%eye, 400);

   %startPoint = %player.getEyeTransform();
   %endPoint = VectorAdd(%startPoint,%vec);
   %searchMasks = $TypeMasks::TerrainObjectType;

   //cast ray
   %object = ContainerRayCast(%startPoint,%endPoint,%searchMasks, %player); 
   echo(">object created>>" @ %object);
   
   //get
   %x = getWord(%object,1);
   echo(">object created>>" @ %x);
   %y = getWord(%object,2);
   echo(">object created>>" @ %y);
   %z = getWord(%object,3);
   echo(">object created>>" @ %z);
   
   
 %item = new StaticShape(Flag)
 {  
    position = localclientconnection.camera.getPosition();
    rotation = "1 0 0 0";
    datablock  = Flag;
 };    
  
  %item.setTransform(%x SPC %y SPC %z);

    
}