简体   繁体   中英

Nape Physics ShapeDebug seems to be scaled

i have a strange problem with a simple nape demo... here's my code

package com.secretpackage {

   import flash.display.Sprite;
   import flash.events.Event;
   import flash.text.TextField;
   import nape.phys.Body;
   import nape.shape.Circle;
   import nape.space.Space;
   import nape.util.ShapeDebug;
   import nape.geom.Vec2;
   import nape.phys.BodyType;
   import nape.phys.Material;

   public class Main extends Sprite {

    // -------
    private var world_db:Space=new Space(new Vec2(0,1000));
    private var debug:ShapeDebug;
    private var napeDebugSprite:Sprite = new Sprite();
    private var sphere:Body;
    private var labels:TextField;
    // -------

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point


        debug  = new ShapeDebug(stage.stageWidth, stage.stageHeight, stage.color);

        labels = new TextField();

        var posX:int = stage.stageWidth/4;
        var posY:int = stage.stageHeight/4;
        var r:int = 10;
        sphere= new Body(BodyType.KINEMATIC, new Vec2(posX, posY));
        sphere.shapes.add(new Circle(r, new Vec2(posX, posY), Material.rubber()));
        sphere.space = world_db;
        labels.x = 0;
        labels.y = 0;
        addChild(labels);

        var tc:test_circle = new test_circle(); 
        addChild(tc);
        tc.x = stage.stageWidth / 2;
        tc.y = stage.stageHeight / 2;

        addChild(napeDebugSprite);
        debug.drawConstraints=true;
        napeDebugSprite.addChild(debug.display);

        addEventListener(Event.ENTER_FRAME, update);
    }

    private function update(e:Event):void {
        world_db.step(1/stage.frameRate, 10, 10);
        debug.clear();
        debug.draw(world_db);
        debug.flush();
        labels.text = sphere.position.x + " - " + sphere.position.y + 
                        "\n" + stage.stageWidth + " - " + stage.stageHeight + 
                        "\n" + napeDebugSprite.width + " - " + napeDebugSprite.height + 
                        "\n" + debug.display.width + " - " + debug.display.height;
    }
}

    }

Plese note:

- sphere is a Circle with radius=10 placed at StageWidth/4 and StageHeight/4;
- test_circle is a Movieclip of a circle with radius=10 placed at StageWidth/2 and StageHeight/2;

When i compile this script i get ... http://i.imgur.com/MAYF3j9.png?1 The two circles are centered and sphere has a doubled radius.

Am i doing something wrong?

Thank you.

You set up the Body to have position (stageWidth/4, stageHeight/4), you then added a Circle shape, whose local offset is also (stageWidth/4, stageHeight/4), so when the body is un-rotated the circle will end up being at (stageWidth/2, stageHeight/2).

As to the graphical size difference, I can only assume that your graphic is not actually 'radius 10' but 'width/height 10' which is half the width/height of a radius 10 circle (whose width/height is 20)

A temporary solution is to scale debug:ShapeDebug, so code becomes...

    ....
    addChild(napeDebugSprite);
    debug.drawConstraints=true;
    //Scaling
    debug.display.scaleY = 0.5;
    debug.display.scaleX = 0.5;

    napeDebugSprite.addChild(debug.display);
    ....

This fixes the problem, but it is just a workaround.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM