how do I drag an object in libgdx using box2d? - java

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!

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.

Related

Make body borders invisible/different color

Java LibGdx Box2d
I would like to make body borders invisible or at least different color. One body should work as sensor and thus not be visible on the screen. I could not find a valid answer for that in hours and I cannot believe there is no such option in this library so I assume I must have missed one liner somewhere in Docs.
I would like to make this square transparent/invisible or at least different color but still keep it to discover movement of these circles.
Looking over stackoverflow and googling it in docs.
Closest things I found:
Make invisible body line box2d libgdx
so maybe the only solution to this is setting some flags in render method or unique render methods for each body?
What I can see on your screenshot is Box2d's shapes for debugging.
They were not designed to have adjustable visuals and are here only to show you the precise shapes and positions of your Box2d's entities.
According to the wiki (https://libgdx.com/wiki/extensions/physics/box2d#debug-renderer):
Debug Renderer
The next thing we are going to do is setup our debug renderer. You generally will not use this in a released version of
your game, but for testing purposes we will set it up now like so: ...
You will need some other kind of renderer if you want to adjust the visuals. For example:
ShapeRenderer, for simple shapes (https://libgdx.com/wiki/graphics/opengl-utils/rendering-shapes)
SpriteBatch, for textured visuals of any kind (https://libgdx.com/wiki/graphics/2d/spritebatch-textureregions-and-sprites)
PolygonSpriteBatch / CpuSpriteBatch / any custom renderer you may find or create yourself
Retrieve the positions / shapes / sizes of your Box2d entities and use one of the more advanced renderers to draw shapes or textures in positions of your Box2d entities. Box2d is not designed for drawing stuff, it just simmulates the physics and gives you position / rotation of bodies and some other info about its world.
Just make the box the same color as the background. Adding color to lines Box2d (EdgeShape)
This post shows how to change the color

Libgdx collision detection between sprites using border of sprite, not a rectangle or circle

I'm using libgdx in android studio, and I am working with sprite collision detection. I need to know how to detect a collision between two irregularly shaped sprites. My sprites are created from png image textures. Is it possible to use the border of the sprite as the detection surface? Using a rectangle or circle drawn around the sprite will not be accurate enough. Thank you for the help!
Use Box2D which is a great and easy to learn library for physics and lighting. After setting bodies correctly, you can set body shape as rectangle, circle, square or any custom shape using paths, you don't need to worry about collision at all. You only take care what should happen while or after collision

How do I make an isometric hitbox?

I am making a game in Java that uses an isometric grid pseudo-3D system. It uses tiles that have this basic shape:
I can't work out how to make a hitbox that covers all the area of a tile, and only the area of a tile. I need this because all my tiles are stacked and hitbox stacking can produce unwanted results.
It would depend a bit on your targeted framework (as it might already support the functionality), but at a very basic level, you could make use of the Java2D's Shape API to create a polygon representing the shape of the tile and use it's contains and intersects functionality
As a conceptual example

Java 2D- Actual Player Collision Detection | Ignore Transparency

So I'm creating a 2D game platform that works with Tiles. Currently it won't let the player go through the any tiles and works fine. Though rather then stopping the player at a solid tile. I would like to stop the player at an actual object. Pretend the triangle is in a tile.
Whats Happening:
What I want:
I would like the player to be able to walk through the tile until their is no more transparents. Basically walk on the triangle.
Player Class http://pastebin.com/SJrzSvVV
Tile Class http://pastebin.com/V3nqxh61]
TileMap Class http://pastebin.com/fuj8dR5K
You should check out the intersects() method provided by the Java2D API.
You can define the two sprites as shapes and adjust the coordinates when the two shapes intersect. I'm guessing you are splitting up a BufferedImage to create the animation frames.
You can draw a Rectangle or Ellipse frame around your sprites and check those for collisions. The frames do not have to be visible but making them visible helps when debugging.
Figured it out, essentially what I did was I was creating this game using tiles. I created something to scan a tile and find out where it's transparent, and allow players to go through it or not depending on whether the transparency is already inside something in the tile or not.

Box2d lights - lights over sprite

I wanted to ask if you can use box2d lights so you can see only objects that are in the lights area. For example i have a flashlight and only want to see game objects in the light. I managed to do something like this but the problem is that the sprites of the game objects lose their color intensity because I render lights on top of sprites and the game itself doesn't look good because of this (even though it is the effect that i want).I used box2d bodies with user data containing sprites. I can't figure any way out. Is there any proper way to use box2d lights library to make these objects visible and with their real color? (I am setting lights to X-rays to do this; also I am using it with libgdx in java).
It may be because the default setting is to not use diffuseLight. You have to set rayHandler.useDiffuseLight(true).
Libgdx and Box2DLights - too bright + colors grayed out

Categories