Page 1 of 1

whats the best way to make general colision detection?

Posted: Wed May 15, 2024 4:28 pm
by edu
im making a space invaders like game, and im having some issues with colision detection.

Re: whats the best way to make general colision detection?

Posted: Wed May 15, 2024 5:30 pm
by BrotSagtMist
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.

Re: whats the best way to make general colision detection?

Posted: Wed May 15, 2024 7:35 pm
by dusoft
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.

Re: whats the best way to make general colision detection?

Posted: Thu May 16, 2024 5:56 am
by RNavega
@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.

Re: whats the best way to make general colision detection?

Posted: Thu May 16, 2024 11:00 am
by knorke
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.

Re: whats the best way to make general colision detection?

Posted: Fri May 17, 2024 8:01 am
by dusoft
knorke wrote: Thu May 16, 2024 11:00 am 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.
knorke meant e.g. this:
https://love2d.org/wiki/PointWithinShape

Re: whats the best way to make general colision detection?

Posted: Mon May 27, 2024 7:25 pm
by Rondu
--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!