I am attempting to use the DarkPhysics engine to simulate particles with differing charges, that effectively apply forces on each other when they get too close. So far, mostly working.
The snag that I have run into, is that I need a fast solution for applying a linear force (appropriate Linear Velocity/Momentum) through "PHY ADD RIGID BODY LOCAL FORCE" which under the right circumstances would cause particle A to orbit around particle B. Under the wrong circumstances it would simply fling off.
More specifically, and simply put, I am trying to calculate the amount of Velocity in X,Y,Z directions that I need to apply, to get the particle to begin orbiting its companion in a mostly unguided manner. I want the physics engine to be in control of this relationship because I want to be able to hit those particles with other particles and break them apart, etc.
I have grabbed some example code from a few other threads, but I'm still not sure how to make this work through the physics engine, since every example I've found has DBP manipulating the object directly (Which I cannot have). In the code provided I am hung up at the function ForceOrbit() Since that is the function applying this linear force to the particles. I can't quite figure out how or where I am supposed to get AngX or AngY, since the particles can enter and leave the influence of the other particles at any time. I have no array set up to track each particles' rotation around the other, and would prefer not to have one. Is that the missing link, is it necessary for this kind of function? Any other suggestions?
Small snippet of the code I am using here;
`A little background, object numbers 7-300 represent smaller particles which orbit the larger ones
`Which are object numbers 301 to 350
for b = 301 to 350
if object exist(b)=1
global bb# as integer = 0
bb#=FindNearestObject(7,300,object position x(b),object position y(b),object position z(b))
if bb#>0
ForceOrbit(b,bb#,nearestDistance#)
endif
lvx#=phy get rigid body linear velocity x(b)
lvy#=phy get rigid body linear velocity y(b)
lvz#=phy get rigid body linear velocity z(b)
lmx#=phy get rigid body linear momentum x(b)
lmy#=phy get rigid body linear momentum y(b)
lmz#=phy get rigid body linear momentum z(b)
`Do Nothing, but help to keep them constant
phy set rigid body linear velocity b,lvx#,lvy#,lvz#
phy set rigid body linear momentum b,lmx#,lmy#,lmz#
phy set rigid body angular velocity b,lvx#,lvy#,lvz#
phy set rigid body angular momentum b,lmx#,lmy#,lmz#
endif
next b
function FindNearestObject(startObj,endObj,x#,y#,z#)
nearestObj = -1
global nearestDistance# as integer = 50
for obj = startObj to endObj
if object exist(obj)
xdist# = object position x(obj) - x#
ydist# = object position y(obj) - y#
zdist# = object position z(obj) - z#
set vector3 1,xdist#,ydist#,zdist#
distance# = length vector3(1)
if distance# < nearestDistance#
nearestObj = obj
nearestDistance# = distance#
endif
endif
next obj
endfunction nearestObj
Function ForceOrbit(Origin#,Orb#,Dist#)
x1#=object position x(Orb#)
y1#=object position y(Orb#)
z1#=object position z(Orb#)
x2#=object position x(Origin#)+sin(AngY)*cos(AngX)*Dist
y2#=object position y(Origin#)-sin(AngX)*Dist
z2#=object position z(Origin#)+cos(AngY)*cos(AngX)*Dist
Endfunction