Game Development Community

How do you find if the player is on the ground or not?

by Chris Cain · in Torque Game Engine · 01/17/2004 (1:37 pm) · 15 replies

I'm trying to find when the player is on the ground... but I'm not sure how I should go about doing this... does anyone know how?

#1
01/17/2004 (2:00 pm)
In the C++ code I would look at Player::UpdateMove()
It has a check to see if the player is on the ground (has to do with the jump code)

As far as I know, there is no 'easy' script method of doing this, but it is possible to do in script I just dont have any code handy for it
#2
01/17/2004 (2:06 pm)
Well thanks for your response, but I was looking at a way to do it in script (its has to do with one of the commands I'm making in the script... basically, it teleports you where you want, and I don't want to teleport straight through the ground)
#3
01/17/2004 (2:18 pm)
John,

I would think the "containerRayCast" function would work for you with a mask of "$TypeMasks::TerrainObjectType"

Get the location where you plan on placing the player and do a RayCast down. If not "above" the ground move the player up.

I use it to "see" if a vehicle is infront of the player for mounting.

Some of that code from my command.cs file is below:
(note this code looks in front of the player. It would not work as a solution. I only post it for examples of how the function is used)
%selectRange = 13;
 // Only search for vehicles Or Turrets!
 %searchMasks =  $TypeMasks::vehicleObjectType|$TypeMasks::TurretObjectType;
 %pos = %client.player.getEyePoint();
 // Start with the shape's eye vector...
 %eye = %client.player.getEyeVector();
 %eye = vectorNormalize(%eye);
 %vec = vectorScale(%eye, %selectRange);
 %end = vectorAdd(%vec, %pos);

 %scanTarg = ContainerRayCast (%pos, %end, %searchMasks);
 // a target in range was found so select it
 if (%scanTarg)
 {
   // Code here for the ray found it.
 }

Hope that helps.

PS Code wasn't my brainstorm it came From
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=3925
// Generic Car Code
And modified to also work for mounting with Turrets.
#4
01/17/2004 (2:24 pm)
Thanks! I'll try this right away!
#5
01/17/2004 (2:26 pm)
John,

After you posted I edited my post for the disclaimer that the code would need to be altered for a solution. It is not ment as an answer only as an example of how the function works.

Hope it helps.
#6
01/17/2004 (2:38 pm)
Ok after borrowing some code from the Tribes 2 beacon... I've come up with something like this:
%vector = %player.getForwardVector();
%searchRange = 3.0;
%x = 0;
%y = 0;
%z = 90 * 3.141592654 / 180;
%matrix = MatrixCreateFromEuler( %x SPC %y SPC %z);
%finalVector = MatrixMulVector( %matrix, %vector );
%mask = $TypeMasks::TerrainObjectType | $TypeMasks::InteriorObjectType | $TypeMasks::StaticShapeObjectType | $TypeMasks::ForceFieldObjectType;
%feetPos = posFromTransform(%player.getTransform());
%scFeetVec = VectorScale(VectorNormalize(%finalVector), %searchRange);
// add the scaled & normalized eye vector to the position of the camera
%feetEnd = VectorAdd(%feetPos, %scFeetVec);
%searchResult = containerRayCast(%feetPos, %feetEnd, %mask, 0);

I'm sorry I don't know how to post it in one of those neat code boxes.... but you get the idea...
This code works SOME of the time... but not always... can anyone help me?
#7
01/17/2004 (2:55 pm)
John

The "neat code boxes" and other "neat" formatting stuff can be done by following the directions by clickin on the "here" in the line:
Quote:
(Want to use bold, italics and add links to your text? Click here to learn how)
located at the bottom of the box you use to post a reply.

As far as the code goes... I am not sure. How does it fail? Does it fail to find a searchResult?
#8
01/17/2004 (2:59 pm)
Sometimes it fails to find a searchResult, but sometimes it does find one... its strange...
#9
01/17/2004 (3:08 pm)
It would fail if the object isn't found within the search range. The code appears to search from the feet down 3.0 units (the %searchRange). If the characters feet were stuck in the ground the search down would fail to find the ground because the ground would be above the characters feet. It would also fail if the ground was more than 3.0 units below the character's foot.

If I am wrong and the 3.0 actually is searching up, the searchResult would fail if the ground was below the players feet.

Get what I mean?

(sorry I can't give a better answer than that. My vector math is really poor.)
#10
01/17/2004 (3:15 pm)
Well, what is strange is it *seems* it is searching from the sides...
#11
01/17/2004 (4:15 pm)
Ok... I went from using the "Z" to using the "X" and I did a new way of seeing where the vector was going... well, that works half way.... when I'm facing south, it points down, but when I face north, it points straight up! ... Please someone who is an expert at vectors, please explain to me how to fix this! It'd mean alot to me :)
%x = (90 * 3.1415926) / 180;
      %y = 0;
      %z = 0;
      %matrix = MatrixCreateFromEuler( %x SPC %y SPC %z);
      %finalVector = MatrixMulVector( %matrix, %vector );
      %mask = $TypeMasks::TerrainObjectType | $TypeMasks::InteriorObjectType | $TypeMasks::StaticShapeObjectType | $TypeMasks::ForceFieldObjectType;
      %feetPos = posFromTransform(%player.getTransform());
      %scFeetVec = VectorScale(VectorNormalize(%finalVector), %searchRange);
      // add the scaled & normalized eye vector to the position of the camera
      %feetEnd = VectorAdd(%feetPos, %scFeetVec);
      %searchResult = containerRayCast(%feetPos, %feetEnd, %mask, 0);
#12
01/17/2004 (4:28 pm)
John,

That makes sense the "%player.getForwardVector();" I assume is the forward looking view or vector. Down is what you want.

(Thats what I thought when I looked at it. Maybe my vector math isn't as bad as I think it is)

Try this:
// Get current transform of the player into a var.
    %startPos = %player.getTransform(); 
    // Set endPos a vector with an off set of 3.0 in the 'z' direction.
    %endPos = getWord(%startPos , 0) @ " " @ getWord(%startPos , 1)@ " " @ getWord(%startPos , 2) + 3.0 @ " 1 0 0 0";
%searchResult = containerRayCast(%startPos , %endPos , %mask);

Now it probally would be good to use the "feet" I believe the "%player.getTransform(); " returns the origin. Also I haven't tried the above code, but in theory it is what I would try.

Hope I am not leading you down the wrong path.
#13
01/17/2004 (4:55 pm)
It works!!!
I had to modify the + 3.0 to be - 3.0... but it works now!!!
Thank you sooo much, thank you thank you thank you!!!
(thank you)*57192471825791283691826598162398162598162348712498612598612^1000000 < what I wish I could thank you.

To anyone who ever needs the full code... this is about it! It needs cleaning up, but here you go!

%vector = %player.getForwardVector();
      %mask = $TypeMasks::TerrainObjectType | $TypeMasks::InteriorObjectType | $TypeMasks::StaticShapeObjectType | $TypeMasks::ForceFieldObjectType;
      %feetPos = posFromTransform(%player.getTransform());
      %feetEnd = getWord(%feetPos , 0) @ " " @ getWord(%feetPos , 1)@ " " @ getWord(%feetPos , 2) - 3.0 @ " 1 0 0 0";
      %searchResult = containerRayCast(%feetPos, %feetEnd, %mask, 0);
#14
01/17/2004 (5:33 pm)
Oh yeah, btw, the reason I used "getForwardVector" is because it gets the exact front of the player (without "Z" axis) which is better than using muzzle, or eye in something like this... but actually, now that I look at the code, now the vector isn't used at all! LOL.
#15
07/19/2004 (2:44 pm)
@Dan I been trying to use your code snippit for getting a ray hit on a object and all I get is 0 can you help me some?