Game Development Community

Spawn rotation

by Very Interactive Person · in Torque Game Engine · 09/08/2004 (11:10 am) · 6 replies

Can anybody tell me what i'm doing wrong here?
I tried a couple of things already, and this code is what I think the most right way of handling the rotation problem (I want the new spawnpoint to have the same rotation as the original one). But this code doesn't seem to work, and the other things I tried are even less stable (I mean more random).
Why is the rotation of my object not the same as the one of the spawnpoint??

Here's the code:
function rotFromTransform(%transform)
{
   %rotation = getWord(%transform, 3) @ " " @ getWord(%transform, 4) @ " " @ getWord(%transform, 5) @ " " @ getWord(%transform, 6);
   return %rotation;
}
function pickSpawnPoint() 
{
    %groupName = "MissionGroup/PlayerDropPoints";
    %group = nameToID(%groupName);

    if (%group != -1)
    {
         %count = %group.getCount();
         if (%count != 0)
         {
               %index = getRandom(%count-1);
               %spawn = %group.getObject(%index);


	       %getNewPos = getTerrainLevel(%spawn.position,%spawn.radius);
               %rot = rotFromTransform(%spawn.getTransform());
               %newSpawn = %getNewPos @ " " @ %rot;
	       return %newSpawn;


         }
         else  error("No spawn points found in " @ %groupName);
     }
     else  error("Missing spawn points group " @ %groupName);
     return "0 0 300 1 0 0 0";
}

function getTerrainLevel(%pos,%rad)
{
   while(%retries < 500)
   {
      %x = getWord(%pos, 0) + mFloor(getRandom(%rad * 2) - %rad);
      %y = getWord(%pos, 1) + mFloor(getRandom(%rad * 2) - %rad);
      %z = getWord(%pos, 2) + mFloor(getRandom(%rad * 2) - %rad);
      
      %start       = %x @ " " @ %y @ " 5000";
      %end       = %x @ " " @ %y @ " -1";
      %ground    = containerRayCast(%start, %end, $TypeMasks::TerrainObjectType, 0);
      %z       = getWord(%ground, 3);


      %z += 3.5;
      
      %position = %x @ " " @ %y @ " " @ %z;
      
      %mask = ($TypeMasks::VehicleObjectType |
         $TypeMasks::MoveableObjectType |
         $TypeMasks::StaticShapeObjectType |
         $TypeMasks::ForceFieldObjectType |
         $TypeMasks::InteriorObjectType |
         $TypeMasks::ItemObjectType);

      if (ContainerBoxEmpty(%mask,%position,2.5))
      {
         return %position;
      }
      else
         %retries++;
   }
   return pickSpawnPoint();
}

#2
09/08/2004 (2:31 pm)
Lolz.... your reply didn't help me code wise, after all its exactly that code I was trying to fix. But... after looking over it again I realized that the second time I spawn I'm not using these functions, but something else, that's why it all stopped working after the first spawn! So anyway, the code above works just fine, and is actually a fix to the code from the resource wich didn't work (at least not for me).
#3
05/05/2005 (4:28 am)
Try this spawn function instead...

function pickSpawnPoint() 
{
	%group = nameToID(PlayerDropPoints);

	if(%group != -1)
	{
		%count = %group.getCount();

		if(%count != 0)
		{
			%spawn = %group.getObject(getRandom(%count-1));
			%getNewPos = getTerrainLevel(%spawn.position,%spawn.radius);
			%rot = %spawn.rotation;
			%newSpawn = %getNewPos SPC %rot;
			return %newSpawn;
		}
		else  error("No spawn points found in " @ %groupName);
	}
	else  error("Missing spawn points group " @ %groupName);

	return "0 0 300 1 0 0 0";
}
#4
05/05/2005 (4:41 am)
That *spawn* looks like a VERY HARD fall... hie hie hie...
#5
05/05/2005 (6:02 am)
@Juan: Please use your own work to make a game. Specially if you post screenshots in public places. You might upset the original authors of the work. In this case all I'm upset about is you screwed the already bad looking artwork ;) lol.
#6
05/05/2005 (6:49 am)
To new spawners :)

It might help to understand the transform structure.

There are 7 numbers in the transform matrix, such as the default "0 0 300 1 0 0 0"

The first three are the x, y, and z position (usually the easiest to understand).

The second three are the rotation axis x, y, and z. This is usually the problem as the default of "1 0 0" actualy creates a rotation around the x axis which is why the players end up on their head as it rotates them forward. Now, since most people want to simply turn their player to a different heading, the rotation axis should be "0 0 1". This will solve most problems.

The last number is the rotation amount in RADIANS. A 360 degree turn is 2 PI or 6.28, a 180 degree turn is PI or 3.14, and a 90 degree turn is half PI or 1.57.


Getting terrain height is usually an issue as well, and I wish a certain command was better publicised (i,e in the demo).

%spawn = %group.getObject(%index);
%point = getWords(%spawn, 0, 1); // this gets the x and y position
// if you're using a spawn point, get the rotation otherwise you can set it directly
%rot = getWord(%spawn, 6);
%z = getTerrainHeight(%point);
// if you want to add a little to make them fall or bounce
// %z += 1;
%spawn = %point SPC %z SPC "0 0 1" SPC %rot;

Voila, you are perfectly on the terrain in the right rotation.

Thanks.