Game Development Community

TGB 1.5 Platformer - Player Oscillating

by Phillip O'Shea · in Torque Game Builder · 07/03/2007 (4:09 am) · 9 replies

I am trying to create a solid platformer base for a potential game idea. I have worked through the MiniPlatformer and AdvancedPlatformer tutorials and I think I have a grasp of the concepts.

I wanted to impliment acceleration and deceleration upon movement, so I though the best way to do this would be using the onUpdateScene function (used in MiniPlatfomer too).

function t2dSceneGraph::onUpdateScene() {
	if (isObject($actor))
		$actor.updateMovement();
}

function ActorClass::onLevelLoaded(%this, %scenegraph)
{
	$actor = %this;

	%this.Accel_X = 0.03;
	%this.Decel_X = 0.02;

	%this.Velocity_X = 0;
	%this.Velocicy_Y = 0;

	%this.MaxVelocity_X = 20;
	%this.MaxVelocity_Y = 60;

	%this.onGround = false;
      
	moveMap.bindCmd(keyboard, "left", "moveLeft();", "stopLeft();");
	moveMap.bindCmd(keyboard, "right", "moveRight();", "stopRight();");
	moveMap.bindCmd(keyboard, "space", "moveJump();", "");
}

function ActorClass::updateMovement(%this)
{
	//Actual Updates
	%this.updateHorizontal();
}

function ActorClass::updateHorizontal(%this)
{
	//MoveLeft
	if (%this.moveLeft) {
		%dir = -1;
		%this.Velocity_X -= %this.Accel_X;
	}
	//MoveRight
	else if (%this.moveRight)
	{
		%dir = 1;
		%this.Velocity_X += %this.Accel_X;
	}
	//Decelerate
	else
	{
		%dir = -1;

		if (%this.Velocity_X > 0)
		{
			%dir = 1;
		}

		if (mAbs(%this.Velocity_X) > mAbs(%this.Decel_X))
		{
			%this.Velocity_X -= %dir*%this.Decel_X;
		}
		else
		{
			%this.Velocity_X = 0;
		}
	}

	//Make sure we aren't going above the max velocity
	if (mAbs(%this.Velocity_X) > mAbs(%this.MaxVelocity_X))
	{
		%this.Velocity_X = %dir*%this.MaxVelocity_X;
	}

	//Update velocity
	%this.setLinearVelocityX(%this.Velocity_X);
}

function moveLeft()
{
	$actor.moveLeft = true;
	$actor.moveRight = false;
}

function moveRight()
{
	$actor.moveLeft = false;
	$actor.moveRight = true;
}

function stopLeft()
{
	$actor.moveLeft = false;
}

function stopRight()
{
	$actor.moveRight = false;
}

The problem I have is that when I move into a solid wall, the actor bounces backwards a few points, then tries to move back to the wall, then bounces and so forth.

So, I removed all of the above, changed it to when the key is pressed, set velocity (just a constant) and he will bounce off the wall. The faster he is traveling, the further he will bounce. (I have set collision to CLAMP, not bounce =P).

Sooooo, are there any potential fixes for this? Am I going about my objectives the wrong way?

Any help is appreciated!

About the author

Head of Violent Tulip, a small independent software development company working in Wollongong, Australia. Go to http://www.violent-tulip.com/ to see our latest offerings.


#1
07/03/2007 (5:54 pm)
As far as I know, to accomplish platformer 'physics' and 'movement' you have to basically 'roll your own' collision detection and in some cases, your own physics code ... not relying on the code that ships with TGB at all (outside of the scope of triggering a custom 'onCollision' callback, that is).

But in short ... the most likely reason for 'bouncing' is because of a funky physics reaction between the players collision bounds and the walls collision bounds ... which means one of them is sending physics and one of them is recieving them ... and ... a wall should neither send or recieve physics in a platformer ... it should simple report a collision and let the developer handle the collision (move the player backwards so they don't "stick" in the wall, for example -- or possibly cause the wall damage, if your gameplay includes such things).


I believe one of the things that most people trying to develop a platformer in TGB are experiencing is ... the problems they face when they try to use a Physics engine which wasn't designed to do what they want ... Platformers don't have realistic physics reactions ... so the built-in TGB physics system is 'out the door' ...


Again, in short ... my personal opinion on the matter of a Platformer in TGB is ... it won't happen until someone writes up a custom collision/physics system to handle the specifics of platformers ... rather then the current ones, which are all hacks and work-arounds to get the built-in physics to do the job ... yes, using forces to push the player up and down to simulate jumping are ok ... but these should be applied manually on the player object and none of the objects should send or recieve physics ... just collisions ... and all the collisions should be custom scripted on a per-object-type basis (platforms for standing, player and enemies, interactable objects, etc)

TGB 1.5's Behaviour system is a great way to simplify this, cause you can easily focus on one aspect and make it concrete ... and then focus on another ... and make it concrete ... and just simply apply the behaviours you need to the objects that need them ... making sure that the player's "Platform Player Controller" bevahiour (or whatever you want to call it, hah) is aware of all the other possible behaviours and works with them ...



Whew ... I think that was a mouth full ... and it probably didn't even answer the question ... haha


#2
07/03/2007 (10:53 pm)
Hi David,

Thanks for the response, though it was the one I was fearing!

I was reading a thread about this sort of thing and one of the Torque X Platformer programmers said that they were able to change the physics of Torque X to suit what they needed.

So, I should uncheck Send and Receive Physics and just use callBack? Or have an opUpdateScene -> updatePhysics route?

Thanks mate,
Phil
#3
07/03/2007 (11:52 pm)
@Philip
Set a collision callback with your walls, in it you can tell the player to stop cleanly depending of the angle of the collision. You don't have to take out physics, it is useful, but you have to use collision callbacks to tweak things to your liking.
#4
03/05/2008 (9:03 pm)
Now that I'm reading this. Is there any way I can make a platform game the old fashion way by avoiding the Torque Game Builders physic engine all together and using the old fasion pixel by pixel collison like in programs like Flash and Blitz Basic.
#5
03/05/2008 (9:11 pm)
TGB doesn't support pixel based collisions.
#6
04/07/2008 (8:39 pm)
Ok even after a year this problem still persists.

Maybe using TXB platformer kit was a better solution.

Alas it doesnt work on my pc, unless thats been fixed. Have to check that.
#7
04/08/2008 (9:43 am)
There's a platformer kit for TGB now as well. Have you had a look at it Wicked?
#8
04/08/2008 (11:49 am)
Nope

Thanks Mike, I will give it a look.
#9
05/01/2008 (12:30 pm)
In case you haven't seen it, definitely check out the video of Melv's latest physics fixes, and how it helps basic platformers. Looks like simple and solid platformer games are finally going to be possible using TGB.