How would I go about checking collisions for the player with an object that is declared in main or some other file?
On top of that, how would I go about creating a parent class like "collidable" to handle all the objects the player could collide with.
To clarify, I know the formula for collision (I believe), I'm just not sure where to implement it.
I'm new to Love, coming from Gamemaker.
Thank you!
Implementing collisions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Implementing collisions
Hello.
The approach I would recommend is:
1. Check if the player intersects with the environment
2. Move the player so that he's not intersecting with the environment. Find the shortest separation vector (collision normal and penetration depth).
3. Adjust the player's velocity according to friction and restitution. You need to understand the role of these two coefficients and use them with the collision normal.
Better yet, use love.physics if you're unsure how to implement the code yourself.
That's one way to do it. Personally I recommend moving your collision code into a separate module. This way your game logic is less dependent of the collision code.creating a parent class like "collidable" to handle all the objects the player could collide with
I'm afraid there is no single formula. It becomes increasingly more complicated depending on the "level of realism" you want.To clarify, I know the formula for collision
The approach I would recommend is:
1. Check if the player intersects with the environment
2. Move the player so that he's not intersecting with the environment. Find the shortest separation vector (collision normal and penetration depth).
3. Adjust the player's velocity according to friction and restitution. You need to understand the role of these two coefficients and use them with the collision normal.
Better yet, use love.physics if you're unsure how to implement the code yourself.
Re: Implementing collisions
You will need some central place that holds all the objects that can collide. In the simplest case, this is just a global table. In love.update(), you iterate over all the objects and check for collisions. You probably have something like this for drawing your objects:
Another method would be to use the signal/observer pattern, where you have all the collidables subscribe to a 'check-collision'-signal (or similar). For each object you want to collide, you simply emit the signal, e.g.:
Note that both solutions are equivalent in terms of computational complexity. The second version just hides the global table.
Code: Select all
for obj1 in pairs(entities) do
for obj2 in pairs(entities) do
if obj1 ~= obj2 and do_collide(obj1, obj2) then
do_stuff()
end
end
end
Code: Select all
-- in collidable constructor
signal.register('check-collision', function(other)
if self ~= other and do_collide(obj1, obj2) then
do_stuff()
end
end
-- in collidable update
signal.emit('check-collision', self)
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 8 guests