Well, I am actually working on some 2d physics experiments.
So far, the physics engine is doing well. Actually, I've reached the point where I have to deal with collisions detection/resolution.
First of all, my simulation is only about particles (for now). Assume a particle has:
- velocity, position and acceleration vectors
- a mass,
- a radius (to be drawn as full circles shapes)
All generated particles are listed in a simple array (Simulation.particles).
For each simulation timestep, I am using a kind of timed-controlled update system, inside love.update, as follows: (dt is provided by love.update callback, and is about 0.004 on my computer):
Code: Select all
function Simulation:update(dt)
if dt <= 0 then return end
self.__buffer = self.__buffer+dt
local step = 0
while (self.__buffer >= self.timestep and step < self.__steps) do
step = step+1
self:integrate(dt)
self.__buffer = self.__buffer - dt
end
end
Then comes the part where I have to deal with collision/detection and resolution.
What I did first:
The first approach to this was a naïve attempt:
Code: Select all
local particle
for i = 1,#self.particles do
particle = self.particles[i]
-- apply impulses
-- update particle
for j = i+1,#self.particles do
-- check for collision for the pair i,j
-- resolve it
end
end
But well, everything went right...for a simulation containing up to 100~300 particles.
But above this limit, I encountered severe framerate drops.
So, I said to myself: "don't bash Löve for that, as your approach may not be the best one."
The second approach:
So i figured out a way to narrow down the number of collision checks.
After making some search, I came accross space partitionning techniques. Well, there are lots of techniques, and making a quick review, it seemed to me spatial hashing was the simplest one.
Implementating won't be a problem (hopefully), but I have some of questions about it, before.
First, it is obvious that spatial hashing works well when the grid cell size parameter is well tuned.Not too high, but not too low.
So does this means I have to impose a maximum radius size to my particles ? So how would be the correct approach to this problem .
Second, maybe some of you have experienced different techniques (quadtrees, octrees, bsp, sweep and prune). Would you really recommend spatial hashing to me, based on your experienced and on what i am aiming to accomplish ?
Third, is it really safe to perform collision detection/resolution while updating particles ?
The alternative would be looping through the particles list, updating each one, and collect particles pairs colliding inside an array.
Then loop trhrough this array to resolve each collision (something that I have to experiment, also).
Okay, there are some others questions left, but i'll stop from now. I may be overthinking on some points, but that mostly due to my lack on experience on the topic. So i'll be happy to have some advises.
Thanks in advance.