简体   繁体   English

Box2d setTransform

[英]Box2d setTransform

Set the scene: • Our environment: LibGdx Android & iOS //If you don't know LibGdx, it's fine, just know that this program is being build for iOS and Android 设置场景:•我们的环境:LibGdx Android和iOS // //如果您不了解LibGdx,那就很好,只需知道此程序是为iOS和Android构建的

• Our screen has nothing on it, just a Box2d Circle •我们的屏幕上没有任何东西,只有Box2d Circle

Okay, so, what do I need to do? 好的,那我该怎么办? I need to make my Box2d Circle to "teleport" to where the screen is tapped... but I need it to have some velocity so if it collides w/ something else it will throw the other object out of its way. 我需要使Box2d Circle能够“传送”到点击屏幕的位置...但是我需要使其具有一定的速度,因此,如果它与其他物体碰撞,它将把其他物体挡住。

I am currently using body.setTransform() but I have no velocity and people say it is very buggy. 我目前正在使用body.setTransform(),但速度不快,有人说它很容易出错。

How can I do this? 我怎样才能做到这一点? Thank you! 谢谢!

The setTransform method is not buggy itself. setTransform方法本身不是错误的。 It can cause some troubles because of ignoring physics which can appear when : 由于忽略物理现象,可能会引起一些麻烦

  • using joints - joints provide some constraint and this "violent" change can make bodies behave weird 使用关节 -关节提供了一些约束,这种“剧烈的”变化会使身体表现得怪异
  • collision exists - because the transformed body can be "pushed" into other body 存在碰撞 -因为可以将转换后的物体“推入”其他物体

The situation you are describing is totally typical situation when setTransform is being used and I see no reason here to be afraid of it. 您所描述的情况是在使用setTransform时的完全典型情况,在这里我没有理由担心。 You don't need any velocity here also. 您在这里也不需要任何速度。


However, if you decide to change the "teleporting" mechanism to applying the velocity to move object to the world point you should: 但是,如果决定更改“传送”机制以应用速度将物体移动到世界点,则应该:

  • calculate velocity vector by subtracting target position and body position 通过减去目标位置和身体位置来计算速度矢量

     Vector2 velocity = target.sub( body.getPosition() ) ); //where target is Vector2 of touched point 
  • limit the velocity to some maximum I guess if you need to (this is optional) 将速度限制到最大,我猜是否需要 (这是可选的)

     //optional velocity.nor(); velocity.mul( MAX_VALUE ); //MAX_VALUE is a float 
  • set the velocity to the body 设定身体的速度

     body.setLinearVelocity( velocity ); 
  • add to main loop (render method) check if the body is in target position (or in some range - due to precision there is small chance that it will precisely at the target if you will limit the velocity - if not I guess it should be in the target position after one iteration of world.update() ) 添加到主循环(渲染方法)中,检查身体是否处于目标位置(或处于某个范围内)-由于精度的原因,如果您限制速度,则将其精确地对准目标的可能性很小-如果不是,我想应该是在world.update()的一次迭代之后位于目标位置

     if( body.getPosition().sub( target ).len() < SOME_PRECISION ) { body.setLinearVelocity( new Vector2(0, 0) ); } 

You can also take a look at Box2D MouseJoint although I've been never using this and cannot provide any hint here. 您也可以看一下Box2D MouseJoint,尽管我从来没有使用过它,也无法在此处提供任何提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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