在BeginContact事件上设置新的正文位置

时间:2013-11-14 10:14:42

标签: javascript box2d

我想在BeginContact事件上设置一个新的body位置,但它仍然不起作用。它是在JavaSript中写入绘图到画布,但它对Box2d无关紧要。在HTML文件中,body只是空画布,没有别的。这是我的代码:

在JS文件的开头只声明了一些变量。

Vec2 = Box2D.Common.Math.b2Vec2;
BodyDef = Box2D.Dynamics.b2BodyDef;
Body = Box2D.Dynamics.b2Body;
FixtureDef = Box2D.Dynamics.b2FixtureDef;
Fixture = Box2D.Dynamics.b2Fixture;
World = Box2D.Dynamics.b2World;
PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
DebugDraw = Box2D.Dynamics.b2DebugDraw;

var player;

接下来是一个在开始时调用的设置函数。

function setup()
{
    canvas = document.getElementById("collisionCanvas");
    context = canvas.getContext('2d');

    document.getElementsByTagName('body')[0].style.backgroundColor = "black";

    canvas.style.backgroundColor = "white";
    canvas.width = 320;
    canvas.height = 320;

    world = new World(new Vec2(0, 10), false);

    //Point of the problem!!!
    //setting contact listener
    var listener = new Box2D.Dynamics.b2ContactListener;
    listener.BeginContact = function(contact)
    {
        var body1 = contact.GetFixtureA().GetBody();
        var body2 = contact.GetFixtureB().GetBody();

        if(body1.GetUserData().type == "player")
        {
            body1.SetPosition({x:5, y:5});
        }
        else
        {
            body2.SetPosition({x:5, y:5});
        }
    }
    world.SetContactListener(listener);

    var fixDef = new FixtureDef;
    fixDef.density = 1.0;
    fixDef.friction = 0.5;
    fixDef.restitution = 0.2;

    var bodyDef = new BodyDef;

    //creating ground
    bodyDef.type = Body.b2_staticBody;
    bodyDef.position.x = convertPixelsToMeters(160);
    bodyDef.position.y = convertPixelsToMeters(320-32/2);
    bodyDef.userData = {type: "static"};
    fixDef.shape = new PolygonShape;
    fixDef.shape.SetAsBox(convertPixelsToMeters(canvas.width/2), convertPixelsToMeters(32/2));
    world.CreateBody(bodyDef).CreateFixture(fixDef);

    //creating player
    bodyDef.type = Body.b2_dynamicBody;
    bodyDef.fixedRotation = true;
    bodyDef.position.x = convertPixelsToMeters(160);
    bodyDef.position.y = convertPixelsToMeters(160);
    bodyDef.userData = {type: "player"};
    fixDef.shape = new PolygonShape;
    fixDef.shape.SetAsBox(convertPixelsToMeters(16), convertPixelsToMeters(16));
    player = world.CreateBody(bodyDef);
    player.CreateFixture(fixDef);

    //setup debug draw
    var debugDraw = new DebugDraw();
    debugDraw.SetSprite(document.getElementById("collisionCanvas").getContext("2d"));
    debugDraw.SetDrawScale(32.0);
    debugDraw.SetFillAlpha(0.3);
    debugDraw.SetLineThickness(1.0);
    debugDraw.SetFlags(DebugDraw.e_shapeBit | DebugDraw.e_jointBit);
    world.SetDebugDraw(debugDraw);

    window.setInterval(update, 1000 / 60);
}

最后只有更新功能,一个帮助功能就是这样。

function update()
{
    world.Step(
        1 / 60   //frame-rate
        ,  10       //velocity iterations
        ,  10       //position iterations
    );
    world.DrawDebugData();
    world.ClearForces();
}

function convertPixelsToMeters(x)
{
    return x*0.03125;
}

$(function(){
    setup();
})

重要的只是中间代码,其中是BeginContact事件,其中调用的SetPosition函数不起作用。

我尝试在其他地方更改位置,例如在KeyDown事件上,并且它是正确的,所以我可以理解为什么它不起作用。

1 个答案:

答案 0 :(得分:2)

在b2Contactlistner方法中,我们无法更改任何属性或位置。

你可以接受任何布尔变量,并在beign contact时将其设为true,如果根据布尔变量改变body的位置。

就像你的代码一样.......    var bodyyy;
var boolennn

listener.BeginContact = function(contact)
{
    var body1 = contact.GetFixtureA().GetBody();
    var body2 = contact.GetFixtureB().GetBody();

    if(body1.GetUserData().type == "player")
    {
        //body1.SetPosition({x:5, y:5});
       bodyyy = body1;
       booleannn = true;
    }
    else
    {
       // body2.SetPosition({x:5, y:5});
        bodyyy = body2;
        boolennn = true;
    }
}

现在在您的Update方法中

if(booleann)
{
 bodyyy.SetPosition({x:5, y:5})
}

抱歉我不知道java脚本的语法