New Control Behavior: Platformer
by Eric Robinson · in Torque Game Builder · 05/01/2007 (8:19 pm) · 8 replies
I just added a behavior for platformer style games. Find it here.
Like the article says, the code follows the design found in the MiniPlatformerTutorial. As such it is prone to several things:
1) For things to work properly, physics and collision must be set up as in the tutorial.
2) There is "bug" in the code that can sometimes get your character stuck on a wall and can lead to wall scaling (using jump). "Bug" is in quotes because it is unclear as to whether its a script bug or an engine bug. It is present in 1.1.3 and 1.5 Beta 3. One way to solve this would be to look into the collision information returned by the multiple calls to castCollision() and by using the onCollision() callback. Another way would be to use pickLine()/pickRect to determine the orientation of the player with respect to obstacles (note that you must be very careful if you include sloped surfaces in your level design).
(The above bug appears in rare cases where the code doesn't call %this.owner.setLinearVelocityX(0); until after the character is actually colliding with the wall. This then causes subsequent castCollision() calls to return a collision, pushing control flow into the final else-statement in the updateVertical() call, stopping all downward movement.)
Pre-Post [heh] Update:
The following code modification will fix the wall sticking bug with a special stipulation:
* This code was only checked when the character's Collision mode was set to Full or Polygon using a custom fitted 5-point polygon.
The script based fix [hack?] is located in the updateVertical() function. Change:
I recommend testing the value using "echo([playerNameField].castCollision(1));". If you are having trouble timing your calls, you can bind a key to that function or make it a global function in script and schedule() it in the command console. Good stuff!
Mountain Dew at the Vending Machine: $5
Ticket to Spiderman 3: $10
Adding platformer controls to an object in the engine in under 5 seconds: Priceless*
Test and enjoy!
Note:
This fix is not included in the behavior download for two reasons:
1) It is unclear if this bug is due to a bug in the source or an issue with the script (if a collision occurs, is the engine supposed to leave it there or push it just beyond the collision point?).
2) The code in the behavior attempts to remain as close to the code in the tutorial as possible. If the tutorial gets updated with this fix, so then too should the behavior.
* Actual cost depends on your hourly income and the number of hours it would take to implement the controls without Behaviors.
Like the article says, the code follows the design found in the MiniPlatformerTutorial. As such it is prone to several things:
1) For things to work properly, physics and collision must be set up as in the tutorial.
2) There is "bug" in the code that can sometimes get your character stuck on a wall and can lead to wall scaling (using jump). "Bug" is in quotes because it is unclear as to whether its a script bug or an engine bug. It is present in 1.1.3 and 1.5 Beta 3. One way to solve this would be to look into the collision information returned by the multiple calls to castCollision() and by using the onCollision() callback. Another way would be to use pickLine()/pickRect to determine the orientation of the player with respect to obstacles (note that you must be very careful if you include sloped surfaces in your level design).
(The above bug appears in rare cases where the code doesn't call %this.owner.setLinearVelocityX(0); until after the character is actually colliding with the wall. This then causes subsequent castCollision() calls to return a collision, pushing control flow into the final else-statement in the updateVertical() call, stopping all downward movement.)
Pre-Post [heh] Update:
The following code modification will fix the wall sticking bug with a special stipulation:
* This code was only checked when the character's Collision mode was set to Full or Polygon using a custom fitted 5-point polygon.
The script based fix [hack?] is located in the updateVertical() function. Change:
else if(%yVelocity < 0 && %this.airborne)
{
%this.setConstantForceY(100);
}toelse if((%yVelocity < 0 || mAbs(getWord(%collision, 5)) < 0.05) && %this.airborne)
{
%this.setConstantForceY(100);
}The behavior version would look like this:else if((%yVelocity < 0 || mAbs(getWord(%collision, 5)) < 0.05) && %this.owner.airborne)
{
%this.owner.setConstantForceY(100);
}This uses the y-component of the collision normal returned by the castCollision() call to determine where the collision occurred. If you have only 100% vertical and horizontal surfaces, you can change "< 0.05" to "== 0". The more you increase that number (ie "< 0.10"), the more you limit the incline that the player can jump onto (adding functionality for running along inclines may require changes to the updateHorizontal() function, depending on your implementation). Keep in mind that you must play-test this value. The code above was checked using a custom polygon. Further testing with the default, square-polygon revealed a few inconsistencies with "< 0.05". Note that if you have any sloped surfaces, you may need to define your own non-square collision polygon to achieve the desired effect.I recommend testing the value using "echo([playerNameField].castCollision(1));". If you are having trouble timing your calls, you can bind a key to that function or make it a global function in script and schedule() it in the command console. Good stuff!
Mountain Dew at the Vending Machine: $5
Ticket to Spiderman 3: $10
Adding platformer controls to an object in the engine in under 5 seconds: Priceless*
Test and enjoy!
Note:
This fix is not included in the behavior download for two reasons:
1) It is unclear if this bug is due to a bug in the source or an issue with the script (if a collision occurs, is the engine supposed to leave it there or push it just beyond the collision point?).
2) The code in the behavior attempts to remain as close to the code in the tutorial as possible. If the tutorial gets updated with this fix, so then too should the behavior.
* Actual cost depends on your hourly income and the number of hours it would take to implement the controls without Behaviors.
#2
Though... I could look into making that value a variable that you can play with within the editor for the Behavior. That would actually be really interesting. Unfortunately I won't be able to do the behavior the justice it deserves for another week or so (day job got busy for a bit).
Ideally you would enter in a "Maximum walkable angle" or some-such and that would determine what platforms you could walk along. This is complicated, however, as the current fix implementation relies solely upon the interaction of collision polygons... and custom polygons can affect the collision normals...
Hopefully you can see where things get a little challenging. Heh.
05/14/2007 (8:48 pm)
The problem is that the "fix" only works for the special case of all walls being completely vertical. I mention in the explanation above that you can change a value (the 0.05 in the if-statement) to change the way the interaction occurs. It's an understanding thing. For the most part, everything in the code is straightforward and easily understandable. That one piece requires some explanation as it's a special case. This could be added to the Mini Tutorial but even that would require quite a bit of explanation and seem inelegant.Though... I could look into making that value a variable that you can play with within the editor for the Behavior. That would actually be really interesting. Unfortunately I won't be able to do the behavior the justice it deserves for another week or so (day job got busy for a bit).
Ideally you would enter in a "Maximum walkable angle" or some-such and that would determine what platforms you could walk along. This is complicated, however, as the current fix implementation relies solely upon the interaction of collision polygons... and custom polygons can affect the collision normals...
Hopefully you can see where things get a little challenging. Heh.
#3
This might solve all most all the problem.
I have updated the TDN tutorial with a TGB based solution. Together these might work perfectly well.
06/25/2007 (8:55 am)
Thanks for the script.This might solve all most all the problem.
I have updated the TDN tutorial with a TGB based solution. Together these might work perfectly well.
#4
06/25/2007 (7:48 pm)
To be honest I did the main testing for the Behavior without any tilemaps. I used t2dSceneObjects (HUGE) for the platforms and tested by rotating them. Very efficient that way, I guess. Hopefully things will work for you using the behavior and hopefully they'll work even better when 1.5 is launched. Here's to Garage Games hopefully fixing some collision bugs before final release!
#5
even I hope so.
But what I have read, the tutorials will be almost useless as a lot of scripting will change with 1.5.
I hope I can catch it even faster then before.
06/25/2007 (11:43 pm)
Ya Ericeven I hope so.
But what I have read, the tutorials will be almost useless as a lot of scripting will change with 1.5.
I hope I can catch it even faster then before.
#6
I just have one little problem --and this is new to me since I had to take over a year off from developing my game. (wedding + post-graduate degree = a busy year!)
When I tried to test my small simple level, I got the following error:
"Game Startup Error
'initializeProject' function could not be found. This could indicate a bad or corrupt common directory for your game.
The game will now shutdown because it cannot properly function"
If anyone can let me know what I need to do, I'd appreciate it. The game itself is just a simple test, mostly to see if my guy can jump without sticking to the walls. I'll continue to look at the same game on TGB v1.1.3 and see if I can figure out what I'm missing. It's probably something very basic... Like I said, it's been a while since I used this software.
Which reminds me...
Great work, GG!! The behavior playground was a very cool way to introduce the features.
06/26/2007 (9:01 am)
Eric, thanks for the control behavior!I just have one little problem --and this is new to me since I had to take over a year off from developing my game. (wedding + post-graduate degree = a busy year!)
When I tried to test my small simple level, I got the following error:
"Game Startup Error
'initializeProject' function could not be found. This could indicate a bad or corrupt common directory for your game.
The game will now shutdown because it cannot properly function"
If anyone can let me know what I need to do, I'd appreciate it. The game itself is just a simple test, mostly to see if my guy can jump without sticking to the walls. I'll continue to look at the same game on TGB v1.1.3 and see if I can figure out what I'm missing. It's probably something very basic... Like I said, it's been a while since I used this software.
Which reminds me...
Great work, GG!! The behavior playground was a very cool way to introduce the features.
#7
The miniplatformer tutorial one from TDN?
this control behaviour needs to be updated in that tutorials player script.
Check you have copied the script properly or post your script here so that we can help you out.
06/26/2007 (9:28 am)
John which script are you using ?The miniplatformer tutorial one from TDN?
this control behaviour needs to be updated in that tutorials player script.
Check you have copied the script properly or post your script here so that we can help you out.
#8
(1) I should have executed another script, such as a player.cs
(2) I should give my playable character a class and/or superclass
It's 1 am Hong Kong time, so I'm going to try this solution in the morning --or any other solution suggested by someone who knows what he's doing or what I'm not doing. (Forgive the 'he' --my wife is pregnant and my child's gender is currently unknown, so saying 'he or she' has gotten old. Though in retrospect, if I just typed '(s)he' in the first place, I would've saved myself lots of typing.)
Sunny, thanks for checking out the script. I simply downloaded this, and change the names of the animation. (PlayerRun to PlayerWalk, since I plan to add a run button later.)
***
EDIT- I deleted the code, since it just makes this thread too long and there are no errors in the code.
I figured out the problem --it was because I saved the file in the "My Documents" folder. Silly me! I can start my game now... but I appreciate the effort to help.
06/26/2007 (10:06 am)
I used the Platformer Controls Behavior script (pasted below), and executed that in the game.cs file. My current guess is that I forgot to do the following:(1) I should have executed another script, such as a player.cs
(2) I should give my playable character a class and/or superclass
It's 1 am Hong Kong time, so I'm going to try this solution in the morning --or any other solution suggested by someone who knows what he's doing or what I'm not doing. (Forgive the 'he' --my wife is pregnant and my child's gender is currently unknown, so saying 'he or she' has gotten old. Though in retrospect, if I just typed '(s)he' in the first place, I would've saved myself lots of typing.)
Sunny, thanks for checking out the script. I simply downloaded this, and change the names of the animation. (PlayerRun to PlayerWalk, since I plan to add a run button later.)
***
EDIT- I deleted the code, since it just makes this thread too long and there are no errors in the code.
I figured out the problem --it was because I saved the file in the "My Documents" folder. Silly me! I can start my game now... but I appreciate the effort to help.
Torque 3D Owner Phil Carlisle
Good to see more people posting behaviors.