A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [MX04] Ragdoll Engine - Need Help

  1. #1
    Working On An Online RPG grimm 88's Avatar
    Join Date
    Sep 2007
    Location
    Nowhere
    Posts
    280

    [MX04] Ragdoll Engine - Need Help

    hi, i have recently started trying to figure out a ragdoll engine and was wondering if anybody out there could please tell me;

    how to add a second player's ragdoll (that would be a different colour so that they could battle)

    and also how to change the colour of the ragdolls both to begin with, eg player 1: black, player 2: dark grey, and during the movie, aka. if player 1 got a metal suit powerup that reduces damage done to him, that it would change his colour to be light grey instead of black.

    here is the code i am using... please ask if you need any more information about what i am trying to achieve.

    Code:
    function Vector(x, y) {
    	this.x = x;
    	this.y = y;
    	this.length = Math.sqrt(x*x+y*y);
    }
    function constrain(p0, p1, l) {
    	var correction = 0.025000;
    	var dx = p1._x-p0._x;
    	var dy = p1._y-p0._y;
    	var d = new Vector(dx, dy);
    	var r = (d.length-l)/d.length;
    	var x = d.x*0.500000*r;
    	var y = d.y*0.500000*r;
    	p1._x = p1._x-(x-correction);
    	p1._y = p1._y-y;
    	p0._x = p0._x+(x+correction);
    	p0._y = p0._y+y;
    }
    function update() {
    	var p;
    	var i = 0;
    	while (i<joints.length) {
    		this[joints[i]].update();
    		i++;
    	}
    }
    function collisions() {
    	var p;
    	var x;
    	var y;
    	var c = false;
    	var i = 0;
    	while (i<joints.length) {
    		p = this[joints[i]];
    		if (p.collisions()) {
    			c = true;
    			x = p._x;
    			y = p._y;
    		}
    		i++;
    	}
    	if (c) {
    		if (!soundplaying) {
    			var sbreak = new Sound(this);
    			sbreak.onSoundComplete = function() {
    				soundplaying = false;
    			};
    			var rand = random(4);
    			var pan = -100+head._x*200/445;
    			sbreak.attachSound("break"+rand);
    			sbreak.start(0, 0);
    			sbreak.setVolume(75);
    			sbreak.setPan(pan);
    			soundplaying = true;
    		}
    		drawblood(x, y);
    	}
    }
    function constraints() {
    	var i = 0;
    	while (i<2) {
    		constrain(lhand, lelbow, 12);
    		constrain(lelbow, neck, 12);
    		constrain(rhand, relbow, 12);
    		constrain(relbow, neck, 12);
    		constrain(neck, waist, 20);
    		constrain(lknee, waist, 20);
    		constrain(lfoot, lknee, 20);
    		constrain(rknee, waist, 20);
    		constrain(rfoot, rknee, 20);
    		constrain(head, neck, 3.500000);
    		i++;
    	}
    }
    function setup() {
    	constraints();
    	var p;
    	var i = 0;
    	while (i<joints.length) {
    		p = this[joints[i]];
    		p.lx = p._x;
    		p.ly = p._y;
    		i++;
    	}
    }
    function drawblood(x, y) {
    	var b;
    	var i = 0;
    	while (i<=random(3)+3) {
    		b = attachMovie("Blood", "Blood"+blooddepth, blooddepth++);
    		b._x = x;
    		b._y = y;
    		i++;
    	}
    }
    function drawhead(col) {
    	var cx = 0;
    	var cy = 0;
    	var r = 7;
    	head.beginFill(col == undefined ? (0) : (col), 100);
    	head.moveTo(cx+r, cy);
    	var a = 0;
    	while (a<=6.283185) {
    		head.lineTo(cx+Math.cos(a)*r, cy+Math.sin(a)*r);
    		a = a+0.069813;
    	}
    	head.endFill();
    }
    function draw(col) {
    	clear();
    	lineStyle(5, col == undefined ? (0) : (col), 100);
    	moveTo(lhand._x, lhand._y);
    	lineTo(lelbow._x, lelbow._y);
    	lineTo(neck._x, neck._y);
    	lineTo(relbow._x, relbow._y);
    	lineTo(rhand._x, rhand._y);
    	moveTo(neck._x, neck._y);
    	lineTo(waist._x, waist._y);
    	lineTo(lknee._x, lknee._y);
    	lineTo(lfoot._x, lfoot._y);
    	moveTo(waist._x, waist._y);
    	lineTo(rknee._x, rknee._y);
    	lineTo(rfoot._x, rfoot._y);
    	var dx = neck._x-head._x;
    	var dy = neck._y-head._y;
    	head._rotation = Math.atan2(dy, dx)*180/3.141593;
    }
    function onEnterFrame() {
    	update();
    	constraints();
    	collisions();
    	draw(ragdollcolor);
    }
    MovieClip.prototype.useHandCursor = false;
    var dragging = null;
    var blooddepth = 0;
    var soundplaying = false;
    var ragdollcolor = 0;
    var joints = new Array("lhand", "lelbow", "rhand", "relbow", "neck", "waist", "lknee", "lfoot", "rknee", "rfoot", "head");
    drawhead(ragdollcolor);
    setup();
    stop();

  2. #2
    Senior Member ozmic66's Avatar
    Join Date
    Oct 2005
    Posts
    472
    So you found this cool piece of code, and you post it here for people to dissect it and tell you how to make it more modular... this may be the wrong forum
    I think you'll have better luck asking specific questions about the physics aspects of the problem and I'm sure you'll get more fruitful answers
    Pixelwave Flash-based iPhone framework
    iPhone Games: Flyloop | Freedom Run

    Twitter: Oztune

  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    create two movieclips, player1 and player2, give them each these properties:

    color = 0x000000
    joints = //an array of joints

    run the above code on both movieclips, using the above data specific for each movieclip. When you want to change color, change the players color variable.
    lather yourself up with soap - soap arcade

  4. #4
    Working On An Online RPG grimm 88's Avatar
    Join Date
    Sep 2007
    Location
    Nowhere
    Posts
    280
    wow thanks... that helped loads, the only problems now are that they seem to be in slow motion, falling really slowly, hardly responding to arrow keys, the boundaries that are meant to stop them going off stage seem to be in the wrong place, and player 2 seems to be falling slowly up instead of down... any help with this...
    Last edited by grimm 88; 09-20-2007 at 12:02 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center