Game Development Community

Wierd behavior with SceneObject.SetPosition()

by Clint Herron · in Torque Game Builder · 09/12/2006 (6:13 am) · 5 replies

Hey all!

I'm somewhat new to Torque Game Builder, having only seriously picked it up and starting to use it last week. I'm running into some strange behavior with my scene objects, and I was hoping that someone could give me some advice.

I'm creating a very simple game where the enemies aren't intelligent, but rather just bounce around in the playing area. I have done this by giving them x/y velocity and setting them to bounce on world limit collisions. I next wanted to have a function that would randomly place them in the world boundaries -- so I wrote the following function:

function evilClass::jumpToRandomLoc(%this)
{
	$lastEvil = %this;
	
	// Get the world limits
	%limits = %this.getWorldLimit();

	// Debug echo to help me figure out what's wrong
	echo ("Limits are" SPC %limits);
	
	// Sort out the left, right, top and bottom edges
	
	// NOTE: Documentation says that the order returned from getWorldLimit() is MinX, MaxX, MinY, MaxY, but the order in fact seems to be MaxX, MinX, MaxY, MinY
	%minX = getWord(%limits, 2) + %this.getSizeX() / 2;
	%maxX = getWord(%limits, 1) - %this.getSizeX() / 2;
	%minY = getWord(%limits, 4) + %this.getSizeY() / 2;
	%maxY = getWord(%limits, 3) - %this.getSizeY() / 2;
	
	// Pick a random location within the world limits and assign the position
	%randX = getRandom(%minX, %maxX);
	%randY = getRandom(%minY, %maxY);
	
	
	// %randX = -6; // I tried setting to these valid world limit values, but the bug still occurs.
	// %randY = 118;
	
	%this.setPosition(%randX, %randY);
	
	// Another debug print to help me figure out why it's not working
	echo ("World limits of" SPC %minX SPC %maxX SPC %minY SPC %maxY SPC "and we warped to" SPC %randX SPC %randY SPC "which is actually" SPC %this.getPositionX() SPC %this.getPositionY());
}

I don't think it should matter, but I'm using a particle emitter for my enemy. You can see a picture of it
here. It's the evil red glowy thing.

All of the debug information looks fine, but when I actually warp the enemy object, it makes him disappear and he is no longer inside his world boundaries. Here is my console.log:

Boom! We hit the player!
Limits are BOUNCE 75.000000 -50.000000 222.315994 92.334999 false
World limits of -34 59 108.335 206.316 and we warped to -6 118 which is actually -6 118

All looks fine, right? Except that the enemy doesn't stay at (-6, 118) for very long (maybe one frame). I then try to look at his position:
==>echo($lastEvil.getPosition());
-4907.852051 38.100407

-4907.852051??? And if I keep making calls to that echo(), I can see him shuffle around a little bit -- he's still moving, but just wiggling. It's really wierd that he seems to have warped okay, but then all of the sudden he's just way out in left field and he won't come back.

Does anyone have a clue for me? I'm pretty lost. It seems like a bug, though I'm sorry I can't pin it down more precisely.

I can zip this up and submit it as a demo project if that would be helpful. Beyond this function though, there's really not that much to it.

Thanks for reading!

--clint

#1
09/12/2006 (11:49 am)
Looking carefully at your debug output, you're warp to position is -not- within your world limits, but is (of course) within your slightly misleading second debug output (which isn't your true World Limits, but is your randomly generated range for the world limits).

How are you initially setting your world limits? I'm suspecting this may be your issue, and the object is getting outside of them with a linear velocity, so it just keeps going, and going, and going...

PS: I double checked the underlying code, and the world limits are in fact processed in the order of (MinX MinY) (MaxX MaxY), but your self-documentation lists them incorrectly--both in what you expected, and what you intuitively mapped. Basically, world limits are defined as two points: the MinPoint (left/top), and the Max Point (right/bottom), with 0, 0 as the center of your world box.

Which version of the docs did you pull your initial expectation from? I checked TDN and it lists them correctly, but don't have the most recent version of the .pdf in my office at the moment, so there may be a documentation error in the pdf.
#2
09/12/2006 (12:12 pm)
Hey Stephen! Thanks for the reply!

Quote:Looking carefully at your debug output, you're warp to position is -not- within your world limits


AAAH! You are right! Silly me for not following the documentation. I was pulling the world limits out in the wrong order. That fixed that problem.

Though now I'm having the *very* strange side-effect of it moving my player whenever I collide with the enemy -- it moves my player towards the enemy in almost a gravitic sense -- very strange stuff happens when they get overlapping.

Thanks again!

--clint
#3
09/12/2006 (12:17 pm)
Would be a little bit of refactoring for you, but I would highly suggest turning your "enemy" into a scene object, and then mounting your particle effect to that scene object--would give you much more flexibility regarding how to handle collisions and such--and particles, while able to have collisions, can be abit hard on your performance.

Theoretically it wouldn't change much, since all the collision you are dealing with is down at the scene object level anyway (which particle effect objects inherit from), but it's the expected implementation practice, and it's possible that there may be subtle issues/bugs with particle effect to sprite collision.

PS: I did want to comment on your debug output and internal self-documentation--great practices! Glad to see :)
#4
09/12/2006 (12:26 pm)
Okay -- I've already got a scene object (an image that is my light map
that casts extra shadow around the enemy) that actually is doing the
collision with a simple circle, and that is mounted to the particle
effect which has the physics that move it around. I could switch stuff
around and and make the shadow have both the physics and the collision
code (as opposed to just the collision code the way it is now). You're
right -- that certainly might simplify my life.

Do you have any idea as to why colliding with my enemy object could
move me *towards* it? It's really quite trippy.

Thanks for the compliment about my debugging and commenting. :)

--clint
#5
09/12/2006 (12:42 pm)
Been trying to puzzle over that latest, but honestly I don't know :( Sounds like a possible conflict in your movement model, but without basically having your entire project (and the time to really look at it), it's hard to tell.

You might want to possibly post up any (script) collision callbacks you are using--on your enemy class and player class?

Do you have any prototyped ai code for your player "seeking" the bad guy or anything?