Made this with nxPascal, using custom made GLSL 3D-shader: (Changed to improved version of the video)
Screenshot of earlier version: http://img32.imageshack.us/img32/2327/particles.png
Each particle is now drawn with size 5 GL_POINTS. I used total 3000 particles, and any more would make it lag more horribly than it currently is. It's not about rendering though, as seen in the screenshot. I initialized it with 200000 particles and it wasn't even breaking a sweat at the vsync'd 63 fps with 0ms render time.
So do you know how i can optimize this gravity calculation?
Code:
function Hypot3f_2(const x, y, z: single): single; inline;
begin
result:=sqrt(x*x+y*y+z*z);
end;
function VectorDist_2(const a, b: PVector): single; inline;
begin
result:=hypot3f_2(a^.x-b^.x, a^.y-b^.y, a^.z-b^.z);
end;
procedure TGame.GameLoop;
var i, j: integer; d, f: single; vec: TVector;
start: cardinal;
begin
start:=nx.GetTick;
... // Quick interpolation of camera, zoom etc...
if not paused then begin
// Update forces
for i:=0 to count-2 do
with star[i] do
for j:=i+1 to count-1 do begin
d:=vectordist_2(@v, @star[j].v);
// If particles exactly overlap, rather keep current velocity this frame
if d>0 then begin
f:=0.00001/(d*d);
d:=f/d;
if star[j].positive<>positive then d:=-d;
vec.x:=(star[j].v.x-v.x)*d;
vec.y:=(star[j].v.y-v.y)*d;
vec.z:=(star[j].v.z-v.z)*d;
movement.x:=movement.x+vec.x;
movement.y:=movement.y+vec.y;
movement.z:=movement.z+vec.z;
star[j].movement.x:=star[j].movement.x-vec.x;
star[j].movement.y:=star[j].movement.y-vec.y;
star[j].movement.z:=star[j].movement.z-vec.z;
end;
end;
// Move particles
for i:=0 to count-1 do
with star[i] do begin
v.x:=v.x+movement.x;
v.y:=v.y+movement.y;
v.z:=v.z+movement.z;
end;
end;
looptime:=nx.GetTick-start;
end;
Bookmarks