whats the best way to make general colision detection?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
whats the best way to make general colision detection?
im making a space invaders like game, and im having some issues with colision detection.
- BrotSagtMist
- Party member
- Posts: 659
- Joined: Fri Aug 06, 2021 10:30 pm
Re: whats the best way to make general colision detection?
Thats a broad question and largely depends on what type of game, how many objects are on the screen or even how many pixels the game has.
Losely there are 3 approaches to this:
Mapping based - Where the entire viewable area is mapped to a table, that means around 10-100 pixel are grouped together as one field, an object puts itself in and collidables only check if their part of the map is taken. This is mostly default for tile based games and old school rpgs but also works well on retro games, lightweight and can handle lots of objects.
But it gets very resource heavy when the fields are too small which makes it inaccurate and limiting the form of stuff to display.
Distance based - A colidable, say a bullet, checks its distance to every object at the screen every frame and triggers when it is close.
Line crossing based- booth the above have a mayor weakness of flying though an object if it moves too fast whitin a frame, that means usually if it needs less than 1/60 of a second to pass through it it is not detected. Therefore the professional way is to not check if the bullet collides but if the path of the bullet crosses a target.
Frankly thats quite a bit too complicated for an easy game like space invaders unless your bullets are meant to go in real speed.
Personally i think distance based would work well on space invaders. Unless youre doing a bullet hell with hundrets/thousands of em on the screen which makes maps a good option.
Losely there are 3 approaches to this:
Mapping based - Where the entire viewable area is mapped to a table, that means around 10-100 pixel are grouped together as one field, an object puts itself in and collidables only check if their part of the map is taken. This is mostly default for tile based games and old school rpgs but also works well on retro games, lightweight and can handle lots of objects.
But it gets very resource heavy when the fields are too small which makes it inaccurate and limiting the form of stuff to display.
Distance based - A colidable, say a bullet, checks its distance to every object at the screen every frame and triggers when it is close.
Line crossing based- booth the above have a mayor weakness of flying though an object if it moves too fast whitin a frame, that means usually if it needs less than 1/60 of a second to pass through it it is not detected. Therefore the professional way is to not check if the bullet collides but if the path of the bullet crosses a target.
Frankly thats quite a bit too complicated for an easy game like space invaders unless your bullets are meant to go in real speed.
Personally i think distance based would work well on space invaders. Unless youre doing a bullet hell with hundrets/thousands of em on the screen which makes maps a good option.
obey
Re: whats the best way to make general colision detection?
Either go with simple AABB or use something in the middle such as Hardon Collider https://vrld.github.io/HardonCollider/tutorial.html or go the hard way with Love physics module https://love2d.org/wiki/love.physics.
AABB should work well for simple games such as Space Invaders.
But first: be specific in your question and provide the code.
AABB should work well for simple games such as Space Invaders.
But first: be specific in your question and provide the code.
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: whats the best way to make general colision detection?
@edu you're asking two different things: "how are collisions usually done in games", and "why is this thing in my game not working".
The answer to the first is: usually you approximate the shape of complex objects with simpler geometrical primitives (like the boxes, circles/ellipses and lines that Brot mentioned), and then test for the geometrical intersections of those primitives. Most of the time it works fantastic, even with something as simple a bounding boxes like dusoft wrote.
The answer to the second question is: there's not enough information to answer. We'd need to know how it is failing, what you were trying to do, and what's your code implementation of it.
The answer to the first is: usually you approximate the shape of complex objects with simpler geometrical primitives (like the boxes, circles/ellipses and lines that Brot mentioned), and then test for the geometrical intersections of those primitives. Most of the time it works fantastic, even with something as simple a bounding boxes like dusoft wrote.
The answer to the second question is: there's not enough information to answer. We'd need to know how it is failing, what you were trying to do, and what's your code implementation of it.
Re: whats the best way to make general colision detection?
For Space Invaders I would just use a isPointInRectangle() check
The projectiles are points and the player's ship, "shield walls" and enemies are rectangles.
The projectiles are points and the player's ship, "shield walls" and enemies are rectangles.
Re: whats the best way to make general colision detection?
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: whats the best way to make general colision detection?
--two tables in love.load!
p1 ={x =mx,y =my ,h =40 ,w =42 }--person(4points)
d1 ={x =28, y=400, h =75, w =750} --dragon(4points)
--check in love.draw
if (p1.y + p1.h > d1.y ) and ( p1.y < d1.y + d1.h ) and
(p1.x + p1.w > d1.x ) and ( p1.x < d1.x + d1.w ) then
love.graphics.print('person and dragon in same space!',10,10)
end
*when you draw the dragon and person give them the p1 or di table indexes!
p1 ={x =mx,y =my ,h =40 ,w =42 }--person(4points)
d1 ={x =28, y=400, h =75, w =750} --dragon(4points)
--check in love.draw
if (p1.y + p1.h > d1.y ) and ( p1.y < d1.y + d1.h ) and
(p1.x + p1.w > d1.x ) and ( p1.x < d1.x + d1.w ) then
love.graphics.print('person and dragon in same space!',10,10)
end
*when you draw the dragon and person give them the p1 or di table indexes!
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests