Game Development Community

OnWorldLimit fired more than once?

by David House · in Torque Game Builder · 03/01/2005 (10:39 am) · 15 replies

I'm getting like 5 - 7 calls of onWorldLimit when I should only get 1 I think. Here is the code that sets the world limit for my fruit:

%fruit.setWorldLimit( sticky, %left - 6 @ " -45 " @ %left + 6 @ " " @ %bottom, true );

%bottom is where I want the fruit to stop. And it does stop exactly on the bottom part of the limit. I turned debug on to see the world limit rectangle. So no problems there.

But when the onWorldLimit is called, it appears to happen more than once. I would assume that with the sticky parameter, the sprite will stop moving and call onWorldLimit just once.

Maybe I'm doing something wrong here, I'm not sure. I'm having a wierd problem with my script code right now, and this was the only wierd thing that I have noticed so far. I'm going to keep looking, but just thought I'd ask about this anyway to know why it does that.

Still working on my fruity game. Had almost no time to work on it sun or monday. Hope to finish today if I can get all the bugs worked out of the matching code! ACK!

#1
03/01/2005 (10:41 am)
Is the sprite playing an animation?

I think the call will be made on a per frame basis.
#2
03/01/2005 (11:21 am)
@David - might be easier using SPC instead of @, then you don't have to worry about adding trailing and leading spaces to your literal strings
#3
03/01/2005 (11:35 am)
No, the sprite doesn't have animation.

And yeah, I agree about the SPC stuff. Still kinda new to Torque Scripting so I'm probably doing alot of things the hard way.
#4
03/01/2005 (12:23 pm)
@David: The world-limit will nullify the linear velocity of the object, it won't touch any angular velocity so watch out for spinning objects. The rigid-body response deals with angular velocity though.

Make sure you've not got any constant forces acting on the objects otherwise they'll simply continue colliding with their limits again and again.

- Melv.
#5
03/01/2005 (12:48 pm)
Quote:The world-limit will nullify the linear velocity of the object, it won't touch any angular velocity so watch out for spinning objects.
AH, this explains some issues with multiple collisions I was having, too. Thanks for pointing that out!
#6
03/01/2005 (1:35 pm)
@Melv, all I'm doing is using .setLinearVelocityY() to a positive value so that the fruit fall downwards. I'm not doing any angular velocity since spinning fruit is just too disturbing... :)

And I should also point out that I'm getting "bottom" as the limit type on the onWorldLimit calls.

If I get a moment, I'll try to reproduce this in a separate project to make sure I'm not doing anything wierd.
#7
03/01/2005 (2:07 pm)
@Melv, its not just in my code, it happens using the sample as well. I went back to the base T2D code, and added the spaceship like in the example. Here is the code:

function setupT2DScene()
{
	// Create fxSceneGraph2D.
	new fxSceneGraph2D(t2dSceneGraph);
	
	// Associate Scenegraph with Window.
	sceneWindow2D.setSceneGraph( t2dSceneGraph );
	
	// Set Camera Position to be centered on (0,0) with
	// view width/height of (100/80).
	sceneWindow2D.setCurrentCameraPosition( "0 0 100 75" );

	
	// ************************************************************************
	//
	// Add your custom code here...
	//
	// ************************************************************************
	datablock fxImageMapDatablock2D(playershipImageMap)
	{
		mode = full;
		textureName = "~/client/images/playerShip";
	};

	$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	$player.setPosition("0 0");
	$player.setSize( "14 7" );
	$player.setImageMap( playershipImageMap );
	$player.setWorldLimit( sticky, "-20 -20 20 20", true );
	$player.setLinearVelocityY( 10 );
	$player.setDebugOn( BIT(4) );
}

function fxSceneObject2D::onWorldLimit( %srcObj, %limitMode, %limit )
{
	echo( "onWorldLimit " SPC %limitMode SPC %limit );
}

This generates the following messages in the log:

onWorldLimit STICKY bottom
onWorldLimit STICKY bottom
onWorldLimit STICKY bottom
onWorldLimit STICKY bottom
onWorldLimit STICKY bottom
onWorldLimit STICKY bottom
onWorldLimit STICKY bottom
#8
03/01/2005 (2:33 pm)
Hmmm....

Maybe there's a problem with the STICKY mode. I'll add it to the list.

Thanks for the Heads-up. :)

- Melv.
#9
03/08/2005 (7:17 am)
FYI

I have fixed this problem. It will be included in the next update.

As a temporary fix, you can set your objects relaxation to 1. When an object collides with a world-limit, any overlap into the world-limit is resolved by the physics system but it was using the objects relaxation parameter which relaxes the overlap solution and so removes the overlap over time. Reducing relaxation results in longer to remove the overlap. Setting relaxation to 1 results in an instant solution. This is correct behaviour of the physics system but is not appropriate for the collision with the world-limits so a special-case for the physics has been added which will always solve overlaps with the world-limits instantly.

Please note that if your object is rotating, the system will solve the world-limit collision but if the object continues to rotate, dependant upon its collision-polygon, it may continue to collide with the world-limit. Eventually, the object will not collide with the world-limit when it is distanced by at least its maximum radii from the point of rotation. Setting a relaxation higher than one will amplify the seperation due to overlaps and can be used to always stay a discreet distance, relative to the objects' size, away from any overlap collision.

Thanks for the report.

- Melv.
#10
03/08/2005 (8:58 am)
Here's the code I've been using for my bouncing ball test:
function fxSceneObject2D::onWorldLimit(%this,%mode,%limit)
{
	%spd = mAbs($player.getLinearVelocityX()) + mAbs($player.getLinearVelocityY());
	echo ("SPD=",%spd,"  LIMIT=",%limit);
	if (%spd > 10)   alxPlay( bounceAudio );
	if ( (%spd < 1) && (%limit $= "bottom") )   $player.setImmovable();
}
The variable %spd is just a rough indicator of the object's speed.
If it's moving faster than 10, I play the bounce sound.
If it's moving slower than 1 AND it's on the bottom, I set it to immovable to disable it's interaction with the world limit.

Now you can have bouncing, spinning, dancing fruit that won't send the world limit collision system into overload :-)

Note: The sound isn't quite right when the object is "scooting" across the bottom, but for three lines of code, I'm not gonna complain :-)

Edit: Thanks for the tip, Matthew!

According to Melv, $player.setRelaxation( 1 ) has the same effect in this setting as $player.setImmovable(). I'm going to try switching them, because I'd rather leave the object ready for additional physics reactions than to have to reset the density when I want to use the object again.
#11
03/08/2005 (9:11 am)
@Terry: just a friendly tip... when posting code on the board, wrap it in these tags
"[ code]"
"[/ code]"

(without the space inside)

so for example

function testFunction(%var)
{
      echo(%var);
}

:)
#12
03/08/2005 (9:27 am)
Err, well you don't need to do any of that at all, just do this on your object (until the update)...
%obj.setRelaxation( 1 );
.. and you'll only get a single call from the world-limit, assuming it's not rotating. You can then proceed to play sounds, stop the object etc.

Anyway, this has now been resolved. Expect it in the next update. :)

- Melv.
#13
03/08/2005 (3:23 pm)
Thanks Melv! You rock man.
#14
03/08/2005 (4:58 pm)
%obj.setRelaxation(1); didn't work for me, but my object is rotating, and has a constant gravitational force applied to it, so it probably shouldn't have :-)

I am having WAY too much fun with this stuff! I probably need to start thinking of an actual idea for a game right about now, according to the screams of my "inner boss" :-)
#15
03/09/2005 (1:14 am)
@Terry: Yes, as I said, rotating objects, unless they're circular, will collide multiple times, dependant upon their shape. This is normal behaviour though. :)

I really recommend setting relaxation to 1 as it will affect collisions with other objects causing them to jumping away from collisions.

Definately in the next update.

- Melv.