gpt4 book ai didi

javascript - 在 BeginContact 事件中设置新的 body 位置

转载 作者:行者123 更新时间:2023-11-30 17:46:54 25 4
gpt4 key购买 nike

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

在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 事件上,它是正确的,所以我可以理解为什么它不起作用。

最佳答案

在 b2Contactlistner 方法中我们不能改变任何属性或位置。

您可以采用任何 bool 变量,并在接触时使它为真,如果根据 bool 变量改变 body 的位置。

如您的代码所示...... var bodyyy;
变量 bool 值

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脚本的语法

关于javascript - 在 BeginContact 事件中设置新的 body 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com