whats the best way to make general colision detection?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
edu
Prole
Posts: 7
Joined: Sat Apr 06, 2024 2:21 pm

whats the best way to make general colision detection?

Post by edu »

im making a space invaders like game, and im having some issues with colision detection.
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

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

Post 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.
obey
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

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

Post 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.
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

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

Post 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.
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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
User avatar
Rondu
Prole
Posts: 7
Joined: Mon May 27, 2024 2:52 am

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

Post 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!
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 18 guests