Coding Ray Cast Doors
by Andrew Scheuerer · in Game Design and Creative Issues · 05/07/2009 (5:13 pm) · 4 replies
I am trying to use code found under the resources part of this website to implement ray casting to detect what the player is 'aiming' at, and play an animation or dialogue box based on the object being used.
When Torque loads, it should find doors.cs under the shapes folder. This file should have all the information needed to track the status (open or closed) of the door, and play open/close animations. Commands.cs on the server side has the code for ray casting, and default.bind.cs on the client side sets up the 'e' key as the use button.
My problem is that the door only opens, and stays there. The player is able to detect that it is a door, but it will not close. I've been out of the loop on scripting for a while now, and cannot figure out what I am doing wrong... any thoughts?
And thanks in advance for any input.
// Commands.cs file has this code for ray casting:
function serverCmdUseButton(%client, %arg)
{
// scope the player
%player = %client.getControlObject();
// key is being pressed
if(%arg)
{
if(!%client)
{
return;
}
if(%player.isMounted == 1)
{
return;
}
// RAY_CASTING
//echo("%client: " @ %client);
//echo("%arg: " @ %arg);
//echo("%player: " @ %player);
// muzVec is the vector coming from the player's eyes
%muzVec = %player.GetEyeVector();
// muzNVec = normalized muzVec
%muzNVec = VectorNormalize(%muzVec);
// range of ray
%range = 50;
// scale muzNVec to the range the beam can reach
%muzScaled = VectorScale(%muzNVec, %range);
// muzPoint = the actual point of the gun's "muzzle"
%muzPoint = %player.GetEyePoint();
// rangeEnd = muzzle point + length of beam
%rangeEnd = VectorAdd(%muzPoint, %muzScaled);
%searchMasks = $TypeMasks::StaticObjectType |
$TypeMasks::StaticShapeObjectType |
$TypeMasks::CorpseObjectType |
$TypeMasks::ItemObjectType |
$TypeMasks::VehicleBlockerObjectType |
$TypeMasks::VehicleObjectType |
$TypeMasks::WaterObjectType;
// search for objects within the beam's range that fit the masks above
%scanTarg = ContainerRayCast(%muzPoint, %rangeEnd, %searchMasks, %obj);
if(%scanTarg)
{
%serverID = getWord(%scanTarg,0);
// echo("%scanTarg.getClassname(): " @ %scanTarg.getClassname());
switch$(%scanTarg.getClassname())
{
case StaticShape:
echo("Player is targeting Static Shape: " @ %serverID);
%id=%serverID.getName();
if(%id.isOpen==0)
{
openDoor(%id);
}
if(%id.isOpen==1)
{
closeDoor(%id);
}
case Player:
echo("Player is targeting another player - Server ID = " @ %serverID);
default:
echo("No Target");
}
}
}
// key is being released
else
{
// Do Nothing
}
}
//------------------
//------------------
//------------------
// Doors.cs file with data blocks and functions for opening/closing
// doors
//------------------
datablock StaticShapeData(door)
{
category = "Doors";
shapeFile = "scriptsAndAssets/data/shapes/doors/door/door.dts";
};
datablock StaticShapeData(womensdoor)
{
category = "WDoors";
shapeFile = "scriptsAndAssets/data/shapes/doors/womendoor/womendoor.dts";
};
datablock StaticShapeData(mensdoor)
{
category = "MDoors";
shapeFile = "scriptsAndAssets/data/shapes/doors/mendoor/mendoor.dts";
};
datablock StaticShapeData(ventdoor)
{
category = "VDoors";
shapeFile = "scriptsAndAssets/data/shapes/doors/ventdoor/ventdoor.dts";
};
function door::onAdd(%this, %obj)
{
%obj.isOpen=0;
}
function openDoor(%id)
{
%id.playThread(0, "DoorOpen");
%obj.isOpen=1;
}
function closeDoor(%id)
{
%id.playThread(0, "DoorClose");
%obj.isOpen=0;
}
When Torque loads, it should find doors.cs under the shapes folder. This file should have all the information needed to track the status (open or closed) of the door, and play open/close animations. Commands.cs on the server side has the code for ray casting, and default.bind.cs on the client side sets up the 'e' key as the use button.
My problem is that the door only opens, and stays there. The player is able to detect that it is a door, but it will not close. I've been out of the loop on scripting for a while now, and cannot figure out what I am doing wrong... any thoughts?
And thanks in advance for any input.
// Commands.cs file has this code for ray casting:
function serverCmdUseButton(%client, %arg)
{
// scope the player
%player = %client.getControlObject();
// key is being pressed
if(%arg)
{
if(!%client)
{
return;
}
if(%player.isMounted == 1)
{
return;
}
// RAY_CASTING
//echo("%client: " @ %client);
//echo("%arg: " @ %arg);
//echo("%player: " @ %player);
// muzVec is the vector coming from the player's eyes
%muzVec = %player.GetEyeVector();
// muzNVec = normalized muzVec
%muzNVec = VectorNormalize(%muzVec);
// range of ray
%range = 50;
// scale muzNVec to the range the beam can reach
%muzScaled = VectorScale(%muzNVec, %range);
// muzPoint = the actual point of the gun's "muzzle"
%muzPoint = %player.GetEyePoint();
// rangeEnd = muzzle point + length of beam
%rangeEnd = VectorAdd(%muzPoint, %muzScaled);
%searchMasks = $TypeMasks::StaticObjectType |
$TypeMasks::StaticShapeObjectType |
$TypeMasks::CorpseObjectType |
$TypeMasks::ItemObjectType |
$TypeMasks::VehicleBlockerObjectType |
$TypeMasks::VehicleObjectType |
$TypeMasks::WaterObjectType;
// search for objects within the beam's range that fit the masks above
%scanTarg = ContainerRayCast(%muzPoint, %rangeEnd, %searchMasks, %obj);
if(%scanTarg)
{
%serverID = getWord(%scanTarg,0);
// echo("%scanTarg.getClassname(): " @ %scanTarg.getClassname());
switch$(%scanTarg.getClassname())
{
case StaticShape:
echo("Player is targeting Static Shape: " @ %serverID);
%id=%serverID.getName();
if(%id.isOpen==0)
{
openDoor(%id);
}
if(%id.isOpen==1)
{
closeDoor(%id);
}
case Player:
echo("Player is targeting another player - Server ID = " @ %serverID);
default:
echo("No Target");
}
}
}
// key is being released
else
{
// Do Nothing
}
}
//------------------
//------------------
//------------------
// Doors.cs file with data blocks and functions for opening/closing
// doors
//------------------
datablock StaticShapeData(door)
{
category = "Doors";
shapeFile = "scriptsAndAssets/data/shapes/doors/door/door.dts";
};
datablock StaticShapeData(womensdoor)
{
category = "WDoors";
shapeFile = "scriptsAndAssets/data/shapes/doors/womendoor/womendoor.dts";
};
datablock StaticShapeData(mensdoor)
{
category = "MDoors";
shapeFile = "scriptsAndAssets/data/shapes/doors/mendoor/mendoor.dts";
};
datablock StaticShapeData(ventdoor)
{
category = "VDoors";
shapeFile = "scriptsAndAssets/data/shapes/doors/ventdoor/ventdoor.dts";
};
function door::onAdd(%this, %obj)
{
%obj.isOpen=0;
}
function openDoor(%id)
{
%id.playThread(0, "DoorOpen");
%obj.isOpen=1;
}
function closeDoor(%id)
{
%id.playThread(0, "DoorClose");
%obj.isOpen=0;
}
#2
05/08/2009 (11:41 am)
Thanks for the lead, I'll play with the code today and see what I can get.
#3
05/08/2009 (2:57 pm)
Look at the codefunction openDoor(%id)
{
%id.playThread(0, "DoorOpen");
%obj.isOpen=1;
}
function closeDoor(%id)
{
%id.playThread(0, "DoorClose");
%obj.isOpen=0;
}What is %obj? perhaps you mean %id
#4
I've decided to nix the close button, and just put the door close action on a schedule.
05/08/2009 (3:09 pm)
Yeah, %id makes it work closer to what I was going for, but I was still unable to get the code I had to work as an open AND close button.I've decided to nix the close button, and just put the door close action on a schedule.
Torque Owner Infinitum3D
www.garagegames.com/community/resources/view/10200
Try it out. If you can't get it to work, I can post my scripts for you tomorrow.
Tony