PDA

View Full Version : verlet ragdoll



slideth3
September 15th, 2006, 05:05 PM
hey everyone, been playing round with my verlet physics engine ( http://www.kitegallery.net/misc/Slideth/Sam/verlet.swf.html ) and decided to make a ragdoll, after all what is a physics engine without a ragdoll, lol, anyway the results are quite cool.

http://www.kitegallery.net/misc/Slideth/Sam/living+man.swf.html

I really want the ragdoll to have some kind of ai as well (balancing, standing up...). If anyone wants to play with the source code feel free.

Also is there any way to do actionscript 3 on a mac using a real program, would really like to uppgrade due to the speed issue.

Cheers Sam

edit: can't attach fla for some reason! :(

i'll post up the code tomorrow

matthewjumps
September 16th, 2006, 07:54 AM
:player: hey that is quite cool...this kind of stuff always seems so daunting to me...how do you get a grounding in hardcore AS stuff like physics and 3d? Where do you even start?

mnkeymasta
September 16th, 2006, 01:48 PM
try and get something like the ragdoll mastars. I luv that game : )
http://www.ragdollsoft.com (http://www.ragdollsoft.com/)

mathew.er
September 16th, 2006, 02:01 PM
:player: hey that is quite cool...this kind of stuff always seems so daunting to me...how do you get a grounding in hardcore AS stuff like physics and 3d? Where do you even start?
With the guy called Google, I guess. ;)

Pretty nice thing. I never realy explored this area deeply, but will check out the code for sure... Just few questions... Does verlet means something different, than e.g. inverse or forward kinematics? And if so, were the "muscles" (the lines showed when you pressed the button) just links between nodes or were they something more?

evildrummer
September 16th, 2006, 05:05 PM
thats some pretty nice coding, could we see the code pretty please? :D

EDIT// Didnt read the first post properly about code being posted tomorow, damn me.

matthewjumps
September 17th, 2006, 02:34 AM
yea im looking forward to seeing the code too...tho i think i will look at it and be very confused....

matthew.er - yea google :)

slideth3
September 17th, 2006, 06:47 AM
With the guy called Google, I guess. ;)

Pretty nice thing. I never realy explored this area deeply, but will check out the code for sure... Just few questions... Does verlet means something different, than e.g. inverse or forward kinematics? And if so, were the "muscles" (the lines showed when you pressed the button) just links between nodes or were they something more?

The principles behind verlet integration are fairly simple. Normally to send an object moving you have a position, and update it by a velocity every frame. In verlet integration all you have is the position of the object at the moment, and the position it was in in a previous time step.

Now you can see that from this the velocity is simply the current position minus the old position. This means pretty much whatever force you apply to the object will be accurately carried out. It is a method used in describing molecular dynamics. Jacobsen wrote a really good article about its implementation in the hitman game series.

As for the constraints between particles, this is simply a matter of moving both particles by a factor of the distance towards the required distance from each other.... If the factor is high, say 0.5 the particles will both move half way of the distance away from required and the constraint will be met.... If on the other hand the factor is lower, like 0.05, the particles will only move some of the way, depending on the distance, and follow hookes law and be elastically attracted towards each other.

This is the basis behind my muscle and constraint code, simply have 2 particles and constrain them by distance r eg. constrain(particle1, particle2, r)

The code shows this fairly clearly, I'll post it up when I'm on my computer..

Cheers

Sam

ps. gonna give the ragdoll masters idea a go :D

edit:

and heres the code....



//these are just the rendering controls, using the tickbox, turn them on and off how you want
muscles = false;
skeleton = false;
outline = true;
//this turns wave muscles on or off
wave = false;

//verlet integration 2d

//how many points you have on the stage; points should have instances ball1, ball2, ball3 etc.
point_no = 17;

//this creates an array containing x, y, oldx, and oldy posiitions for each point
points = new Array();
for(i=1;i<point_no+1;i++){
points[i] = {x:_root["ball"+i]._x, y:_root["ball"+i]._y, x2:_root["ball"+i]._x, y2:_root["ball"+i]._y};
}

//iterations is how many times calculations are repeated per frame, should be >4 for best results but depends on what kind of
//object it is
iterations = 4;

//universal gravity
gravity = 0.4

//on enter frame call each function
function onEnterFrame(){
//clear drawing after frame
_root.clear();
// carry out main functions;
verlet();
render();
constraints();
ai();
keys();
}

//the verlet function pretty much moves the points by the difference of the previous two points
verlet = function(){
//for each particle
for(i=1;i<point_no+1;i++){
//store initial point
tempx = points[i].x
tempy = points[i].y
//verlet integration
points[i].x += points[i].x - points[i].x2
points[i].y += points[i].y - points[i].y2 + gravity
//store previous point
points[i].x2 = tempx;
points[i].y2 = tempy;
}
}

//this controls where each particle is allowed to go
constraints = function(){
//for each particle carry out distance and boundary functions
for(it=0;it<iterations;it++){
distance();
boundaries();
}
}
//this function connects each particle/point with either a solid line (constrain) or a spring (muscle);
distance = function(){
//torso
muscle(1, 2, 68);
constrain(2, 3, 60);
//chest triangle
constrain(4, 7, 65);
constrain(4, 3, 95);
constrain(7, 3, 95);
//hip triangle
constrain(10, 13, 65);
constrain(10, 2, 95);
constrain(13, 2, 95);
//waist muscles;
muscle(4, 10, 100);
muscle(7, 13, 100);
//neck muscles;
muscle(4, 1, 70);
muscle(7, 1, 70);
muscle(3, 1, 128);
muscle(13, 1, 200);
muscle(10, 1, 200);
//left arm;
constrain(4, 5, 60);
constrain(5, 6, 50);
muscle(1, 5, 130);
muscle(4, 6, 110);
//right arm;
constrain(7, 8, 60);
constrain(8, 9, 50);
muscle(1, 8, 130);
muscle(7, 9, 110);
//left leg;
constrain(10, 11, 85);
constrain(11, 12, 80);
muscle(10, 12, 165);
muscle(3, 10, 40);
//right leg;
constrain(13, 14, 85);
constrain(14, 15, 80);
muscle(13, 15, 165);
muscle(3, 13, 40);
//leg width;
muscle(12, 15, 90);
muscle(11, 14, 100);
muscle(2, 15, 280);
muscle(2, 12, 280);
//feet;
constrain(12, 16, 30);
constrain(15, 17, 30);
muscle(16, 11, 100);
muscle(17, 14, 100);
muscle(17, 16, 170);
//arms up;, the wave function only occurs on clicks
if(wave == true){
muscle(9, 13, 220);
muscle(6, 10, 220);
muscle(15, 9, 350);
muscle(12, 6, 350);
}
}
//edge of the world and friction
boundaries = function(){
for(i=1;i<point_no+1;i++){
if(points[i].x > 540) (points[i].x = 540)
if(points[i].x < 0) (points[i].x = 0)
if(points[i].y > 400){
//friction depends on the normal force ie penetration depth...
depth = points[i].y-400
points[i].x -= depth*(points[i].x-points[i].x2)/10
points[i].y = 400;
}
//move the balls to the positions
_root["ball"+i]._x = points[i].x
_root["ball"+i]._y = points[i].y
}
//position head movie clip (you'll need one of these on the stage);
head._x = ball1._x
head._y = ball1._y
}
//the constrain function
constrain = function(point, target, distance){
r = distance;
dx = points[point].x-points[target].x
dy = points[point].y-points[target].y
d1 = Math.round(Math.sqrt(dx*dx + dy*dy))
d2 = 0.5*(d1-r)/d1
dx = dx*d2;
dy = dy*d2;
points[target].x += dx; points[target].y += dy;
points[point].x -= dx; points[point].y -= dy
if(skeleton==true){
_root.lineStyle(2, 0xFF0000);
_root.moveTo(points[target].x, points[target].y);
_root.lineTo(points[point].x, points[point].y);
}
}
//the muscle function;
muscle = function(point, target, distance){
r = distance;
dx = points[point].x-points[target].x
dy = points[point].y-points[target].y
d1 = Math.round(Math.sqrt(dx*dx + dy*dy))
d2 = 0.05*(d1-r)/d1
dx = dx*d2;
dy = dy*d2;
points[target].x += dx; points[target].y += dy;
points[point].x -= dx; points[point].y -= dy;
if(muscles==true){
_root.lineStyle(2, 0x0000FF);
_root.moveTo(points[target].x, points[target].y);
_root.lineTo(points[point].x, points[point].y);
}
}
//control with the keys
keys = function(){
for(i=1;i<point_no+1;i++){
if(Key.isDown(Key.RIGHT)) points[i].x += gravity*2
if(Key.isDown(Key.LEFT)) points[i].x -= gravity*2
if(Key.isDown(Key.UP)) points[i].y -= gravity*2
if(Key.isDown(Key.DOWN)) points[i].y += gravity*2
}
}
//render it
render = function(){
if(outline==true){
_root.lineStyle(10, 0x000000, 100);
_root.moveTo(ball1._x, ball1._y);
_root.lineTo((ball4._x+ball7._x)/2, (ball4._y+ball7._y)/2);
_root.moveTo(ball4._x, ball4._y);
_root.beginFill(0x000000, 100);
_root.curveTo((ball2._x+ball3._x)/2, (ball2._y+ball3._y)/2, (ball10._x+ball11._x)/2, (ball10._y+ball11._y)/2);
_root.curveTo((ball10._x+ball13._x)/2, (ball10._y+ball13._y)/2, (ball13._x+ball14._x)/2, (ball13._y+ball14._y)/2);
_root.curveTo((ball2._x+ball3._x)/2, (ball2._y+ball3._y)/2, ball7._x, ball7._y);
_root.lineTo(ball4._x, ball4._y);
_root.endFill();
_root.lineTo(ball5._x, ball5._y);
_root.lineTo(ball6._x, ball6._y);
_root.moveTo(ball7._x, ball7._y);
_root.lineTo(ball8._x, ball8._y);
_root.lineTo(ball9._x, ball9._y);
_root.moveTo((ball10._x+ball11._x)/2, (ball10._y+ball11._y)/2);
_root.lineTo(ball11._x, ball11._y);
_root.lineTo(ball12._x, ball12._y);
_root.lineTo(ball16._x, ball16._y);
_root.moveTo((ball13._x+ball14._x)/2, (ball13._y+ball14._y)/2);
_root.lineTo(ball14._x, ball14._y);
_root.lineTo(ball15._x, ball15._y);
_root.lineTo(ball17._x, ball17._y);
}
}
//control the wave function;
_root.onMouseDown = function(){
wave = true;
}
_root.onMouseUp = function(){
wave = false;
}

slideth3
September 17th, 2006, 01:59 PM
http://www.kitegallery.net/misc/Slideth/Sam/RagdollMasters.swf.html

http://www.kitegallery.net/misc/Slideth/Sam/RagdollMasters2.swf.html

as requested ;)

they run slow as **** though so I got another one to show how many balls have to collide, fair play its so slow to be honest, lol. Now to get it done in actionscript 3!

slideth3
September 17th, 2006, 02:53 PM
Made some improvements, more efficient collisions, head hits and you can actually go right lol

http://www.kitegallery.net/misc/Slideth/Sam/RagdollMasters2_001.swf.html

Its actually working out quite well, imagine a big online tournament, anyone want to make that? :D

Cheers

Sam

matthewjumps
September 17th, 2006, 07:37 PM
:be: that is really fun! i love it!

mnkeymasta
September 18th, 2006, 04:14 PM
Wow thats awesome :thumb:

Imagine a big online tournament. I would pwnzord you all:evil:

I could imagine a skatboard fighting game. o yah that game is no. 48 if anybody wants to know!

slideth3
September 23rd, 2006, 04:06 PM
woah just rewrote the engine in actionscript 3 and been playing around, its so fast for flash! got like 30 particles all colliding with everyone, so thats 30+29+28+27 calculations times like 10 iterations per frame! and it runs really fast, like 20fps or more! got it up to 50 at like 10 fps,

anyway here is a sample.

http://www.kitegallery.net/misc/Slideth/Sam/physics+engine.swf.html

try placing the mouse controlled one under the rope, its quite cool the way the mass is distributed when the other thing bounces on it!

Oh yeah key pressing is a bit wierd in actionscript 3, anyone got a good method of doing


onClipEvent(enterFrame){
if(Key.isDown(39) do function
}


in as3?

at the moment I'm doing something silly which I'm sure is wrong and only registers like one key at a time, and not if you release another! :(



function keylisteners()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP , onKeyRelease);
}

function onKeyPress(event:KeyboardEvent){
key = event.keyCode
}
function onKeyRelease(event:KeyboardEvent){
key = 0
}

function movement(){
keyaffector(14)
keyaffector(15)
keyaffector(16)
}

function keyaffector(target)
{
if(key==37) points[target].x-=grav*2
if(key==38) points[target].y-=grav*2
if(key==39) points[target].x+=grav*2
if(key==40) points[target].y+=grav*2
}

slideth3
October 6th, 2006, 12:41 PM
sorry just had to post this its so cool :D

http://www.kitegallery.net/misc/Slideth/Sam/gish.swf.html

tried a gish style thing, think its capable :)

nafets
October 6th, 2006, 03:35 PM
Nice! ChronicLogic (the makers of gish) actually also made a game called "bridgeBuilder". In that game, the springs are made of steel and are thus less flexible. I tried to implement this "stiff" behaviour, but it didn't work! Does this kind of physic engine only work for very elastic bodys?

andrewfitz
October 6th, 2006, 04:00 PM
That gish whatever is pretty nice.

slideth3
October 6th, 2006, 06:25 PM
Nice! ChronicLogic (the makers of gish) actually also made a game called "bridgeBuilder". In that game, the springs are made of steel and are thus less flexible. I tried to implement this "stiff" behaviour, but it didn't work! Does this kind of physic engine only work for very elastic bodys?

no they should be stiff unless you use muscular constraints, the stiffness also depends on the number of iterations for which you run the constraints. Sometimes the forces acting on a particle cause it to move out of a constraint, and this is why the iterations need to be run as to make all particles satisfy all constraints. Obviously in flash there isn't enough power to run too many, which is a shame but oh well. Bridge building should work fine though, I'll give it a go later :)

cheers

sam

mnkeymasta
October 6th, 2006, 07:32 PM
The "gish whatever" isn't showing for me? I wanna see what it is:huh:

slideth3
October 7th, 2006, 05:30 AM
The "gish whatever" isn't showing for me? I wanna see what it is:huh:

hey its written in AS3.0 so you'll need the new flash 9 web player, downloadable for free somewhere or other lol. Going to try a 3d gish now. got 3d cube sorted!

http://www.kitegallery.net/misc/Slideth/Sam/vi-3d-bfc-cube.swf.html

cheers

sam

pips
October 7th, 2006, 05:34 AM
wow! really nice work... I would be very interesting to make Gish 2 or some Mortal Combat analog using this engine :D

vega
November 2nd, 2006, 01:39 AM
Very nice work. I should really start taking actionscripting seriously now. Been wasting too much time. Btw, any chance you may give tutorials for these type of stuff?

slideth3
November 2nd, 2006, 09:03 AM
Yeah just building a game using the engine at the moment, tower builder, lol, but I'll definately write some proper tutorials soon.

sorrenti
November 3rd, 2006, 12:31 AM
It would be great to see a tutorial on creating a game such as Mortal Combat!

slideth3
November 8th, 2006, 11:28 AM
wouldn't a game like that use sprites not inverse kinematics?

I guess ik could be used for when you die and stuff like in "n"

Darth Arias
November 22nd, 2006, 10:04 PM
Someone really needs to make a tutorial in depth on this... there are no good ones out there, I have looked... A good tutorial could bring on a flood of great flash games.

PARAGONPVP
December 6th, 2006, 06:42 PM
Can you post source for the ragdoll type game you made, the updated one? I've been trying to get it so it's just a basic stickman but it's not working, and if you really wanted to make your game like ragdoll you should add in more rotation when the guy moves, it's fairly simple I'm just too busy to figure out where to put it and stuff,,,

seriousmedia
October 9th, 2007, 10:36 AM
Hey Sam,

this is just what i'm looking for. I've just been reading about verlet integration today and then i found your demo here.
I'm building an 3d animation with papervision3d and want some cubes to bounce off the ground.
Could you send me the code? Please!!!

Greetings,

Ramon van de Laar
www.seriousmedia.nl (http://www.seriousmedia.nl)


hey its written in AS3.0 so you'll need the new flash 9 web player, downloadable for free somewhere or other lol. Going to try a 3d gish now. got 3d cube sorted!

http://www.kitegallery.net/misc/Slideth/Sam/vi-3d-bfc-cube.swf.html

cheers

sam