Trying to generate sword swinging attack using Box2D in Citrus Engine

146 Views Asked by At

I've been banging my head up against a wall for the past couple of days trying to figure out how to properly extend CitrusEngine's Box2DPhysicsObjects to generate my custom objects. My goal is to generate this behavior: example of desired behavior.

This is designed to simulate my hero dashing at a direction determined by using input while swinging his sword to attack. The sword "sleeps" until the attack state is activated.

I think I have a fundamental misunderstanding of how to properly use Box2D (especially joints). If someone could point me in the right direction I would be eternally grateful. I can't really provide my current code because it's become beyond broken.

1

There are 1 best solutions below

0
On

An implementation such as the one above would have very poor performance and would likely tunnel in almost every situation. Therefore a solution is to add a sensor with a funnel shape and add a joint between this sensor and my hero. The implementation:

    override protected function createShape():void{

        var radius:Number = 4;
        var vertices:Vector.<b2Vec2> = new Vector.<b2Vec2>();
        vertices.push(new b2Vec2(0,0));

        for (var i:int = 0; i < 7; i++) {
            var angle:Number = i / 6.0 * .5* Math.PI;
            vertices.push(
                new b2Vec2( radius * Math.cos(angle), 
                            radius * Math.sin(angle) ));
        }

        var sword_shape:b2PolygonShape = new b2PolygonShape();
        sword_shape.SetAsVector(vertices,8);
        _shape = sword_shape;       
    }