LibGDX Hitbox / Polygonshape and Physics - libgdx

Hello I have two smaller questions:
Is it possible to map the Shape of the Body 1:1 to the texture so I have a perfect collision detection ? Because right now I only know of two ways to do it: Create the Shape before and load it up with a texture or load the texture first and then set the shape as a box ... But that is too simple I think.
Also does anyone know how I can turn off the physics of the world ? When Body A hits Body B I want the Body to do a certain animation and fall to the ground with some sound effects, but when the two objects collide they bounce off each other in different directions etc. When two objects collide I want it to be "Game over" basically and stop calculating momentum etc.
I dont care about the solution itself, just telling me if its possible at all for both questions would be enough.

I guess you are using Box2d
Is it possible to map the Shape of the Body 1:1 to the texture so I
have a perfect collision detection ?
It possible, you can use PolygonShape in Box2D, but it has to be a convex polygon. Furthermore it slower then predefined shapes.
Also does anyone know how I can turn off the physics of the world ?
With box2d Physics you can simply stop the simulation on your own by not executing the world.step() command

Related

Changing the animation and velocity in water

I'm trying to make a 2D game in Java, and I'm using Libgdx. But, I don't know how to change the animation and velocity in water, for instance, changing the movement from walking to swimming.
Can anyone help me with the code and methods please?
Assuming that you use rectangular areas for your water and your sprites, you might want to look at checking if a rectangle (your water) contains another rectangle (your sprite). If you use non-rectangle shapes, you should find the appropriate methods to check if the hitbox of your sprite is inside your water.
If your water contains your sprite, you can handle (update, render, etc.) it differently.

Libgdx Individually reference textures

In my game I have some gold that the player can collect. My problem is I want to individually refernce textures of gold so i can take that particular instance of gold off the screen (player picked it up). How would I go about doing this as i am pretty sure you cannot do this with regular textures. Would i have to create a shape and fill it with the gold texture so I can delete that particualr gold piece? Thanks
I think you confuse Texture (which is basically a loaded image that you can draw) with game entities. Depending on how you implement your game, you can spawn multiple bodies (Box2D), actors (Scene2D) or your simple data containers (width, height, x, y) representing each coin and draw each one on the screen using the same texture. Actually, that's the preferred way to handle assets: you load images once and then simply reuse them for each sprite.
I suggest looking into the following classes:
Sprite (makes it easier to draw textures).
Image (allows to display simple images on Scene2D stage).
Box2DSprite (third party utility class that makes it easier to draw Box2D worlds).

Drawing shapes by pixel in libgdx

I'm trying to draw shapes in libgdx that change constantly so I don't think using sprites will work, and I was wondering if it is possible to draw a shape using a function that is called for each pixel to determine if it should be drawn.
What I need to draw is part of a washer (an area bound by two concentric circles and two radiuses), with the circles and radiuses changing constantly.
What I want to know is wether here is a way to draw complex shapes that are determined by a function (the shape would consist of those (x,y) for wich theFunction(x,y)=true) instead of an image
Everything is possible. The best solution really depends on the details of what you want to create. Perhaps you can show an example of what you mean?
Without seeing an example, it looks you might want to start with ShapeRenderer. See the javadocs for detailed information on how to use it. That should get you started and if you find it to be insufficient in some way then you at least you have a more specific question.
Btw, using a Pixmap as suggested by #Ludevik is also possible, but since you want it to change constantly that would imply uploading the entire image each frame which is not very performant.
Would use of Pixmap help?
You can create a pixmap:
Pixmap pixmap = new Pixmap(300, 300, Pixmap.Format.RGBA8888);
and then draw pixels with specific color on that pixmap:
pixmap.drawPixel(x, y, color);
Then you can then create a texture from that pixmap and draw the texture. I'm not sure about the performance of such solution though.
See also Pixmaps in the libgdx wiki.

How to create a circular constrained paddle using Box2D?

I am trying to create the ball and paddle setup just like in Circle Pong. It is basically a paddle which revolves about a circular path as if it is tied by a rope to the center of the circular orbit.
here is a video of Circle Pong: https://www.youtube.com/watch?v=9sitwBolywY
I want to use Box2D for the physics. How do I constrain the paddle to move in a circular orbit? I might even want to have multiple paddles for a circular orbit (with a fixed angular distance from each other).
A distance joint with a static central body won't work because the central body might obstruct / come in the way of the ball. Also it won't work with multiple paddles.
Any ideas?
for the first I'm not sure if you need physics engine in game like this at all - it seems to be such simple that simple scene2d actions + setting origins should be enough - please notice that it would improve your game performance and make it easier to deploy.
But - if you are sure you want to use box2d please consider using distance joint with a body which has a fixture with isSensor = true. It won't take part in collision calculations, letting you to achieve your point.
FixtureDef fixtureDef = yourMaterial;
fixtureDef.shape = yourShape;
fixtureDef.isSensor = true;

Is it possible to create a visualization like this in WebGL?

This visualization is created using Away3D (Flash):
www.guardian.co.uk/world/interactive/2011/mar/22/middle-east-protest-interactive-timeline
Would it be possible to create something of the same quality (re. interaction, animation, performance, pixel-perfection etc.) using WebGL?
Bonus: How would one set up the basic scene? (without interaction and animation)
Yes, it would be. The scene is not complex at all, so it would have good performance. Interaction, yes, depends how you implement it, but if you are doing project with, for example, Three.js it wouldn't be a problem. Pixel perfection, obviously, yes.
In the scene, you could have curved plane, with texture that is changing UVs of vertices when you use 'navigate'. Pins could be done with particle, or better, simple quads with transparent textures. To have pixel-precise pin picking, you could depth-test pins on ray trace, or use pin-shaped geometry with shader, which probably the best solution.
So, basic scene - curved plane with per-vertex-changing-UV, pin-shaped and texture pins.
Alternatively, you could do exactly the same thing with 2D canvas. All elements are just drawn and scaled, text would be much simpler, and picking would be with 2d calculations.
Hope this helps.