SetLinearVelocityY not working
by Bryce Bangerter · in Torque Game Builder · 04/03/2006 (6:26 pm) · 5 replies
Using the latest beta version of T2D I have the following code
function t2dSceneObject::onCollision (%srcObj, %dstObj, %srcRef, %dstRef, %time,
%normal, %contactCount, %contacts)
{
if ($player == %srcObj)
{
DeflectObject (%dstObj, $player.getPositionX ());
StunPlayer (%dstObj.type.stun);
}
if (%dstObj == $player)
{
DeflectObject (%srcObj, $player.getPositionX ());
StunPlayer (%srcObj.type.stun);
}
else if (%srcObj.moving && %dstObj.moving) // drop to drop collision
{ // average the speed of the two drops
DropCollision (%srcObj, %dstObj);
}
}
In my DeflectObject function I have
function DeflectObject (%obj, %deflectX)
{
if (%obj.getPositionX () == %deflectX)
{
// random deflection direction
%deflectX = getRandom (0, 1) * 60;
}
%obj.setLinearVelocityY (-0.7 * %obj.type.weight);
if (%obj.getPositionX () < %deflectX) // deflect to the left
{
%obj.setLinearVelocityX (-0.7 * %obj.type.weight);
}
else // deflect to the right
{
%obj.setLinearVelocityX (0.7 * %obj.type.weight);
}
}
My X velocity is appropriately applied, but the Y velocity is not (the object has an initial setLinearVelocityY (10) when it is created along with a max and a constant force: i.e.
%drop.setLinearVelocityY (%drop.type.weight);
%drop.setMaxLinearVelocity (%drop.type.weight);
%drop.setConstantForce ("0" SPC %drop.type.weight, true);
If I remove the constant force the object travels across the screen horizontally (instead of diagonally up).
Is this a bug in the beta code or am I doing something wrong?
function t2dSceneObject::onCollision (%srcObj, %dstObj, %srcRef, %dstRef, %time,
%normal, %contactCount, %contacts)
{
if ($player == %srcObj)
{
DeflectObject (%dstObj, $player.getPositionX ());
StunPlayer (%dstObj.type.stun);
}
if (%dstObj == $player)
{
DeflectObject (%srcObj, $player.getPositionX ());
StunPlayer (%srcObj.type.stun);
}
else if (%srcObj.moving && %dstObj.moving) // drop to drop collision
{ // average the speed of the two drops
DropCollision (%srcObj, %dstObj);
}
}
In my DeflectObject function I have
function DeflectObject (%obj, %deflectX)
{
if (%obj.getPositionX () == %deflectX)
{
// random deflection direction
%deflectX = getRandom (0, 1) * 60;
}
%obj.setLinearVelocityY (-0.7 * %obj.type.weight);
if (%obj.getPositionX () < %deflectX) // deflect to the left
{
%obj.setLinearVelocityX (-0.7 * %obj.type.weight);
}
else // deflect to the right
{
%obj.setLinearVelocityX (0.7 * %obj.type.weight);
}
}
My X velocity is appropriately applied, but the Y velocity is not (the object has an initial setLinearVelocityY (10) when it is created along with a max and a constant force: i.e.
%drop.setLinearVelocityY (%drop.type.weight);
%drop.setMaxLinearVelocity (%drop.type.weight);
%drop.setConstantForce ("0" SPC %drop.type.weight, true);
If I remove the constant force the object travels across the screen horizontally (instead of diagonally up).
Is this a bug in the beta code or am I doing something wrong?
#2
Drop: 2266
Drop.type.weight: 10
Drop: 2267
Drop.type.weight: 10
Drop: 2268
Drop.type.weight: 10
So the object exists
I also added similar code in the DeflectObject function and get
Obj: 2266
Obj.type.weight: 15
Obj.Position: 34.000000 42.105099
Obj.Velocity: -7.000000 -7.000000
the drops have
%drop.setCollisionResponse (CLAMP);
when does the clamp actually go into effect? Before t2dSceneObject::onCollision or after?
If before then I'm still stuck with why no y velocity change. If after then why does my change to velocity x take effect?
Is there a way to remove all collision repsonse without doing custom? (I'd rather not dig into the C++ code just yet to customize the response).
04/03/2006 (7:02 pm)
I get an output similar toDrop: 2266
Drop.type.weight: 10
Drop: 2267
Drop.type.weight: 10
Drop: 2268
Drop.type.weight: 10
So the object exists
I also added similar code in the DeflectObject function and get
Obj: 2266
Obj.type.weight: 15
Obj.Position: 34.000000 42.105099
Obj.Velocity: -7.000000 -7.000000
the drops have
%drop.setCollisionResponse (CLAMP);
when does the clamp actually go into effect? Before t2dSceneObject::onCollision or after?
If before then I'm still stuck with why no y velocity change. If after then why does my change to velocity x take effect?
Is there a way to remove all collision repsonse without doing custom? (I'd rather not dig into the C++ code just yet to customize the response).
#3
04/04/2006 (12:20 am)
Use %drop.setCollisionPhysics(false,false); to turn off physics.
#4
04/04/2006 (9:30 pm)
Thanks Michael. Still not sure why my Y velocity wasn't changing with the physics turned on, but turning them off solved some other minor problems I was having as well.
#5
04/05/2006 (12:58 am)
I guess, it is exactly what you suspected earlier: That the physics response takes effect after the collision callback is fired. Haven't looked this up in the source code though.
Torque Owner Chris Labombard
Premium Preferred
echo("Drop: " @ %drop); echo("Drop.type.weight: " @ %drop.type.weight);That will let you know if the object exists and if the weight is correct.