Shape and Object goes into different directions
by Bauer Ren · in Torque Game Engine · 06/20/2006 (4:10 pm) · 2 replies
The goal is to extend StaticShape, so that is possible to give StaticShape a direction (optional) and the Instance will move in a special Direction.
Therefore:
1. I added an new member and two methods
// staticShape.h
// move direction
Point3F moveDirection;
void StaticShape::setMoveDirection(const Point3F& vel);
Point3F StaticShape::getMoveDirection() const;
2. I added some new code for init and the concrete method
// staticShape.cc
// a
StaticShape::StaticShape()
{
mTypeMask |= StaticShapeObjectType | StaticObjectType;
mDataBlock = 0;
// ---------------------------
// set Direction
// ---------------------------
// ***
moveDirection.x=0.1f;
moveDirection.y=0.0f;
moveDirection.z=0.0f;
}
// b - change in ProcessTickount
void StaticShape::processTick(const Move* move)
{
Parent::processTick(move);
// update
// translate it ..
MatrixF mat = getTransform();
// simple translation
Point3F p;
mat.getColumn(3,&p);
p.x=p.x+moveDirection.x;
p.y=p.y+moveDirection.y;
p.z=p.z+moveDirection.z;
mat.setColumn(3,p);
Parent::setTransform(mat);
Parent::setRenderTransform(mat);
[....]
3. added the method to change this and console method
//----------------------------------------------------------------------------
// ****
void StaticShape::setMoveDirection(const VectorF& vel)
{
moveDirection = vel;
}
Point3F StaticShape::getMoveDirection() const
{
return moveDirection;
}
ConsoleMethod( StaticShape, setMoveDirection, void, 5, 5, "(float x, float y, float z)")
{
if(object->isClientObject())
return;
VectorF vel(dAtof(argv[2]), dAtof(argv[3]), dAtof(argv[4]));
object->setMoveDirection(vel);
}
ConsoleMethod( StaticShape, getMoveDirection, const char *, 2, 2, "()"
"returns moveDirection")
{
char* ret = Con::getReturnBuffer(256);
if (object->isServerObject())
dSprintf(ret, 255, "%g %g %g",
object->moveDirection.x,
object->moveDirection.y,
object->moveDirection.z);
else
dStrcpy(ret, "0 0 0");
return ret;
}
4. Problem
The problem is, that the object starts correctly and drive into "1 0 0" direction. but if i make in the console
// object 1315 is an instance of staticShape
1315.setMoveDirection(-1,0,0);
then the object goes on in the direction "1 0 0" and the pivot point of the objects makes the direction "-1 0 0". if i use 1315.getMoveDirection(), i get "-1 0 0".
so what is wrong in my code? how can i update the object constantly ?
thanks for any help and thanks for reply
Rene Bauer
Therefore:
1. I added an new member and two methods
// staticShape.h
// move direction
Point3F moveDirection;
void StaticShape::setMoveDirection(const Point3F& vel);
Point3F StaticShape::getMoveDirection() const;
2. I added some new code for init and the concrete method
// staticShape.cc
// a
StaticShape::StaticShape()
{
mTypeMask |= StaticShapeObjectType | StaticObjectType;
mDataBlock = 0;
// ---------------------------
// set Direction
// ---------------------------
// ***
moveDirection.x=0.1f;
moveDirection.y=0.0f;
moveDirection.z=0.0f;
}
// b - change in ProcessTickount
void StaticShape::processTick(const Move* move)
{
Parent::processTick(move);
// update
// translate it ..
MatrixF mat = getTransform();
// simple translation
Point3F p;
mat.getColumn(3,&p);
p.x=p.x+moveDirection.x;
p.y=p.y+moveDirection.y;
p.z=p.z+moveDirection.z;
mat.setColumn(3,p);
Parent::setTransform(mat);
Parent::setRenderTransform(mat);
[....]
3. added the method to change this and console method
//----------------------------------------------------------------------------
// ****
void StaticShape::setMoveDirection(const VectorF& vel)
{
moveDirection = vel;
}
Point3F StaticShape::getMoveDirection() const
{
return moveDirection;
}
ConsoleMethod( StaticShape, setMoveDirection, void, 5, 5, "(float x, float y, float z)")
{
if(object->isClientObject())
return;
VectorF vel(dAtof(argv[2]), dAtof(argv[3]), dAtof(argv[4]));
object->setMoveDirection(vel);
}
ConsoleMethod( StaticShape, getMoveDirection, const char *, 2, 2, "()"
"returns moveDirection")
{
char* ret = Con::getReturnBuffer(256);
if (object->isServerObject())
dSprintf(ret, 255, "%g %g %g",
object->moveDirection.x,
object->moveDirection.y,
object->moveDirection.z);
else
dStrcpy(ret, "0 0 0");
return ret;
}
4. Problem
The problem is, that the object starts correctly and drive into "1 0 0" direction. but if i make in the console
// object 1315 is an instance of staticShape
1315.setMoveDirection(-1,0,0);
then the object goes on in the direction "1 0 0" and the pivot point of the objects makes the direction "-1 0 0". if i use 1315.getMoveDirection(), i get "-1 0 0".
so what is wrong in my code? how can i update the object constantly ?
thanks for any help and thanks for reply
Rene Bauer
About the author
Torque Owner Griffin Milsap
When you change a variable on the object, torque automatically changes it on the server side, because thats where all the magic happens to prevent cheating. If you havn't modified packUpdate and unpackUpdate to include your variables, the variable is changed on the server object, but not the client object which is the one actually rendered.
Find those functions and add your variables into the stream in packUpdate, and at the same place in the unpackUpdate, add your read function. There are plenty of examples in the code, so I'll leave that to you.
Just make sure they are packed and unpacked in the same order, or things get messed up.
-Griff