collision detection, in libgdx - java

I am new to the world of libgdx and the world of game programming in general. I want to create a game, but not any game. I have created some basic game like breaks, and pong. But I still cant go any further, I google for good articles, but I always have problems with collision, especially between entities! I want to create a game with slopes like sonic.

Why not use Box2D (libGDX extension) ? It's perfect for platformers.

Do you know how to create rectangles. I assume that you know about rectangles.
if you want to check collision of two rectangles you can do as follows:
Rectangle a = new Rectangle(), b = new Rectangle();
in constructor set rectangles
a.setRectangle(yourX, yourY, yourWidth, yourHeight);
b.setRectangle(yourX, yourY, yourWidth, yourHeight);
in render check collision like this:
if(a.overlaps(b))
{
//do your work
}

U can use OverlapTester class given in SuperJumper Project by LibGdx

Create Your bounds using rectangle class in Libgdx and test them using Intersector class.
This class has many function to test overlapping of rectangles, circles etc..

I recommend you to use box2d if you know the basics.
if you know how to use a rectangle, sprite batch, camera etc. Then you should proceed to Box2d if you don't just take some good tutorial and try to make the application without any extension.this will make your concept clear and you will easily able to grasp the logic behind the game.

Related

Game Development, Collision

I'm new to the game development and I'm trying to code 2D RPG game. im using png images to create objcts and move around. So it is pretty easy to detect collision between rectngles and other simple shapes like this.
if(object1.collides(object2)){
}
what is best way to detect collision of image objects like player, or npc?
The most common way is to create a hitbox that is the approximate shape of your object. The hitbox can be very simple like a rectangle or circle around your object, which is actually invisible.
Have a look at this example, the black box is the hitbox of Mario and you only need to check its boundaries:
But if you really need precision, then you will need to go through the pixels of an image and create the custom polygon. However, the more complex your polygon is, the harder it is to detect collision. Most of the time, game developers rely on physics engines to deal with it.

Libgdx/Java 2D Duengon collision and collision from all sides

im currently developing a small game, a duengon rpg crawler.It all works fine and today i tried to implement a collision mechanic, because you shouldnt go trough walls... The map is completly randomly generated.
I havent found any good tutorials how to setup a collision mechanic for libgdx or java, just something like this :
Rectangle rect = new Rectangle();
rect.overlaps(r2);
That worked yes, but it didnt showed me, where the Rectangle colides with the other one. All i wanna have, is a good tutorial about java or libgdx, how to make a all 4 sides collision, step by step :) Or an example...
Thanks for your attention and your help.

Physics-based/Polynomial Animation with Box2d (Java)

I'm creating a 2d game using the Java version of Box2d.
I know that the engine is generally paired with a separate library to handle animation, but I'm looking for something simple, with a more rustic feel, so I'm considering handling the animation from scratch.
The animation I am trying to create is something for the collision of two objects.
I'm using openGl to handle the graphics, and I pretty much have everything set up with the fps and rendering, it's just a matter of tweaking the frames to make it look smooth.
I tried bliting images of some things that I drew myself and it looked atrocious. I think the main problem is that the animation frame images are not linked to the physics forces and so they don't blend well.
What I want to try next is a sort 'spark effect' using a small number of pixels that sort of resemble 'dust' exploding off of the colliding images. If you've ever played dxball, you may recall a similar effect when the ball collided with the paddle at high speed.
As you can see in the above image, the principles are fairly simple. I figure by creating a set of small bodies with fixtures, I can use the box2d engine to produce this effect. I already have code that grabs the point of collision for all bodies, and am familiar enough with parabolic functions that I pretty sure I can create the force direction.
I'm just not sure if there is a better way of doing this, or how to apply a polynomial function to a body like in the image.
Does anybody know how to achieve this functionality with box2d? Any built in methods or documentation that would be helpful?
As a point of reference, the following snippet of code demonstrates the drawing of a small, red box on all of the collision points.
for(Vec2 point : listener.worldmanifold.points){
glPushMatrix();
newpos = point.mul(30);
glTranslatef(newpos.x, newpos.y, 0);
glColor3f(1,0,0);
glRectf(-2, -2, 2, 2);
glPopMatrix();
}

libgdx ragdoll physics with animation

I'm in the planning stages of game dev. and am wondering whether or not to pick unity or Libgdx. I already know quite a bit of java, and I want to make a 2D sidescroller that uses ragdoll physics with animation. I have been looking pretty hard, but haven't found a good resource for this yet with Libgdx. It seems possible with Unity though. The resources I have found so far with Libgdx are :
Rube
Glee2d
Spine
Blender
So far with this it seems possible to make a level with a skeletal 2d animating sprite, but is it possible to apply ragdoll physics to this creation so, for example, if an enemy was shot it would react like a ragdoll?
I appreciate any help!
Thanks,
You could clearly use libgdx for that.
You'd have to create the ragdoll as box2d bodies (which can be done with rube). For rendering the ragdoll, you could use the setUserData() methods in box2d to bind an actor to the body/fixture. After each simulation step you would update the actors and create the proper rendering.
You could then place the ragdoll somewhere and shoot it with some other box2d body, let's say a cannonball...

JBox2D - Drawing with DebugDraw

How do you draw elements with JBox2D? I'm fine using the DebugDraw, I just want to find a quick way to do this in Java since I haven't worked much with graphics.
Do I need to use a Canvas? Or a JFrame? And how does the world know when I call
world.drawDebugData()
where to draw it to?
How could I devise a class that just drew points where I want them, and integrate this with JBox2D?
...
while(true)
world.step(timeStep, velocityIterations, positionIterations);
Vec2 position = body.getPosition();
float angle = body.getAngle();
System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
I imagine I could put this code somewhere inside this while loop, but I'm not exactly sure where. Is there a function that's called every time a World steps? Can I put a draw function in there? I'm even ok using the DebugDraw if I could figure it out...
Could you please help me understand what kind of class would take all the objects in the World object and draw them continually? (and where to draw to?)
You can easily create new testbed tests, this is the best way to test out your new physics ideas and not worry about drawing and GUI stuff. It even supports adding extra real-time options. Check it out here:
https://code.google.com/p/jbox2d/wiki/Testbed
It's difficult to know where to organize everything if you're not used to programming games and physics engines.
Dyn4j is another great physics library that has great examples explaining where to put everything and even how the user can interact with the world. A lot about the dyn4j library is identical to box2d/jbox2d, so if you just try out their examples real quick, it will lend a hand to figuring out how to render graphics in box2d.

Categories

Resources