Game Development Community

Detecting object, changing material and highlighting object

by Linmin Pei · in Game Design and Creative Issues · 07/21/2010 (3:20 pm) · 1 replies

how to detect object in T3D? right now i am doing a project using Torque 3D. sometimes i need to change material or highlight object. firstly, we need know whether there is an object in front of character. then we can do something else.

here is my solution that maybe helpful for you. when the character "see/approach to" an object, its material will be changed to nematerial6(of course it is not good, i just test my idea). when the character goes away, the material changes back. also it will show the hand shape cursor if its acquirement is qualified.

code (game/scripts/server/player.cs):

$Player::useTraceDist = 2;
$Player::previousObject=0;

function Player::scheduledUpdate(%this)
{
// additional update for client sided player
if(%this == %this.client.getControlObject())
{

%this.checkObjectsForUse();
}
%this.schedule(150, "scheduledUpdate");
}

function Player::checkObjectsForUse(%this)
{
%eyeVec = %this.getEyeVector();

%startPos = VectorAdd(%this.getEyePoint(), VectorScale(%eyeVec, 0.2));
%endPos = VectorAdd(%startPos, VectorScale(%eyeVec, $Player::useTraceDist));

//%this.useTarget = ContainerRayCast(%startPos, %endPos, $TypeMasks::StaticShapeObjectType, %this);
%this.useTarget = ContainerRayCast(%startPos, %endPos, $TypeMasks::ShapeBaseObjectType, %this);
// echo("+++++++++++++++++++++++++++++: ",%this.useTarget);
%this.useTarget = getWord(%this.useTarget, 0);


// LMP, change material to a , add isSelectable()
if (%this.useTarget != $Player::previousObject) {
if (!$Player::previousObject) {
//echo("TargetName: ",%this.useTarget.getTargetName(0));
//echo("global object: ",$Player::previousObject);
%this.useTarget.changeMaterial(%this.useTarget.getTargetName(0),0,newMaterial6);
$Player::previousObject = %this.useTarget;
}
else {
//echo("restore global object: ",$Player::previousObject);
$Player::previousObject.changeMaterial($Player::previousObject.getTargetName(0),0,defaultMaterial6);
if (!%this.useTarget) {
//echo("useTarget is empty: ",%this.useTarget);
$Player::previousObject = 0;
}
else {
// This part has not been tested yet!!!!!
//echo("do sth for new target: ",%this.useTarget);
%this.useTarget.changeMaterial(%this.useTarget.getTargetName(0),0,newMaterial6);
$Player::previousObject = %this.useTarget;
}
}
}


if(%this.useTarget != 0 )
{
echo("what is the pick up name: ",%this.useTarget.getFieldValue("usable"));
Reticle.setVisible(false);
Hand.setVisible(true);
return %this.useTarget;
}
else
{

Reticle.setVisible(true);
Hand.setVisible(false);
}

}

PS: I will post the code about highlighting object later

About the author

Recent Threads


#1
07/21/2010 (3:25 pm)
Continue, i fixed out how to highlight object. hopefully it is useful for you.

sample code (location: game/scripts/server/player.cs):
$Player::useTraceDist = 2;
$Player::previousObject=0;
//$oldMaterial = 0;

function Player::scheduledUpdate(%this)
{
// additional update for client sided player
if(%this == %this.client.getControlObject())
{

%this.checkObjectsForUse();
}
%this.schedule(150, "scheduledUpdate");
}

function Player::checkObjectsForUse(%this)
{
%eyeVec = %this.getEyeVector();

%startPos = VectorAdd(%this.getEyePoint(), VectorScale(%eyeVec, 0.2));
%endPos = VectorAdd(%startPos, VectorScale(%eyeVec, $Player::useTraceDist));

//%this.useTarget = ContainerRayCast(%startPos, %endPos, $TypeMasks::StaticShapeObjectType, %this);
%this.useTarget = ContainerRayCast(%startPos, %endPos, $TypeMasks::ShapeBaseObjectType, %this);
//echo("+++++++++++++++++++++++++++++: ",%this.useTarget);
%this.useTarget = getWord(%this.useTarget, 0);

//echo("----------------------------: ",%this.useTarget.getName());


// LMP, change material to a , add isSelectable()
if (%this.useTarget != $Player::previousObject) {
if (!$Player::previousObject) {
echo("TargetName: ",%this.useTarget.getTargetName(0));
%oldMaterial = getMaterialMapping(%this.useTarget.getTargetName(0));
//echo("global object: ",$Player::previousObject);
%oldMaterial.setFieldValue(glow, 1);
%oldMaterial.setFieldValue(emissive, 1);
%oldMaterial.reload();
//%this.useTarget.changeMaterial(%this.useTarget.getTargetName(0),0,newMaterial6);
$Player::previousObject = %this.useTarget;
}
else {
//echo("restore global object: ",$Player::previousObject);
%oldMaterial = getMaterialMapping($Player::previousObject.getTargetName(0));
%oldMaterial.setFieldValue(glow, 0);
%oldMaterial.setFieldValue(emissive, 0);
%oldMaterial.reload();
//$Player::previousObject.changeMaterial($Player::previousObject.getTargetName(0),0,defaultMaterial6);
if (!%this.useTarget) {
//echo("useTarget is empty: ",%this.useTarget);
$Player::previousObject = 0;
}
else {
// This part has not been tested yet!!!!!
echo("do sth for new target: ",%this.useTarget);
//%this.useTarget.changeMaterial(%this.useTarget.getTargetName(0),0,newMaterial6);
%oldMaterial = getMaterialMapping(%this.useTarget.getTargetName(0));
//echo("global object: ",$Player::previousObject);
%oldMaterial.setFieldValue(glow, 1);
%oldMaterial.setFieldValue(emissive, 1);
%oldMaterial.reload();
$Player::previousObject = %this.useTarget;
}
}
}

//echo("Usable?? ", %this.useTarget.getFieldValue("usable") == true);
//if(%this.useTarget != 0&&%this.useTarget.getName()$="testKey")
if(%this.useTarget != 0 && %this.useTarget.getFieldValue("usable") == true)
//if(%this.useTarget != 0 )
{

Reticle.setVisible(false);
Hand.setVisible(true);
return %this.useTarget;
}
else
{

Reticle.setVisible(true);
Hand.setVisible(false);
}

}