开发者

How to get a physics engine like Nape working?

Introduction: I think Nape is a relatively new engine so some of you may not know it. It's supposedly faster than box2d and I like that there is decent documentation. Here's the site: http://code.google.com/p/nape/

I'm relatively new to programming. I am decent at AS3's basic functionality, but every time I try to implement some kind of engine or framework I can't even seem to get it to work. With Nape I feel I got a little further than before but I still got stuck.

My problem: I'm using Adobe CS5, I managed to import the SWC file like described here. Next I tried to copy the source of one of the demo's like this one and get it to work but I keep getting errors. I made a new class file, copied the demo source to it, and tried to add it to the stage.

My stage code basically looks like this:

import flash.Boot; // these 2 lines are as described in the tutorial
new Boot(); 

var demo = new Main(); // these 2 are me guessing what I'm supposed to do
addChild(demo);

Well, it seems the source code is not even being recognized by flash as a valid class file. I tried editing it, but even if I get it recognized (give a package name and add curly brackets) but I still get a bunch of errors. Is it psuedo code or something? What is going on?

My goal: I can imagine I'm going about this the wrong way. So let me explain what I'm trying to achieve. I basically want to learn how to use the engine by starting from a simple basic example that I can edit and mess around with. I开发者_运维问答f I can't even get a working example then I'm unable to learn anything. Preferably I don't want to start using something like FlashDevelop (as I'd have to learn how to use the program) but if it can't be helped then I can give it a try.


The code sample on the site is Haxe, not ActionScript, which explains the errors you got when adding 'package'.

Here's a modified version of the class for ActionScript:

package{ 

import flash.display.Graphics; 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.display.StageQuality; 

import nape.space.Space; 
import nape.space.UniformSleepSpace; 
import nape.util.Tools; 
import nape.geom.Vec2; 
import nape.geom.GeomPoly; 
import nape.phys.PhysObj; 
import nape.phys.Material; 
import nape.geom.AABB; 

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  

public class Main extends Sprite
{ 
        private var space:Space; 

        private var g:Graphics; 
        private var vl:Array; 
        private var px:Number; private var py:Number; 

        //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  

        private function mouseDown(ev:MouseEvent):void { 
                var x = stage.mouseX; 
                var y = stage.mouseY; 

                g.lineStyle(1, 0, 0.5); 
                vl.push(new Vec2(x, y)); 
                g.moveTo(x, y); 
                px = x; py = y; 
        } 

        private function mouseUp(ev:MouseEvent):void { 
                if (vl.length >= 3) { 
                        var poly = new GeomPoly(vl); 
                        poly.simplify(50, 4); 
                        if (!poly.selfIntersecting()) { 
                                if (!poly.cw()) poly.points.reverse(); 

                                var p = Tools.createConcave(poly, 0, 0, 0, false, Material.Wood); 
                                space.addObject(p); 
                                addChild(p.graphic); 
                        } 
                } 
                g.clear(); 
                vl = new Array(); 
                px = -100; 
        } 

        //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  

        public function init(ev:Event = null)
        { 
                var s = new Sprite(); 
                g = s.graphics; 
                addChild(s); 
                s.cacheAsBitmap = true; 
                vl = new Array(); 
                px = -100; 

                space = new UniformSleepSpace(new AABB(0,0, 800,600), 30, new Vec2(0, 250)); 

                var p:PhysObj; 
                space.addObject(p = Tools.createBox(-20, 300, 50, 600, 0, 0, 0, true, Material.Wood)); 
                addChild(p.graphic); 
                space.addObject(p = Tools.createBox(820, 300, 50, 600, 0, 0, 0, true, Material.Wood)); 
                addChild(p.graphic); 
                space.addObject(p = Tools.createBox(400, -20, 800, 50, 0, 0, 0, true, Material.Wood)); 
                addChild(p.graphic); 
                space.addObject(p = Tools.createBox(400, 620, 800, 50, 0, 0, 0, true, Material.Wood)); 
                addChild(p.graphic); 

                stage.quality = StageQuality.MEDIUM; 
                stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown); 
                stage.addEventListener(MouseEvent.MOUSE_UP,   mouseUp); 
                stage.addEventListener(Event.ENTER_FRAME,     enterFrame); 
        } 

        //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  

    private function enterFrame(ev:Event) { 
                graphics.clear(); 
                graphics.lineStyle(1, 0, 1); 
                if (px != -100) { 
                        var x = stage.mouseX; 
                        var y = stage.mouseY; 
                        var dx = x - px; var dy = y - py; 
                        if (dx * dx + dy * dy > 5) { 
                                vl.push(new Vec2(x, y)); 
                                px = x; py = y; 
                                g.lineTo(x, y); 
                        }else { 
                                graphics.moveTo(px, py); 
                                graphics.lineTo(x, y); 
                        } 
                } 

        space.step(1/60, 6, 6); 
        } 
}
}

And here's the timeline code:

import flash.Boot; 
new Boot();

var m:Main = new Main();
m.addEventListener(Event.ADDED_TO_STAGE, m.init);
addChild(m)

Your fla should be 800x600 with a frameRate of 60 for this setup. Also, notice that I've changed the constructor to the init method. That is because the stage is accessed there, and until the instance of Main is added to the stage, the reference to stage will be null.

Mixing AS3 and Haxe is not easy for someone new to programming. If you're interested in Haxe, go for it, otherwise, for easier 2D physics I would recommend giving Box2D World Construction Kit a go. It allows you to easily prototype worlds straight in the IDE using component definitions and it should be a bit faster than the direct AS3 port since it's using Alchemy.

How to get a physics engine like Nape working?

Also, there is a free introductory video tutorial on gotoAndLearn to get you started.

Goodluck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜