- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 AndEngine(适用于 Android)中使用 Box2D。
我的目的是在两个物体相互碰撞时创建一个力关节。
当我尝试在 ContactListner
过程中在两个对象(实体)之间创建一个鼠标关节时。应用程序将挂起一段时间然后退出,没有任何错误,只是线程结束的通知。
当我调用 mEnvironment.CreateForceJoint(..)
在 ContactListener 之外 时,联合创建是OK -当应用程序在某些物理环境中运行时的某处。UpdateHandler()。
请帮我解决问题,或者找出原因。感谢您的帮助!
这是我的代码:
public class MyActivity extends SimpleBaseGameActivity {
private final String DEBUG_TAG = "MyActivity";
private GEnvironment mEnvironment;
private PhysicsWorld mPhysicsWorld;
private MyFixture FIXTURE_PLANET = GlobalSettings.FIXTURE_PLANET;
private MyFixture FIXTURE_VACUUM = GlobalSettings.FIXTURE_VACUUM;
// CODE TO CREATE RESOURCES and ENGINE OPTIONS....
@Override
protected Scene onCreateScene() {
Scene scene = new Scene();
scene.setBackground(new Background(0.8627f, 0.9020f, 1.0f));
//CODE: creating physic world
//.....
//creating game environment
mEnvironment = new GEnvironment(mPhysicsWorld, scene, mEngine);
//CODE: creating objects, register and attach them into scene
GMediaPlanet sunZone = mEnvironment.CreatePlanet(x1, y1, sunTextureRegion, FIXTURE_PLANET);
GMediaPlanet earthZone = mEnvironment.CreatePlanet(x2, y2, earthTextureRegion, FIXTURE_PLANET);
// enable contact listener, detect collision between bodies
mPhysicsWorld.setContactListener(new PlanetContactHandler());
return scene;
}
// ----------------------------------------------------
// Handling collision between letter cubes
// ----------------------------------------------------
/**
* Handling the collision of GMediaPlanets
*/
public class PlanetContactHandler implements ContactListener {
private final String DEBUG_TAG = "CONTACT";
// if there's one collision, do not handle others or re-handle it
private boolean mIsColliding = false;
@Override
public void beginContact(Contact contact) {
if (mIsColliding)
return;
//-----------------------------------------------
//suppose colliding objects to be sunZone and earthZone
//-----------------------------------------------
Object aTag = contact.getFixtureA().getBody().getUserData();
Object bTag = contact.getFixtureB().getBody().getUserData();
if (aTag != null && bTag != null) {
GMediaPlanet box = null;
GMediaPlanet cube = null;
if (aTag instanceof GMediaPlanet
&& bTag instanceof GMediaPlanet) {
box = (GMediaPlanet) aTag;
cube = (GMediaPlanet) bTag;
}
if (box != null && cube != null)
{
//!!!!!!!-----------------------------------------------------
//This code will HANG the app when called inside contact listner:
MouseJoint mTestJoint = mEnvironment.CreateForceJoint(box, cube);
//!!!!!!!-----------------------------------------------------
Vector2 target = Vector2Pool.obtain(box.GetLocation());
mTestJoint.setTarget(target);
Vector2Pool.recycle(target);
}
}
mIsColliding = true;
}
@Override
public void endContact(Contact contact) {
Log.d(DEBUG_TAG, "end colliding!");
mIsColliding = false;
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
}
}
public class GMediaPlanet
{
protected IAreaShape mSprite = null;
protected Body mBody = null;
public GMediaPlanet()
{ }
public Vector2 GetLocation()
{
mBody.getPosition();
}
}//end
public class GEnvironment
{
private PhysicsWorld mWorld;
private Scene mScene;
private org.andengine.engine.Engine mEngine;
public GEnvironment(PhysicsWorld pWorld, Scene pScene, org.andengine.engine.Engine pEngine)
{
mWorld = pWorld;
mScene = pScene;
mEngine = pEngine;
}
/** the constructor is hidden, available within Appearances packet only */
public GMediaPlanet CreatePlanet(float pX, float pY, ITextureRegion pTextureRegion, MyFixture pFixture)
{
GMediaPlanet entity = new GMediaPlanet();
entity.mSprite = new Sprite(pX, pY, pTextureRegion, mEngine.getVertexBufferObjectManager());
entity.mBody = PhysicsFactory.createCircleBody(mWorld, entity.mSprite, BodyType.DynamicBody,
pFixture.GetDef(), GlobalSettings.PIXEL_2_METER);
mScene.attachChild(entity.mSprite);
entity.mSprite.setUserData(entity.mBody);
entity.mBody.setUserData(entity);
mWorld.registerPhysicsConnector(new PhysicsConnector(entity.mSprite, entity.mBody, true, true));
return entity;
}
// -----------------------------
// Creating JOINTS
// -----------------------------
/**
* Creating a force joit based on type of MouseJointDef
*
* @param anchorObj
* the static object in the mTestJoint (anchor base)
* @param movingObj
* object to move forward the target
*/
public MouseJoint CreateForceJoint(GMediaPlanet anchorObj, GMediaPlanet movingObj)
{
ChangeFixture(movingObj, GlobalSettings.FIXTURE_VACUUM);
MouseJointDef jointDef = new MouseJointDef();
jointDef.dampingRatio = GlobalSettings.MOUSE_JOINT_DAMP;
jointDef.frequencyHz = GlobalSettings.MOUSE_JOINT_FREQ;
jointDef.collideConnected = true;
Vector2 initPoint = Vector2Pool.obtain(movingObj.mBody.getPosition());
jointDef.bodyA = anchorObj.mBody;
jointDef.bodyB = movingObj.mBody;
jointDef.maxForce = (GlobalSettings.MOUSE_JOINT_ACCELERATOR * movingObj.mBody.getMass());
// very important!!!, the initial target must be position of the sattelite
jointDef.target.set(initPoint);
MouseJoint joint = (MouseJoint) mWorld.createJoint(jointDef);
Vector2Pool.recycle(initPoint);
// very important!!!, the target of the joint then changed to target anchor object
Vector2 targetPoint = Vector2Pool.obtain(anchorObj.mBody.getWorldCenter());
joint.setTarget(targetPoint);
Vector2Pool.recycle(targetPoint);
return joint;
}
public void ChangeFixture(GMediaPlanet entity, MyFixture pFixture)
{
Filter fil = new Filter();
fil.categoryBits = pFixture.categoryBit;
fil.maskBits = pFixture.maskBits;
if(entity.mBody != null)
{
entity.mBody.getFixtureList().get(0).setFilterData(fil);
}
}
}
最佳答案
你不能在Step()-调用Box2D中修改世界,因为世界是锁定的!你应该得到一个异常(exception)。你必须记住哪些对象正在碰撞并在 beginContact 之后做一些事情......例如在更新函数中。
关于java - Box2D 和引擎 : app hangs out when creating Joints during ContactListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13912254/
我有一个非常奇怪的问题,到目前为止我无法调试......问题是......我的应用程序需要下载一些东西才能工作。因此,在 onCreate() 方法的开头,我检查是否已经下载了某些内容。如果没有,我会
我编写了一个基于尝试解决骑士之旅问题的程序。我相信我已经想出了一个合适的解决方案,一切看起来都很好。 我很好奇的一个小问题是一小段代码实现了基于展望 future 可能的方 block 的最佳着法。
我有一段很长的java代码,它使用selenium webdriver和firefox来测试我的网站。如果我不能在这里复制它,请原谅我。它有一个无限的 while 循环来重复执行其功能。这就是它应该做
def retCursor(): host = "localhost" user = "disappearedng" db = "gupan_crawling3" co
与设计师相比,我更像是一名网络程序员,但目前我的工作要求我了解更多关于网页设计的知识。对不起,如果这个问题对专业人士来说太天真了。 在这个问题上卡了几个小时,不知道该怎么办... 请到我的博客查看底部
这是我的源代码: #include using namespace std; int main() { int numBoxes, // Number of boxes
我想要标记单词组的标签,其中: 标签在词组的左边 文字是用线括起来的,标签不是 文字缩进,标签不缩进 这里有一些东西会产生错误的格式。左侧边界应用于::before 生成的文本,缩进也应用于::bef
我运行了几个 python 子进程来将数据迁移到 S3。我注意到我的 python 子进程经常下降到 0%,并且这种情况持续超过一分钟。这会显着降低迁移过程的性能。 这是子流程的图片: 子进程执行以下
编辑:其他容器正常运行。 docker run hello-world 工作正常。 我正在尝试运行最新的 nginx docker 镜像。它无限期地挂起。我已经在 2 个单独的全新安装的 ubuntu
大家好,我要脱发了。我有一个试图连接到服务器的客户端,一切似乎都很好,使用 gethostbyname() , socket() , bind() ,但是当试图 connect()它只是卡在那里,服务
我正在尝试通过驱动器映射在远程服务器上运行批处理文件,如下所示,但进程挂起...... Enable-WSManCredSSP -Role Client -DelegateComputer somes
我刚开始使用 postman 。我在执行集合运行程序时遇到了这个错误“错误:套接字挂断”。我已经阅读了一些关于套接字挂断的帖子,它提到了发送请求并且服务器端没有响应并且可能超时。如何在 Postman
我正在 http 请求函数内调用异步函数。被调用的异步函数执行另一个 http 请求,但该 http 请求抛出错误 错误:套接字挂起 代码 var http = require("http")
我之前使用 arraylist 作为结构,但在这段代码中它不起作用。由于我找不到错误,有人可以帮助我吗? (我确信这是我的错误,但 IDE 没有说什么) 流程:首先是类(class)游戏。我调用 ru
我有一个简单的套接字服务器(用于 HL7 通信)。当它在生产中运行时间较长时,套接字线程会挂起并消耗大量 CPU 时间。 这是监听器线程的相关代码(缩短): public void run() {
我想使用以下代码创建一个信号量计数器: union semun arg_assistant; int max_ass = atoi(argv[1]); printf("Num massimo di a
我正在尝试让我的模型迁移到我正在使用的 postgress 数据库,该数据库在本地托管,我确认它已正常运行。 当我运行 sequelize db:migrate 时,它说 Loaded configu
有人知道为什么 IMemAllocator:GetBuffer 的任何原因吗? (Directshow) 挂起,除了所有正在使用的样本? 我有一个使用 GMFBridge by Geraint Dav
我一直在尝试使用 Java 的 ProcessBuilder 在 Linux 中启动一个应该“长期”运行的应用程序。该程序运行的方式是启动一个命令(在本例中,我正在启动一个媒体播放应用程序),允许它运
我需要获取一个文件列表,将它们修剪到目录路径并返回一个不同的列表。在某些情况下,这可能会处理超过 500 万个文件。 我遇到了一个问题,即核心进程因 CPU 使用率 0% 而挂起,原因我无法确定。 v
我是一名优秀的程序员,十分优秀!