REQ] When A collides with B, B gets force added
by David Janik-Jones · in Torque Game Builder · 08/09/2007 (10:02 am) · 5 replies
The reference image for this request is at http://www.janikjones.com/pic1.jpg
I have a simple test in the trial. I have two objects, A and B on screen. Each have a small circular collision area.
A is being dragged around by the mouse.
When A is dragged to strike B, I need to apply a force to B to move it away from the collision but slow it's movement down over time. The force applied should equate to the speed at which A was moving. B will eventually slow down and stop due to friction.
Like moving a billiard ball around on a table with your hand and striking the other balls on the table.
Can someone show me how to do this, pretty please? This helps me evaluate TGB.
Thanks.
I have a simple test in the trial. I have two objects, A and B on screen. Each have a small circular collision area.
A is being dragged around by the mouse.
When A is dragged to strike B, I need to apply a force to B to move it away from the collision but slow it's movement down over time. The force applied should equate to the speed at which A was moving. B will eventually slow down and stop due to friction.
Like moving a billiard ball around on a table with your hand and striking the other balls on the table.
Can someone show me how to do this, pretty please? This helps me evaluate TGB.
Thanks.
#2
//-----------------------------------------------------------------------------
// Hit another object and move it like a puck script
// Thanks to Melissa
//-----------------------------------------------------------------------------
function HitAndMove::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
%this.owner.enableUpdateCallback();
// get the XY velocity of object A (%dstObj.getLinearVelocity);
// Transfer this velocity to object B (%this.owner.setLinearVelocity);
}
function HitAndMove::onUpdate(%this)
{
// gradually decrease linear velocity on object B (%this.owner);
// if object B
// velocity = 0, disable onUpdate callback;
}
08/09/2007 (12:02 pm)
Thanks Melissa, but Torque's odd scripting (I use JavaScript in Unity) throws me and makes it hard to evaluate ... this is what I came up with ...//-----------------------------------------------------------------------------
// Hit another object and move it like a puck script
// Thanks to Melissa
//-----------------------------------------------------------------------------
function HitAndMove::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
%this.owner.enableUpdateCallback();
// get the XY velocity of object A (%dstObj.getLinearVelocity);
// Transfer this velocity to object B (%this.owner.setLinearVelocity);
}
function HitAndMove::onUpdate(%this)
{
// gradually decrease linear velocity on object B (%this.owner);
// if object B
// velocity = 0, disable onUpdate callback;
}
#3
I forgot to mention that the parameters that are passed into the onCollision() are really handy--%dstObj is the object that was collided with.
You'll also want something like this at the beginning of your script, since this is a behavior:
08/09/2007 (12:09 pm)
Hey, this is looking pretty good. So do you need help using the getLinearVelocity(),setLinearVelocity() methods, if statements, and the like? :)I forgot to mention that the parameters that are passed into the onCollision() are really handy--%dstObj is the object that was collided with.
You'll also want something like this at the beginning of your script, since this is a behavior:
if (!isObject(HitAndMove))
{
%template = new BehaviorTemplate(HitAndMove);
%template.friendlyName = "Hit and Move Behavior";
%template.behaviorType = "Game";
%template.description = "Some sort of description of what your behavior does";
}
#4
I added the behaviour statement at the front as you mentioned but have no idea how to format the commented out stuff. I tend to learn best by looking at simple examples and breaking them down to understand their syntax, formatting and the like, then working into the documents and building towards intermediate stuff. I'm still even that way with the JavaScript coding I do for Unity projects ... I have to ask for help or team up with a person whose focus is programming to get certain stuff done.
So if you would be kind enough to fill in the blanks this time, that'd be very much appreciated.
08/09/2007 (2:02 pm)
Yes please. As you've said, as a newbie simply trying to evaluate TGB I need help with ... using the getLinearVelocity(),setLinearVelocity() methods, if statements, and the like.I added the behaviour statement at the front as you mentioned but have no idea how to format the commented out stuff. I tend to learn best by looking at simple examples and breaking them down to understand their syntax, formatting and the like, then working into the documents and building towards intermediate stuff. I'm still even that way with the JavaScript coding I do for Unity projects ... I have to ask for help or team up with a person whose focus is programming to get certain stuff done.
So if you would be kind enough to fill in the blanks this time, that'd be very much appreciated.
#5
This code will probably not do exactly what you want it to. What you probably want to do is somehow use the velocity of object A to set the acceleration. You would do that like this:
There should be a documentation folder that came with TGB too--a good reference guide full of all the methods you can use on, say, a 2d scene object. Really handy. Also, there are tutorials that include scripting samples.
EDIT: edited onCollision after looking up setImpulseForcePolar, that should work somewhat but I haven't tried it in a game sooooo we'll just see :P
08/09/2007 (2:58 pm)
Mmk. I spent some time on this. It turns out there are setDamping() and setImpulsePolar(), which are two really handy methods for this...damping will slow it to a stop, and setImpulsePolar will get the thing moving :Dif (!isObject(HitAndMove))
{
%template = new BehaviorTemplate(HitAndMove);
%template.friendlyName = "Hit and Move Behavior";
%template.behaviorType = "Game";
%template.description = "Some sort of description of what your behavior does";
%template.addBehaviorField(acceleration, "acceleration", int, 10);
%template.addBehaviorField(damping, "damping", float, 1.0);
}
function HitAndMove::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
%this.owner.setDamping(%this.damping);
%x = getWord(%normal, 0); //get the direction of the collision
%y = getWord(%normal, 1);
%this.owner.setImpulseForcePolar((%x * %this.acceleration), (%y * %this.acceleration));
}This code will probably not do exactly what you want it to. What you probably want to do is somehow use the velocity of object A to set the acceleration. You would do that like this:
%temp = %dstObj.getLinearVelocity();The problem is that I don't think there's a way to get the speed of how fast you're moving something if you're using the mouse instead of, say, the keyboard controls.
There should be a documentation folder that came with TGB too--a good reference guide full of all the methods you can use on, say, a 2d scene object. Really handy. Also, there are tutorials that include scripting samples.
EDIT: edited onCollision after looking up setImpulseForcePolar, that should work somewhat but I haven't tried it in a game sooooo we'll just see :P
Torque Owner Melissa Niiya
- Create a new behavior for the B object.
- Add a BehaviorName::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) callback method
- In this method, get the X and Y velocity of object A (%dstObj.getLinearVelocity)
- Transfer this velocity to object B (%this.owner.setLinearVelocity)
- setTimer() for every few ticks or use onUpdate() to gradually decrease linear velocity on object B (%this.owner), when velocity = 0, disable onUpdate callback
I imagine this idea would take tweaking to get it looking properly physicsy though...