Game Development Community

dev|Pro Game Development Curriculum

Climb Ladders, Trees, Rocks, Walls with TGE 1.5

by Afrim Kacaj · 11/13/2006 (4:42 pm) · 66 comments

Download Code File

This is my first resource so please forgive me in advance. I have been holding on to this code for some time now, hoping to make it better before posting it as a resource. Now that I had to merge it my self to TGE 1.5 I had the opportunity to clean it up a bit. However it still lacks one important feature, you can only enter the climbing state if you are facing the object. Once you get to the top and jump off, it's hard to get back on to climb down (unless you make your ladder longer than needed.)


To implement, merge the attached files with your project. You will need a climbing animation, with vertical transform. Same way you do the running animation with ground transform, you need to do the climbing animation with vertical transform. This will allow the animation speed to change with velocity.
Call the animation anything you want as long as it has the word climb in it and add it to your player.


You will also need an object to climb, you can use any DTS shape. Make sure your DTS has a collision box and as tight to the object as possible. Make the appropriate changes to the ClimbableObjects.cs script file.

i.e change shapefile = "~/data/shapes/ladders/ladder.dts"; to shapefile = "~/data/shapes/YOURFOLDER/YOUROBJECT.dts";

Optional: Add the following code to server\scripts\player.cs after the Armor::onLeaveLiquid function.

function Armor::onStartClimb(%this, %obj, %ClimbObject)
{
   //Dismount the weapon here
   %obj.lastWeapon = %obj.getMountedImage($WeaponSlot);
   %obj.unmountImage($WeaponSlot);
   %obj.setArmThread("look");
}
function Armor::onEndClimb(%this, %obj)
{
 //Remount the previous weapon here
 %obj.mountImage(%obj.lastWeapon, $WeaponSlot);
}

Go into the mission editor and place your shape in the mission, make sure that it is in an angle less than 90 otherwise it will not work.

About the author

Recent Blogs

Page «Previous 1 2 3 4 Last »
#1
10/30/2006 (12:25 pm)
Looks fantastic, thanks!
#2
10/30/2006 (5:15 pm)
Cool, thanks Afrim
#3
10/31/2006 (12:32 am)
Afrim, we would love to put this in the MMOKIT, what a great resource!
#4
10/31/2006 (2:04 am)
This is a great resource, I have been trying to do the same thing myself but with litte success.

Nice work.
#5
10/31/2006 (6:36 am)
OMG! I have been looking for this for a long time!!

Congrats! Great resource!
#6
10/31/2006 (3:16 pm)
Just a few quick questions,

1) Has anyone got climbing animations for the default character? Just would save a lot of time in testing.

2) Is it possible to have several different climbing objects with different climbing animation? E.g. the climbing animations will be different when climbing a ladder to climbing a rope. What would need to be modified to do this?
#7
11/02/2006 (7:21 pm)
Thank you everybody, I am glad that you can make use of it. This does need more work and I will post my updates as I work on it. There are two issues that I would like to address soon. One is that the collision happens on the players collision box, so if your player has a large box he will be offset from the object depending on the size of the players collision box as well as the ladders collision box. The other issue that I would like to address is player rotation. This is not such a big deal if your ladders is standing at almost 90 degrees, but if you tilt your ladder say 45 degrees, the player would look like he is walking on it while the climb animation is playing. Once issue number two is solved, this would allow you to get creative and even have your player climb steep hills instead of walking on them, crawl through grass fields etc. 

By the way, notice I said almost 90 degrees, because the climbing is treated as walking in code. The engine has trouble walking on a 90 degree surface.

Some of you might even want to stop the player from rotating while on the ladder, that can easily be done in the updatemove function. Instead of completely stopping the rotation, I would slow it down. That way if they rotate too much, they will fall off the ladder. Reason they fall off the ladder is because once they rotate enough, the raycast to the ladder returns false. Then mClimbing is set to false, and runSurfaceAngle (which you set in script in the player datablock) kicks in and gravity wins.

FYI, the runSurfaceAngle defines the maximum angle that the player can walk on, hence the above result.

Here is the code that I use in the updatemove function of player.cc to achieve that result.
//Afrim: commented this out and replaced with code below
    //  F32 y = move->yaw;
    //  if (y > M_PI) y -= M_2PI;
      F32 y;
	  //Afrim: I want to slow down or stop the player rotation while climbing
      if (mClimbing | mArmThreadPlayOnce)
      {
         //The divisor will increase or decrease the turning rate...
         y = move->yaw/18.84f;
         if (y > M_PI) 
			 y -= M_2PI;
	   }

     else
      {
         y = move->yaw;
         if (y > M_PI) y -= M_2PI;
      }
#8
11/02/2006 (7:33 pm)
Levi, I have a custom character so no climb animation otherwise I would have uploaded with the resource.

As far as being able to play different animations for different objects, you can easily do that in two ways.

The easy way:
This might even be the prefered way since it would not require to recompile ever time you want a new animation type. Also gives the artist more freedom.
I am not giving you the complete answer here just the idea.
In the pickactionanimation function, instead of setting the climb animation. You can write code to bail out of that function if mClimbing is true. Very important not to pick any animations here.

Then in script, I provided two events:
Armor::onStartClimb(%this, %obj, %ClimbObject) and Armor::onEndClimb(%this, %obj)

%ClimbObject tells you which object you are climbing and you can set your action animation in script accordingly instead.

The reason I said that you want to make sure that you bail out of the pickactionanimation function if mClimbing is true is because otherwise the engine will override your script animation once the player starts moving.

The hard way:
I like the easy way so much that im not even going to tell you the hard way. But in short, you would just add more action animations to the player class and then pick them in setactionthread based on what object the player is climbing.
#9
11/19/2006 (7:31 pm)
Great resource!
Thanks so much for this. This is really what I've been wanting.
#10
11/27/2006 (11:33 pm)
Thanks


Very COOL!!!
#11
12/29/2006 (5:31 am)
Thanks!
I was trying this out and I have the character now climbing up and down but I have one question, is it supposed to climb up/down automatically or only when you press up/down buttons from the keyboard?

Because now it climbs up if I'm at ground level and back down when touching the ladders from the upper end.

PS. Should I make the vertical animation really long (long offset) so that even the highest ladders could be climbed, or have I understand something wrong?
#12
12/29/2006 (1:02 pm)
Thank you Afrim, This will be a good start point to change my player skills...
#13
12/29/2006 (9:49 pm)
Jari, the character is supposed to move up and down only when you press the keyboard keys. Not sure what you did that its going up and down on its own. Also, the animation is a loop so no need to make it long.
#14
01/01/2007 (11:18 am)
Afrim, ok thanks for clearing that out, I just need to learn how to setup the animation properly.
#15
01/25/2007 (2:09 pm)
@Afrim - FYI. If you look at StaticShape::onAdd() you'll see how the 'dynamicType' is added to the mTypeMask. Since sgAllowedDynamicTypes is defined as 0xffffff... your ClimableItemObjectType is masked out and not applied. sgAllowedDynamicTypes at the top of staticShape.cpp needs to be changed to:
static const U32 sgAllowedDynamicTypes = 0xffffff | ClimableItemObjectType;
This fixes your ClimableItemObjectType and you can remove the...
if( objectMask & (ClimableItemObjectType|StaticShapeObjectType))
... tests which allow any StaticShapeObjectType to be climbable.
#16
01/25/2007 (2:12 pm)
Another issue. Isn't this...

*run  = bestVd > mDataBlock->runSurfaceCos | mClimbing;  //Afrim:  make sure we can climb climable objects
... really supposed to be this...
*run  = bestVd > mDataBlock->runSurfaceCos || mClimbing;  //Afrim:  make sure we can climb climable objects

?
#17
01/25/2007 (6:06 pm)
Good catch Tom, I did not notice that sgAllowedDynamicTypes = 0xffffff only has 6 f's and therefore 24 bit.

After making that change in staticshape.cc, if( objectMask & (ClimableItemObjectType|StaticShapeObjectType)) in player.cc should now be if( objectMask & ClimableItemObjectType).

You are also right on the second post, that should be a boolean OR, not a bitwise. Sorry my coding got a little rusty.

I want to go through the code one more time and then I will update the resource files with the above changes.

Thanks Tom!
#18
03/20/2007 (4:24 pm)
Afrim and everyone! Can anyone send me some example code with this feature working? I've recompiled torque, used the sample files, but my player can't climb any ladders. :(

Can anyone help me? It's sort of urgent...

Best regards,

JM
(jmxntg@gmail.com)
#19
03/20/2007 (6:38 pm)
Hi John, the sample code is attached. Follow the instructions carefully it will work. Something you might have overlooked is tilting your climable object. You will not be able to climb an object that is perpendicular to the surface you are standing on, the angle must be less than 90. Is it that your animatin is not playing or is your player not moving up at all?
#20
03/20/2007 (7:19 pm)
Hi Afrim, I've download the code attached but I couldn't get the job done for my player. The .dts object is not perpendicular to the surface, but still I cant climb it. Don't you have a full sample with source, so this way I can see what I'm doing wrong?

Best regards,

JM
Page «Previous 1 2 3 4 Last »