This is the only part where thread collisions can happen. It is possible that some force vectors are not added to movement, but is that likely?
Code:
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;
Also the mainloop's part for threads is like:
Code:
i2:=count div 4;
i3:=i2*2;
i4:=i2*3;
TPhysicsThread.Create(self, 0, i2-1);
TPhysicsThread.Create(self, i2, i3-1);
TPhysicsThread.Create(self, i3, i4-1);
TPhysicsThread.Create(self, i4, count-2);
while threads>0 do begin
sleep(1); // It takes usually over 140ms, so sleep here is not unnecessary
Application.ProcessMessages;
end;
...
constructor TPhysicsThread.Create(parent: TGame; const first, last: cardinal);
...
Thread execute:
...
for i:=FFirst to FLast do
with star[i] do
for j:=i+1 to count-1 do begin
So it will not stop responding. I know it could be done better though, even so that fps wouldn't be affected. I also realize now that all the threads aren't getting equal workload at all. Especially the first thread gets biggest work, and 4th one the smallest.
Win32-binaries and source code here: https://docs.google.com/file/d/0B7FI...it?usp=sharing
Requires Lazarus and nxpascal to compile, possibly SVN version. All the required files are here:
https://code.google.com/p/nxpascal/s...%2Ftrunk%2Fsrc
Bookmarks