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.
Related
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();
}
i am learning libgdx. i want to create a 2d fps game using libgdx, which is basically no physics environment. in that case lot of collision will be there (for example lot of bullets hitting multiple objects). i need to check intersecting of every sprite's rectangle with other sprites. in this way there may be thousand sprite's, checking every sprite with other 999 sprites in every time isn't definitely not a good idea.
libgdx has scene2d which has hit detection api's, is that solve my problem?
or
i should use box2d collision detection?
or
is there any other way to detect collision in libgdx?
i want to create a 2d fps game
As much as i know FPS means "First-Person-Shooter" -> a shooter with the first person perspective. That means, that it is 3D.
So pleas clarify what you mean.
Now to the collision detection question:
Scene2Ds hit detection is used for input like touch or
mouseevents. So it is used to detect, if an Actor is touched,
clicked...
Box2D is a 2D physic-engine which can not only do collision
detection for you, but it can also do the collision response as well
as the physics simulation (gravity and things like that).
Libgdx offers the Intersector class, which you could use for
overlap tests. The organization/the management of the collision
detection is up to you.
It would be better if you tell us how your game should work, how your world should be managed etc.
For example, if your world is tilebased, the collision detection between wall and player could be a simple check, if the tile is occupied -> collision, if not -> no collision.
Also it is important to know the shapes of your objects. A rectangle-rectangle collision detection is different from a rectangle-circle collision detection.
So now there are 3 solutions for your problem:
- Use Box2D and read tutorials on how to use it
- Do some research on collision detection, read tutorials and take a look at some sample projects
- Give us more informations about the game, its objects, their shapes etc.
I can do 2d collision detection of boxes and circles ,but I have a question . How can I do collision detection in this situation (1,2).I am using libGDX game engine for creating games.
Fixture (1) in your world should most likely be a ChainShape. ChainShapes are determined by an array of Vector2 objects, the more you have, the smoother the ChainShape tends to be.
Without very tedious scripting, making a fixture from an image is done by putting vectors at relatively close points to make a shape that looks similar to the image.
Further information on how to create a ChainShape can be found here.
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.
I'm trying to make it so when a sprite which is attached to a physics body overlaps another sprite on the level something happens. The second sprite is NOT attached to a physics body.
More specifically when the two sprites are overlapping I want the game to constantly check to see if the distance between the centers of the sprites is less than a certain amount or not. Then if the distance is small enough something will happen.
I'm trying to use collision checking as a way of optimizing the game so it doesn't have to constantly check distances between every single object of type A and B even if they're not even close. It will only check the distance when they're close enough to be overlapping.
Now what I am wondering is how can I do this? Is there a way to check collisions between sprites as part of AndEngine? Or would it be easier to attach a physics body to the second object also and then just use physics collision detection? But then if I do that can I make it so the collision will be detected but they won't actually physically "collide"?
Yes, see CollisionDetectionExample.java. I suggest you download the whole examples package, it is very useful in the absence of any documentation for AndEngine. Please note that collision detection is not pixel perfect, so it will still detect collisions of transparent parts of the Sprites. There is a library for that, but I am afraid it is outdated.