platform physics demo
Shri

All,

I didn't see anything about how to use the new physics stuff with a platformer. So, I put together a little demo. If you go to my site www.ashatej.com, it is at the bottom of the demos section (physics robot demo). The robot moves left, right and jumps using the left,right and up arrow keys. It can also push around some objects. I tested it on a PC with firefox and chorme and it seems to work as advertised.

The demo uses box2d joints which aren't yet integrated into the wade physics extension, but are easy to add. Basically a joint in box2d is used to tie multiple physics bodies together, (think of your elbow joint which links together your humerus and radius/ulna). The robot jumps using a prismatic joint (a piston) also, the hightest platform is attached to an anchor with a distance joint which gives it a little bouncy effect.  The movement isn't perfect and could use some tweeking, but should be good enough for demonstration purposes.

The whole demo is available for download as a zip file. The graphics were released under CC0 and obtained from opengameart.org or are from here. There is some standard copyright stuff in the source code, but feel free to do with it what you like.  I don't use the online editor, so you have to know a little code to understand what is going on. With a little work, you could easily modify this to create your own plaftormer with different graphics assets (and imporve the motion).

If you have any questions regarding the demo, post them to this thread and I will try to get you an answer.

If I get some requests, then I can put together a blog post breaking down how I did this in simpler steps.

happy new year - cheers - shri

All 3 Comments
cartyman

Thank you sir, I am going to have a good look at the source code.

Can I ask, what do you have to do to add joints to wade? Are they going to be integrated with wade eventually?

Gio

Nice one Shri :)

To answer the question above, yes, it's going to be either in WADE 3.6 or WADE 3.7. You'll be able to add and edit joints from the editor. But depending on how things go with our other projects, this could be a couple of months away or more. So if you need it any time soon do look into Shri's solution. I looked briefly at the code and it looks very easy to understand.

Shri

cartyman

If you download and unpack the zip file, you will notice there are two files PrsmaticJoint.js and DistanceJoint.js. These are the two joint objects. Then, at the end of the global init routine in game.js, there are these two lines

wade.physics.distanceJoint = new DistanceJoint();
wade.physics.prismaticJoint = new PrismaticJoint();

Now, you have two joint object in the wade.physics namespace. If you look into gameWorld.js in the createRobot function, you should see these lines

var jointOptions =  {
	localAxisA: {x:0, y:-1}, upperTranslation: 40, lowerTranslation: 0,
	enableLimit: true, enableMotor: false, maxMotorForce: 600, motorSpeed: 200
};
		
player.joint = wade.app.createPrismaticJoint(box2,player,jointOptions);

This creates the prismatic joint, which joins the body of the robot to another body that will act as a piston (psismatic joint). These lines in the robo.js file

wade.physics.prismaticJoint.setMotorEnabled(bot.owner.joint,true);

if (wade.physics.prismaticJoint.getJointTranslation(self.owner.joint)>=maxJump) {
	wade.physics.prismaticJoint.setMotorEnabled(self.owner.joint,false);
}

force the piston down (jump) and then let it return back up on landing (so you can jump again). If you want to see the two physics joints, then set the sprites of  boxData1 and boxData2 in the createRobot function to visible. You will see a white oval over the robot and a box at the bottom of it. These are the two components of the physics joints. They box will move down on a jump and return to the original position on landing.

Look into the DistanceJoint.js and Prismatic.js files.In the comments, you should be able to see what the joint options settings do and what the various functions that manipulate the joints do. If you want to dig into box2d joints further, check out these sites https://docs.coronalabs.com/guide/physics/index.html   and http://www.iforce2d.net/b2dtut/  They should give you some good info.

If you get stuck on something, post it here and I will try to answer.

cheers - shri

Post a reply
Add Attachment
Submit Reply
Login to Reply