Game Development Community

Can anyone tell me how to do this please..

by March · in Technical Issues · 07/06/2008 (11:45 pm) · 3 replies

When I get hit by something I want my player bouncing back a little bit. I tried to use myplayer.settransform
but my player go thru the wall even .dif object. Can anyone help me how to do something like this please.

Thank you

#1
07/07/2008 (3:51 am)
In your player.cs's ::onCollision() method, add this to the beginning:

%pushBackForce = VectorLen(%obj.getVelocity()) / 2;
%contactPosition = %pos;
%worldBoxCenter = %obj.getWorldBoxCenter();
%vecToWorldBox = VectorSub(%contactPosition,%worldBoxCenter);
%vecNormalized = VectorNormalize(%vecToWorldBox);
%pushBackVector = VectorScale(%vecNormalized,%pushBackForce);
// Send the player (%obj) back
%obj.setVelocity(%pushBackVector);

That should (haven't tested it yet) bounce the player back at half the speed he was moving when he hit the object. SetTransform() changes position and rotation, so that's why it's taking you through walls. I use setVelocity() here, which alters the player's speed and direction, so you won't go through walls.
#2
07/07/2008 (2:04 pm)
Thank you so much but I'm a little bit confused. you mean I have to use setVelocity to bounce my player instead of setTransform() ? and what is the %pos refer to ? this is my test script but I put in AI.cs

function fireWheel::onCollision(%this,%obj)
{
echo("och");
%pos = %col.getPosition
.....
.....
myPlayer.setTransform(%pos);
}

I've never use %pos in onCollision before Can you tell me about onCollision please.

Thank you
#3
07/07/2008 (3:03 pm)
This is my licensed account..

Finally , I got it with this script. Thank you so much for helping me

function Demoplayer::onCollision(%this,%obj,%col,%fade,%pos,%normal)
{
// Set the object's position and initial velocity
%impulseVec = VectorSub(%col.getWorldBoxCenter(), %pos);
%impulseVec = VectorNormalize(%impulseVec);
%impulseVec = VectorScale(%impulseVec, 700);
%pos = %col.getPosition();
%col.applyImpulse(%pos,%impulsevec);
}