I made my first JigLib Demo! Check it out.
If you don't know what JigLib is yet, you are really missing out. Its a great project I'm proud to be the on the team of. It is a 3D physics engine (currently only for Papervision3D, but has been ported to Sandy/Away). It is a port from the JigLib engine done in C. For more information on the project, check out the blog.
I don't really want to give a bottoms up rundown on how jiglib works, so I'm just going to point out a few things that will help you pull off effects like you see in this demo - both from the jiglib point of view, but also from a general pv3d and programming angle.
The Environment
This environment is actually very simple. I have a bitmap for the floor, a lamp model, and a bitmap for the cubes. I dynamically tint the cube materials to give it a little more depth and variety. One thing you will notice is that each cube, and each side of each cube, gets its own material. If you are wondering why, I did this to allow for the bullet holes to be drawn directly onto the material. I use a movie material so that it is easy to just add a bitmap into with a specific blend mode, and then forget it.
The environment for JigLib is very similar. Each cube is simply a "skin" for a class that represents a cube in JigLib. This class is called "JBox". You can think of a JBox as simply the "form" of a cube - it has position and rotation, which are transfered to the object that it is skinning. This it the general way that JigLib works. There are unique JigLib objects which use DisplayObject3D's in Papervision as a skin. All transforms done to the JigLib physics object is then reflected on the DisplayObject3D. JigLib has physics objects for Planes, Cubes (Box) and Spheres. It also includes logic to generate a car with wheels, using a special wheel class and an extension of the JBox class.
So, each cube is controlled by a JBox - and the floor is a JPlane for them to bounce on. There were some hit detection problems I was having with the plane, so I also added a JCube underneath the plane to give extra "floor" for the objects to run into. This is a hack fix, but it seems to help. Something else you can play with to help collision is the timing used by the physics engine. A lower speed will mean that things will move slower, and detect more collisions more accurately.
The Gun Shot
The gun shot is made up of a couple different parts. The gun itself actually "shoots" where it is looking - which is always 2000 in front of the barrel. The only time this is changed is when a hit is detected on a cube. When the mouse clicks on a cube, the target is changed to the exact coordinates of the hit.
The Hit
The actual hit is determined by the mouse click. Take a look at the following code:
-
var hitPoint : Number3D = new Number3D(event.renderHitData.x, event.renderHitData.y, event.renderHitData.z);
-
-
var force:Number3D = new Number3D(hitPoint.x-camera.x, hitPoint.y-camera.y, hitPoint.z-camera.z);
-
force.normalize();
-
force.multiplyEq(6280);
-
var rb:RigidBody = skins[event.displayObject3D];
-
rb.AddWorldForce(new JNumber3D(force.x, force.y, force.z), new JNumber3D(hitPoint.x, hitPoint.y, hitPoint.z));
hitPoint records where the mouse was clicked in world space. Remember, this is only called when a cube is clicked. We then get the difference between that point and the camera, to get our "force" vector. To make it a constant strength, we simply normalize it (make it a length of 1), then multiply it by the force we want. We get our RigidBody from the dictionary I store the references in. Then, we use the function "AddWorldForce" - which takes two JNumber3Ds - the force vector, and the world hit point. This function will add the force to the object from world space - which saves us having to convert things into local coordinates.
The Bullet Trail
The bullet trail is a seperate class called LineOut. I called it LineOut since it fades itself out every frame. Once its invisible, it removes itself from the scene. To get the start and end points of the Line between the gun, I pass in two objects - the target that is positioned at the hit point, and the the muzzle flash that is positioned over the gun barrel. Before we can use the muzzle flash position, however, we need to update its world coordinates:
startPoint.world.calculateMultiply((startPoint.parent as DisplayObject3D).world, startPoint.transform);
Once the world matrix is updated with the current transform, we can use the world matrix to define the start point of the line. I use a segmented line because it looks better - but feel free to play with how many vertices are actually used.
The Bullet Hole
Since we used a seperate bitmap for each side of the cubes, we can alter them individually. This has more memory overhead, but I think it more than makes up for it by having bullet holes. The bullet hole position is determined, again, by the renderHitData that is passed when a cube is clicked. The U and V properties of the renderHitData tell us the "x" and "y" coordinates of the click on the material. Once we have that, we can simply add a bullet hole bitmap to our movie, and position it correctly:
-
var mat:MovieMaterial = event.renderHitData.material as MovieMaterial;
-
var hole : Bitmap = Sprite(mat.movie).addChild(new bulletAsset()) as Bitmap;
-
hole.blendMode = "overlay";
-
hole.x = event.renderHitData.u-hole.width*0.5;
-
hole.y = event.renderHitData.v-hole.height*0.5;
-
-
mat.drawBitmap();
We take half the width and height to make sure that the bullet hole is properly centered on the hit point. One other thing to keep in mind is to define the clipping rect for the movie material when you initially make it. If you don't, your texture will become skewed if bullet holes go off the edge of the material, changing the size of the texture.
Those are the main parts of the demo - hopefully some things are helpful. I was hoping to show some fun uses of JigLib - and hopefully spawn some cool ideas in the process.
and

41 Comments, Comment or Ping
andy, you did it again! now im not gonna get any sleep this weekend either! LOL! awesome demo, and thanx as usual for sharing the sources!
March 5th, 2009
Niceone!
Next up: breakables (compound objects that break in aprts when hit with enough force) and more importantly physics on the lamp :p
March 5th, 2009
Ok, that is totally Rock’In!!! nicely done! Now, I need wrap up my project, so I can make one too!
March 5th, 2009
You are an inspiration – and you make me almost want to give up. Your work is always awesome
March 5th, 2009
Wow… I have been looking at JigLib, really glad to hear your on the team. You always do push flash to its limits. Wow was really exciting when it first came out but it has not really progressed. Any chance we will get rigid body dynamics ^_^
Great work!
March 5th, 2009
Never heard of the JigLib till now, another interesting lib for sure. Great demo, and it runs fairly smoothly which is always a good thing.
March 5th, 2009
Very cool demo! I’m going to have to look into JigLib.
March 5th, 2009
This is by the far the most advanced 3D / Physics / Flash Game combined into one I’ve ever seen. Amazing, this is going to be big!
March 5th, 2009
Amazing job Andy, hat off a new designer has born
March 5th, 2009
Excellent example and more importantly, excellent explanation of the code/library!
March 6th, 2009
you are such an inspiration.
March 6th, 2009
This so awesome but the website for jiglib seems to be down, I tried yesterday, wasn’t working either
March 8th, 2009
I love that the light turns off when you shoot it!
Great work.
March 9th, 2009
Hi great work !
I am also working on this library and i think that i will be able to show something in 1 or 2weeks , but i have just a question about some properties of material :
planePhysic.Material.Restitution =
planePhysic.Material.StaticFriction =
planePhysic.Material.DynamicFriction =
Someone knows the meaning of these properties ? i have tried to change it to understand , and its ok for restitution but nothing for the 2 other, and what are the links with the mass of an other object who strike it ?
tks
March 10th, 2009
GREAT WORK!
March 10th, 2009
Great work!!
March 12th, 2009
You rock Andy, always insightful.
keep up the incredible work!
March 20th, 2009
thats awesome, but i think flash is not yet ready for 3d physic, is just too slow
March 24th, 2009
that’s really cool. thanks for sharing..
March 27th, 2009
Did you rip the stuff from Counter-Strike? Awesome work as usual, thanks for sharing your code.
April 19th, 2009
Complete with shootable light, that wow factor of early FPS, you legend!!!
April 22nd, 2009
Thanks for this great tutorial. Although the main idea was to use Jiglib physics, i played with your tutorial to create a simple shooter game.
http://www.onlineindiangames.com/livegames/SkullCrusher3D/
April 27th, 2009
Absolutely great!
Thanks for the experience.
Cheers!
/Petit
May 3rd, 2009
You can’t sent all files the project, included assets.swc & .dae files?
Thanks!
Sergio
May 6th, 2009
A error ocurred in many lines, ie: jBox[i] = new JBox(pv3dBox, true, size, size, size);
help!
thanks!
S!
May 6th, 2009
Andy,
What versions of PV3D and JigLib are you using? I’m attempting to get this to compile and I’m getting object coercion errors, which I think are related to a change in the API. Many thanks–
Fran
July 18th, 2009
can you post the dae files and .swc files
September 2nd, 2009
Hi, this is my new game based on papervision + jiglib. Its helicopter simulation http://www.littlebigplay.com/game/548
November 26th, 2009
You rule.
July 7th, 2010
excellent
now for halflife 2 in fash…
October 9th, 2010
After I watched this blog, i figured you might be the right person to help me with this. I’m currently working on a project very similiar to this (click explore to see):
http://www.youtube.com/adwords10
I already have the sphere with the material, but i can’t figure out how to do the mouse events, or to put those markers.
Thank you, and awesome work!
January 19th, 2011
Dude this is awesome in every way.
February 16th, 2011
Reply to “JigLib – Bust a Cap In Papervision3D”