Answer the question
In order to leave comments, you need to log in
How are collisions handled in LibGDX?
Hello.
There is an idea to create a 2D toy for one organization.
Can you please tell me how collisions are handled in LibGDX? What classes are used to check for collisions?
For example, I have rectangular random objects scattered all over the map, and I need to find out which of the objects intersect, while rotation of the objects is possible ...
Thank you.
Answer the question
In order to leave comments, you need to log in
1. If the game does not use physical. Box2D engine, then the collision is determined by simple x, y checks of the two objects. Those. a loop is built:
for(int i=0; i second_object.x && first_object.x < second_object.x + second_object.width && firs_object.y + first_object.height > second_object.y && first_object.y < second_object.y + second_object. height)
collisia = true;
}
}
But this method is not always suitable, and if you are creating a platformer, then most often it is better to use the Box2d engine. It is in your cases with rotation that it is better to use it.
2. Box2d - suvitruf.ru/2012/12/22/2764
habrahabr.ru/post/162079
(like from one author)
libGDX has a built-in 2D physics engine, Box2D. He just the same and can handle collisions. Google how to work with it.
The written function for checking collisions of a rectangle with a rectangle helped me ... Here:
// The function for checking for collisions
function checkForCollision(obj1, obj2){
if ((obj1.x < obj2.x + obj2.width) && (obj2.x < obj1 .x + obj1.width) && (obj1.y < obj2.y + obj2.height) && (obj2.y < obj1.y + obj1.height)) return true;
return false;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question