how do I drag an object in libgdx using box2d?

爱⌒轻易说出口 提交于 2019-12-22 12:05:19

问题


I have added objects to a box2d world in libgdx.
I am wondering if it was possible to drag objects with the mouse? If so, how?

thanks!


回答1:


There are a couple of options here. You can use a mouse joint or you can use a kinematic body and set it's position manually. A good example of how to use a mouse joint check this out:

http://code.google.com/p/libgdx-backend-android-livewallpaper/source/browse/gdx-backend-android-livewallpaper-example/src/com/badlogic/gdx/tests/box2d/Box2DTest.java?r=ba02aaf34a8ca07daa0c30493bab993067c652f9

If you want to use a kinematic body you would do this:

in render():

body.setTransform(Gdx.input.getX(), Gdx.input.getY(), angle);

And then you would say body.getPosition() for the rendering of your sprites. Or if you are using the debug renderer that will draw your bodies, but just as shapes.



来源:https://stackoverflow.com/questions/12123470/how-do-i-drag-an-object-in-libgdx-using-box2d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!