libGdx How to use images or actor as body

2019-02-19 13:04发布

问题:

I gone through the libGdx wiki/tutorial but I didn't find example to use an image or an actor as a physics body. In my game I am adding an actor to the stage. But I want to add this actor or sprite image as physics body.

I have to drag this actor and even want to detect collision with other bodies.

please give me reference if you have.

Thanks

回答1:

This is a bit tricky. You don't use an image or an Actor as a physics body.

You will need to implement something like a new "PhysicsActor" which extends Actor. This Physics actor will have a body as a property and will be the bridge between your libgdx scene and the box2d world.

The Actor will update it's position based on the body's position (be careful with the meter to pixel conversion). The Actor will receive the input like a click on it and needs to create a (Mouse-)Joint for the Body to be dragged and follow the mouse.

How you handle the rendering of images attached to the body is also custom. You might give each PhysicsEntity manually a Sprite which will be rendered by the draw-call. Or you will use an editor (RUBE for example, there is also a rube-loader in the Contributions-section of the libgdx forum) which will let you attach images to the bodies and you will read those from the editor.

You might also check this link http://www.netthreads.co.uk/2012/04/23/libgdx-box2d-scene2d-bumpers-demo/ I guess it's exactly what you need, but it seems to be implemented a little bit different. He used some kind of Layer architecture to bridge the libgdx scene and the box2d world.



回答2:

there is already a question like this on stackoverflow

Apart form it box2d and scene2d