Update: 16 Jan 2007
Just the updated code. This works pretty seamlessly.
function kx_startKinetix()
Global kxcontvec as integer
Global kxworlddata as tkxworlddata
dim kxbody(0) as tkxbodydata
dim kxmat(0) as tkxmatdata
dim kxvec(0) as vec3
endfunction
function kx_setupWorld(grav as float, dens as float)
kxworlddata.grav=grav
kxworlddata.dens=dens
endfunction
function kx_makeVector()
local vec as integer
array insert at bottom kxvec() : vec=array count(kxvec())
endfunction vec
function kx_setVector(vec,x as float, y as float, z as float)
kxcontvec=vec
kxvec(vec).x=x
kxvec(vec).y=y
kxvec(vec).z=z
endfunction
function kx_setCurrentVector(vec)
kxcontvec=vec
endfunction
function kx_createMaterial(fric as float, cobj)
local mat as integer
array insert at bottom kxmat() : mat=array count(kxmat())
kxmat(mat).fric=fric
kxmat(mat).cobj=cobj
kxmat(mat).coll=1
endfunction mat
function kx_createBody(obj, mode, mass as float, dragc as float, dens as float, mat)
local body as integer
array insert at bottom kxbody() : body=array count(kxbody())
kxbody(body).obj=obj
kxbody(body).mass=mass
kxbody(body).dragc=dragc
kxbody(body).dens=dens
kxbody(body).size=object size(obj)
kxbody(body).mat=mat
kxbody(body).cobj=kxmat(mat).cobj
kxbody(body).mode=mode
kxbody(body).pos.x=object position x(obj)
kxbody(body).pos.y=object position y(obj)
kxbody(body).pos.z=object position z(obj)
sc_setupComplexObject obj,0,2
sc_updateObject obj
set object rotation zyx obj
endfunction body
function kx_setBodyForce(body)
kxbody(body).frct.x=kxvec(kxcontvec).x
kxbody(body).frct.y=kxvec(kxcontvec).y
kxbody(body).frct.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyTorque(body)
kxbody(body).trqt.x=kxvec(kxcontvec).x
kxbody(body).trqt.y=kxvec(kxcontvec).y
kxbody(body).trqt.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyVelocity(body)
kxbody(body).vel.x=kxvec(kxcontvec).x
kxbody(body).vel.y=kxvec(kxcontvec).y
kxbody(body).vel.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyOmega(body)
kxbody(body).omega.x=kxvec(kxcontvec).x
kxbody(body).omega.y=kxvec(kxcontvec).y
kxbody(body).omega.z=kxvec(kxcontvec).z
endfunction
function kx_updateBody(body)
kxbody(body).mang.x=atanfull(kxbody(body).vel.y,kxbody(body).vel.z)
kxbody(body).mang.y=atanfull(kxbody(body).vel.x,kxbody(body).vel.z)
kxbody(body).mang.z=atanfull(kxbody(body).vel.x,kxbody(body).vel.y)
kxbody(body).fdir.x=sin(kxbody(body).ang.y)*cos(kxbody(body).ang.x)
kxbody(body).fdir.y=-sin(kxbody(body).ang.x)
kxbody(body).fdir.z=cos(kxbody(body).ang.y)*cos(kxbody(body).ang.x)
kxbody(body).mdir.x=sin(kxbody(body).mang.y)*cos(kxbody(body).mang.x)
kxbody(body).mdir.y=-sin(kxbody(body).mang.x)
kxbody(body).mdir.z=cos(kxbody(body).mang.y)*cos(kxbody(body).mang.x)
kxbody(body).frcw.x=0
kxbody(body).frcw.y=kxbody(body).mass*kxworlddata.grav
kxbody(body).frcw.z=0
kxbody(body).frcd.x=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.x^2)*sgn(kxbody(body).vel.x)))
kxbody(body).frcd.y=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.y^2)*sgn(kxbody(body).vel.y)))
kxbody(body).frcd.z=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.z^2)*sgn(kxbody(body).vel.z)))
kxbody(body).frc.x=kxbody(body).frct.x+kxbody(body).frcw.x+kxbody(body).frcd.x+kxbody(body).frcc.x+kxbody(body).frcs.x
kxbody(body).frc.y=kxbody(body).frct.y+(kxbody(body).frcw.y*(1-kxbody(body).ccoll))+kxbody(body).frcd.y+kxbody(body).frcc.y+kxbody(body).frcs.y
kxbody(body).frc.z=kxbody(body).frct.z+kxbody(body).frcw.z+kxbody(body).frcd.z+kxbody(body).frcc.z+kxbody(body).frcs.z
kxbody(body).acc.x=kxbody(body).frc.x/kxbody(body).mass
kxbody(body).acc.y=kxbody(body).frc.y/kxbody(body).mass
kxbody(body).acc.z=kxbody(body).frc.z/kxbody(body).mass
kxbody(body).vel.x=kxbody(body).vel.x+kxbody(body).acc.x
kxbody(body).vel.y=kxbody(body).vel.y+kxbody(body).acc.y
kxbody(body).vel.z=kxbody(body).vel.z+kxbody(body).acc.z
kxbody(body).speed=sqrt(kxbody(body).vel.x^2+kxbody(body).vel.y^2+kxbody(body).vel.z^2)
kxbody(body).pos.x=kxbody(body).pos.x+kxbody(body).vel.x
kxbody(body).pos.y=kxbody(body).pos.y+kxbody(body).vel.y
kxbody(body).pos.z=kxbody(body).pos.z+kxbody(body).vel.z
kxbody(body).trq.x=kxbody(body).trqt.x+kxbody(body).trqd.x+kxbody(body).trqc.x
kxbody(body).trq.y=kxbody(body).trqt.y+kxbody(body).trqd.y+kxbody(body).trqc.y
kxbody(body).trq.z=kxbody(body).trqt.z+kxbody(body).trqd.z+kxbody(body).trqc.z
kxbody(body).angacc.x=kxbody(body).trq.x/kxbody(body).mass
kxbody(body).angacc.y=kxbody(body).trq.y/kxbody(body).mass
kxbody(body).angacc.z=kxbody(body).trq.z/kxbody(body).mass
kxbody(body).omega.x=kxbody(body).omega.x+kxbody(body).angacc.x
kxbody(body).omega.y=kxbody(body).omega.y+kxbody(body).angacc.y
kxbody(body).omega.z=kxbody(body).omega.z+kxbody(body).angacc.z
kxbody(body).ang.x=kxbody(body).ang.x+kxbody(body).omega.x
kxbody(body).ang.y=kxbody(body).ang.y+kxbody(body).omega.y
kxbody(body).ang.z=kxbody(body).ang.z+kxbody(body).omega.z
if kxbody(body).mode=1
c=sc_sphereSlide(0,kxbody(body).pos.x-kxbody(body).vel.x,kxbody(body).pos.y-kxbody(body).vel.y,kxbody(body).pos.z-kxbody(body).vel.z,kxbody(body).pos.x,kxbody(body).pos.y,kxbody(body).pos.z,kxbody(body).size,kxbody(body).cobj)
if c>0
kxbody(body).cldd=c
kxbody(body).ccoll=1
else
kxbody(body).ccoll=0
kxbody(body).cldd=0
endif
if kxbody(body).ccoll=1
kxbody(body).pos.x=sc_getCollisionSlideX()
kxbody(body).pos.y=sc_getCollisionSlideY()
kxbody(body).pos.z=sc_getCollisionSlideZ()
kxbody(body).frcs.x=-(sc_getCollisionNormalX()*kxbody(body).frcw.y)
kxbody(body).frcs.y=-(sc_getCollisionNormalY()*kxbody(body).frcw.y)
kxbody(body).frcs.z=-(sc_getCollisionNormalZ()*kxbody(body).frcw.y)
kxbody(body).frcc.x=(sc_getCollisionNormalX()*kxbody(body).frc.x)
kxbody(body).frcc.y=(sc_getCollisionNormalY()*kxbody(body).frc.y)
kxbody(body).frcc.z=(sc_getCollisionNormalZ()*kxbody(body).frc.z)
else
kxbody(body).frcs.x=kxbody(body).frcs.x*0
kxbody(body).frcs.y=kxbody(body).frcs.y*0
kxbody(body).frcs.z=kxbody(body).frcs.z*0
kxbody(body).frcc.x=kxbody(body).frcc.x*0
kxbody(body).frcc.y=kxbody(body).frcc.y*0
kxbody(body).frcc.z=kxbody(body).frcc.z*0
endif
kxbody(body).mom.x=kxbody(body).mass*kxbody(body).vel.x
kxbody(body).mom.y=kxbody(body).mass*kxbody(body).vel.y
kxbody(body).mom.z=kxbody(body).mass*kxbody(body).vel.z
endif
position object kxbody(body).obj,kxbody(body).pos.x,kxbody(body).pos.y,kxbody(body).pos.z
rotate object kxbody(body).obj,kxbody(body).ang.x,kxbody(body).ang.y,kxbody(body).ang.z
sc_updateObject kxbody(body).obj
endfunction
function rotate_axis(obj,ax#,ay#,az#,amount#)
rem get normal vectors for X, Y, and Z axes of the object
x#=object position x(obj):y#=object position y(obj):z#=object position z(obj)
move object right obj,1
xx#=object position x(obj)-x#:xy#=object position y(obj)-y#:xz#=object position z(obj)-z#
move object left obj,1:move object up obj,1
yx#=object position x(obj)-x#:yy#=object position y(obj)-y#:yz#=object position z(obj)-z#
move object down obj,1:move object obj,1
zx#=object position x(obj)-x#:zy#=object position y(obj)-y#:zz#=object position z(obj)-z#
move object obj,-1
rem rotate the vectors
rem 1) rotate space about the x axis so that the rotation axis lies in the xz plane
xangle#=atanfull(ay#,az#)
xx1#=xx#
xy1#=xy#*cos(xangle#)-xz#*sin(xangle#)
xz1#=xz#*cos(xangle#)+xy#*sin(xangle#)
zx1#=zx#
zy1#=zy#*cos(xangle#)-zz#*sin(xangle#)
zz1#=zz#*cos(xangle#)+zy#*sin(xangle#)
ax1#=ax#
ay1#=ay#*cos(xangle#)-az#*sin(xangle#)
az1#=az#*cos(xangle#)+ay#*sin(xangle#)
rem 2) rotate space about the y axis so that the rotation axis lies along the z axis
yangle#=-atanfull(ax1#,az1#)
xx2#=xx1#*cos(yangle#)+xz1#*sin(yangle#)
xy2#=xy1#
xz2#=xz1#*cos(yangle#)-xx1#*sin(yangle#)
zx2#=zx1#*cos(yangle#)+zz1#*sin(yangle#)
zy2#=zy1#
zz2#=zz1#*cos(yangle#)-zx1#*sin(yangle#)
ax2#=ax1#*cos(yangle#)+az1#*sin(yangle#)
ay2#=ay1#
az2#=az1#*cos(yangle#)-ax1#*sin(yangle#)
rem 3) perform the desired rotation by theta about the z axis
xx3#=xx2#*cos(amount#)-xy2#*sin(amount#)
xy3#=xy2#*cos(amount#)+xx2#*sin(amount#)
xz3#=xz2#
zx3#=zx2#*cos(amount#)-zy2#*sin(amount#)
zy3#=zy2#*cos(amount#)+zx2#*sin(amount#)
zz3#=zz2#
rem 4) Inverse of step 2
xx4#=xx3#*cos(-yangle#)+xz3#*sin(-yangle#)
xy4#=xy3#
xz4#=xz3#*cos(-yangle#)-xx3#*sin(-yangle#)
zx4#=zx3#*cos(-yangle#)+zz3#*sin(-yangle#)
zy4#=zy3#
zz4#=zz3#*cos(-yangle#)-zx3#*sin(-yangle#)
rem 5) Inverse of step 1
nxx#=xx4#
nxy#=xy4#*cos(-xangle#)-xz4#*sin(-xangle#)
nxz#=xz4#*cos(-xangle#)+xy4#*sin(-xangle#)
nzx#=zx4#
nzy#=zy4#*cos(-xangle#)-zz4#*sin(-xangle#)
nzz#=zz4#*cos(-xangle#)+zy4#*sin(-xangle#)
rem calculate angle z
zr#=atanfull(nxy#,nxx#)
rem calculate angle y
yr#=-atanfull(nxz#,sqrt(nxx#^2+nxy#^2))
rem calculate angle x
cx#=-nxz#*nxx#
cy#=-nxz#*nxy#
cz#=1-nxz#*nxz#
c#=sqrt(cx#^2+cy#^2+cz#^2)
cx#=cx#/c#
cy#=cy#/c#
cz#=cz#/c#
xr#=acos(cx#*nzx#+cy#*nzy#+cz#*nzz#)
crosspx#=(nxy# * cz# - nxz# * cy#)
crosspy#=(nxz# * cx# - nxx# * cz#)
crosspz#=(nxx# * cy# - nxy# * cx#)
angle2#=acos(crosspx#*nzx#+crosspy#*nzy#+crosspz#*nzz#)
if angle2#>90
xr#=wrapvalue(-xr#)
endif
if xr#+1>xr# and yr#+1>yr# and zr#+1>zr#
rotate object obj,xr#,yr#,zr#
endif
endfunction
function kx_getBodyCollision(body)
coll=kxbody(body).cldd
endfunction coll
function kx_checkBodyCollision(a,b)
if kxbody(a).cldd=b then exitfunction 1
endfunction 0
function kx_getColliding(body)
local coll as boolean
coll=kxbody(body).ccoll
endfunction coll
function kx_getCollisionNormalX(body)
local nx as float
nx=kxbody(body).cnorm.x
endfunction nx
function kx_getCollisionNormalY(body)
local ny as float
ny=kxbody(body).cnorm.y
endfunction ny
function kx_getCollisionNormalZ(body)
local nz as float
nz=kxbody(body).cnorm.z
endfunction nz
function kx_getBodyVelocityX(body)
local vx as float
vx=kxbody(body).vel.x
endfunction vx
function kx_getBodyVelocityY(body)
local vy as float
vy=kxbody(body).vel.y
endfunction vy
function kx_getBodyVelocityZ(body)
local vz as float
vz=kxbody(body).vel.z
endfunction vz
function kx_getBodySpeed(body)
local sp as float
sp=kxbody(body).speed
endfunction sp
function sgn(value#)
if value#>=0 then exitfunction 1
endfunction -1
function startsparky()
make memblock 1,0
delete memblock 1
endfunction
`Impotant Types
Type vec3
x as float
y as float
z as float
endtype
type tkxworlddata
grav as float
dens as float
endtype
type tkxmatdata
fric as float
cobj as integer
coll as boolean
endtype
type tkxbodydata
obj as integer
pos as vec3
vel as vec3
acc as vec3
fdir as vec3
mdir as vec3
frct as vec3
frcw as vec3
frcd as vec3
frcc as vec3
frcs as vec3
frc as vec3
mom as vec3
mang as vec3
ang as vec3
omega as vec3
angacc as vec3
trqt as vec3
trqd as vec3
trqc as vec3
trq as vec3
mass as float
dens as float
mat as integer
dragc as float
size as float
speed as float
cobj as integer
cldd as integer
coll as vec3
cnorm as vec3
ccoll as boolean
mode as boolean
endtype
Yes people, yet another physics library, but this time it kicks the butt of every other one I've made. I'm totally serious.
Why did I make it?
I can't buy darkPhysics, and Newton... well, a bit complicated for me to bother with
(Sorry Newton people!)
What makes this one better than my others?
A very large shortage in commands, and a much bigger enlargement in functionality...
The functionality compared to my last ones?
- This one has more realistic physics, which means it has real drag forces, collision forces, etc. All controlled by things like densities of air and objects... etc...
- Collision! Woohoo!! Almost works perfectly (and realistically, i.e. uses forces to make the collision, and makes you slide down the slope). Sometimes it can get buggy, but rarely.
- Materials. You can apply them to objects, which say the friction of the objects, and whether they are collidable, and with which objects.
- Other stuff.
The commands?
function kx_startKinetix()
Global kxcontvec as integer
Global kxworlddata as tkxworlddata
dim kxbody(0) as tkxbodydata
dim kxmat(0) as tkxmatdata
dim kxvec(0) as vec3
endfunction
function kx_setupWorld(grav as float, dens as float)
kxworlddata.grav=grav
kxworlddata.dens=dens
endfunction
function kx_makeVector()
local vec as integer
array insert at bottom kxvec() : vec=array count(kxvec())
endfunction vec
function kx_setVector(vec,x as float, y as float, z as float)
kxcontvec=vec
kxvec(vec).x=x
kxvec(vec).y=y
kxvec(vec).z=z
endfunction
function kx_createMaterial(fric as float, cobj)
local mat as integer
array insert at bottom kxmat() : mat=array count(kxmat())
kxmat(mat).fric=fric
if cobj>0
kxmat(mat).cobj=cobj
kxmat(mat).coll=1
endif
endfunction mat
function kx_createBody(obj, mode, mass as float, dragc as float, dens as float, mat)
local body as integer
array insert at bottom kxbody() : body=array count(kxbody())
kxbody(body).obj=obj
kxbody(body).mass=mass
kxbody(body).dragc=dragc
kxbody(body).dens=dens
kxbody(body).size=object size(obj)
kxbody(body).mat=mat
kxbody(body).cobj=kxmat(mat).cobj
kxbody(body).mode=mode
kxbody(body).pos.x=object position x(obj)
kxbody(body).pos.y=object position y(obj)
kxbody(body).pos.z=object position z(obj)
kxbody(body).circ=kxbody(body).size*3.14
sc_setupComplexObject obj,0,2
endfunction body
function kx_setBodyForce(body)
kxbody(body).frct.x=kxvec(kxcontvec).x
kxbody(body).frct.y=kxvec(kxcontvec).y
kxbody(body).frct.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyTorque(body)
kxbody(body).trqt.x=kxvec(kxcontvec).x
kxbody(body).trqt.y=kxvec(kxcontvec).y
kxbody(body).trqt.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyVelocity(body)
kxbody(body).vel.x=kxvec(kxcontvec).x
kxbody(body).vel.y=kxvec(kxcontvec).y
kxbody(body).vel.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyOmega(body)
kxbody(body).omega.x=kxvec(kxcontvec).x
kxbody(body).omega.y=kxvec(kxcontvec).y
kxbody(body).omega.z=kxvec(kxcontvec).z
endfunction
function kx_updateBody(body)
kxbody(body).mang.x=atanfull(kxbody(body).vel.y,kxbody(body).vel.z)
kxbody(body).mang.y=atanfull(kxbody(body).vel.x,kxbody(body).vel.z)
kxbody(body).mang.z=atanfull(kxbody(body).vel.x,kxbody(body).vel.y)
kxbody(body).fdir.x=sin(kxbody(body).ang.y)*cos(kxbody(body).ang.x)
kxbody(body).fdir.y=-sin(kxbody(body).ang.x)
kxbody(body).fdir.z=cos(kxbody(body).ang.y)*cos(kxbody(body).ang.x)
kxbody(body).mdir.x=sin(kxbody(body).mang.y)*cos(kxbody(body).mang.x)
kxbody(body).mdir.y=-sin(kxbody(body).mang.x)
kxbody(body).mdir.z=cos(kxbody(body).mang.y)*cos(kxbody(body).mang.x)
kxbody(body).frcw.x=0
kxbody(body).frcw.y=kxbody(body).mass*kxworlddata.grav
kxbody(body).frcw.z=0
kxbody(body).frcd.x=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.x^2)*sgn(kxbody(body).vel.x)))
kxbody(body).frcd.y=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.y^2)*sgn(kxbody(body).vel.y)))
kxbody(body).frcd.z=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.z^2)*sgn(kxbody(body).vel.z)))
kxbody(body).frc.x=kxbody(body).frct.x+kxbody(body).frcw.x+kxbody(body).frcd.x+kxbody(body).frcc.x
kxbody(body).frc.y=kxbody(body).frct.y+(kxbody(body).frcw.y*(1-kxbody(body).ccoll))+kxbody(body).frcd.y+kxbody(body).frcc.y
kxbody(body).frc.z=kxbody(body).frct.z+kxbody(body).frcw.z+kxbody(body).frcd.z+kxbody(body).frcc.z
kxbody(body).acc.x=kxbody(body).frc.x/kxbody(body).mass
kxbody(body).acc.y=kxbody(body).frc.y/kxbody(body).mass
kxbody(body).acc.z=kxbody(body).frc.z/kxbody(body).mass
kxbody(body).vel.x=kxbody(body).vel.x+kxbody(body).acc.x
kxbody(body).vel.y=kxbody(body).vel.y+kxbody(body).acc.y
kxbody(body).vel.z=kxbody(body).vel.z+kxbody(body).acc.z
kxbody(body).speed=sqrt(kxbody(body).vel.x^2+kxbody(body).vel.y^2+kxbody(body).vel.z^2)
kxbody(body).pos.x=kxbody(body).pos.x+kxbody(body).vel.x
kxbody(body).pos.y=kxbody(body).pos.y+kxbody(body).vel.y
kxbody(body).pos.z=kxbody(body).pos.z+kxbody(body).vel.z
kxbody(body).trq.x=kxbody(body).trqt.x+kxbody(body).trqd.x+kxbody(body).trqc.x
kxbody(body).trq.y=kxbody(body).trqt.y+kxbody(body).trqd.y+kxbody(body).trqc.y
kxbody(body).trq.z=kxbody(body).trqt.z+kxbody(body).trqd.z+kxbody(body).trqc.z
kxbody(body).angacc.x=kxbody(body).trq.x/kxbody(body).mass
kxbody(body).angacc.y=kxbody(body).trq.y/kxbody(body).mass
kxbody(body).angacc.z=kxbody(body).trq.z/kxbody(body).mass
kxbody(body).omega.x=kxbody(body).omega.x+kxbody(body).angacc.x
kxbody(body).omega.y=kxbody(body).omega.y+kxbody(body).angacc.y
kxbody(body).omega.z=kxbody(body).omega.z+kxbody(body).angacc.z
kxbody(body).ang.x=kxbody(body).ang.x+kxbody(body).omega.x
kxbody(body).ang.y=kxbody(body).ang.y+kxbody(body).omega.y
kxbody(body).ang.z=kxbody(body).ang.z+kxbody(body).omega.z
if kxbody(body).mode=1
c=sc_sphereSlide(kxbody(body).cobj,kxbody(body).pos.x-kxbody(body).vel.x,kxbody(body).pos.y-kxbody(body).vel.y,kxbody(body).pos.z-kxbody(body).vel.z,kxbody(body).pos.x,kxbody(body).pos.y,kxbody(body).pos.z,kxbody(body).size,0)
if c>0 then kxbody(body).ccoll=1 else kxbody(body).ccoll=0
if kxbody(body).ccoll=1
kxbody(body).vel.x=sc_getCollisionbounceX()
kxbody(body).vel.y=sc_getCollisionbounceY()
kxbody(body).vel.z=sc_getCollisionbounceZ()
kxbody(body).cnorm.x=sc_getCollisionNormalX()
kxbody(body).cnorm.y=sc_getCollisionNormalY()
kxbody(body).cnorm.z=sc_getCollisionNormalZ()
endif
kxbody(body).mom.x=kxbody(body).mass*kxbody(body).vel.x
kxbody(body).mom.y=kxbody(body).mass*kxbody(body).vel.y
kxbody(body).mom.z=kxbody(body).mass*kxbody(body).vel.z
endif
position object kxbody(body).obj,kxbody(body).pos.x,kxbody(body).pos.y,kxbody(body).pos.z
rotate object kxbody(body).obj,kxbody(body).ang.x,kxbody(body).ang.y,kxbody(body).ang.z
sc_updateObject kxbody(body).obj
endfunction
function kx_getColliding(body)
local coll as boolean
coll=kxbody(body).ccoll
endfunction coll
function kx_getCollisionNormalX(body)
local nx as float
nx=kxbody(body).cnorm.x
endfunction nx
function kx_getCollisionNormalY(body)
local ny as float
ny=kxbody(body).cnorm.y
endfunction ny
function kx_getCollisionNormalZ(body)
local nz as float
nz=kxbody(body).cnorm.z
endfunction nz
function kx_getBodyVelocityX(body)
local vx as float
vx=kxbody(body).vel.x
endfunction vx
function kx_getBodyVelocityY(body)
local vy as float
vy=kxbody(body).vel.y
endfunction vy
function kx_getBodyVelocityZ(body)
local vz as float
vz=kxbody(body).vel.z
endfunction vz
function sgn(value#)
if value#>=0 then exitfunction 1
endfunction -1
function startsparky()
make memblock 1,0
delete memblock 1
endfunction
`Impotant Types
Type vec3
x as float
y as float
z as float
endtype
type tkxworlddata
grav as float
dens as float
endtype
type tkxmatdata
fric as float
cobj as integer
coll as boolean
endtype
type tkxbodydata
obj as integer
circ as float
pos as vec3
vel as vec3
acc as vec3
fdir as vec3
mdir as vec3
frct as vec3
frcw as vec3
frcd as vec3
frcc as vec3
frc as vec3
mom as vec3
mang as vec3
ang as vec3
omega as vec3
angacc as vec3
angmove as vec3
trqt as vec3
trqd as vec3
trqc as vec3
trq as vec3
mass as float
dens as float
mat as integer
dragc as float
size as float
speed as float
cobj as integer
coll as vec3
cnorm as vec3
ccoll as boolean
mode as boolean
endtype
Explanations:
kx_startKinetix()
- Basically just created all the arrays needed
kx_setupWorld(gravity, air density)
- gravity: Obvious really
- air density: the higher it is, the more drag force (you will slow down quicker)
kx_makeVector()
-Will create a control vector, so you can use this to alter forces and stuff
-It will return the vector number
-This doesn't use DBPro's vectors, just some simple custom ones of my own
kx_setVector(vec, x, y, z)
-vec: the vector you wish to use
-X: The vector's x component
-Y: The vector's Y component
-Z: The vector's z component
-Basically, once you've done this, you can assign it to a force (or whatever you want)
-so it will set it's values by what the vectors components are.
kx_createMaterial(friction, collision object)
-friction: the friction amount of the material
-collision object: the object the material reacts to
-This will return the material number
kx_createBody(object, dynamic mode, mass, drag constant, density, material)
-object: Object number to be used (only really works with spheres now)
-dynamic mode: set to 1 to move around, set to 0 to use as a static object, i.e. building
-mass: affects it's weight, acceleration, that sorta stuff
-drag constant: the higher it is, the more easy it is to be slown down by air reistance. Keep between 0 and 1.
-density: I can't remeber if I actually still use this :P
-material: assign a material created by the previous command
kx_setBodyForce(Body)
kx_setBodyTorque(Body)
kx_setBodyVelocity(Body)
kx_setBodyOmega(Body)
-Body: The body to be affected
-The value that's set by those will be the value in the last set vector.
kx_updateBody(Body)
-Basically updates the body, moves it around, etc.
kx_getColliding(Body)
-Checks if the body is colliding with an object
kx_getCollisionNormalX/Y/Z(Body)
-Returns the collision normal (normal=perpendicular(perpendicular=right angles) to the wall)
kx_getVelocityX/Y/Z(Body)
-Returns the body's velocity
Extras:
Sgn(value)
-if value is positive (or 0) it will return 1
-if it's negative, it will return 0
-It's just used in the actual program, but I'm aswell throwing it in anyway
StartSparky()
-Creates and deletes a memblock. This is required for sparky's dll.
Example?
Oh, Deamon, sorry man, found your code for matrix to object, so I had to steal it... Thanks.
`Kinetix - Physics Library #857584
`Alastair Zotos (Zotoaster)
`Main Source File
`setup
sync on : sync rate 60
set display mode 1024,768,32 : hide mouse
autocam off : set camera range 1,0x7fffffff
backdrop on : color backdrop 0
`set ambient light 75
`start Kinetix
kx_startKinetix()
`setup Kinetix world
kx_setupWorld(-1.3,0.02)
`create a simple sphere
create bitmap 1,50,50 : set current bitmap 1
ink rgb(0,0,0),0 : box 0,0,50,50
ink rgb(255,0,0),0 : box 0,23,50,27
get image 1,0,0,50,50
set current bitmap 0 : delete bitmap 1
make object sphere 1,100 : position object 1,-200,45,20
texture object 1,1 : set object rotation zyx 1
mat = kx_createMaterial(0.5,2)
sph = kx_createBody(1,1,50,0.03,0.02,mat)
vec1 = kx_makeVector()
vec2 = kx_makeVector()
frc#=100
`level
rem start
make matrix 1,10000,10000,30,30
for z=1 to 29
for x=1 to 29
height#=50.0-(abs(sin((x*40))*400))
height#=height#+(50.0-(abs(cos((z*40))*400)))
height#=height#/1.5
set matrix height 1,x,z,height#
next x
next z
update matrix 1
matrix_to_mesh(1,10000,10000,30,30,1)
make object 2,1,0 : delete mesh 1 : set object normals 2
position object 2,-5000,0,-50 : delete matrix 1
rem end
`load object "kintest3.x",2 : scale object 2,70,70,70 : yrotate object 2,180 : xrotate object 2,355 : fix object pivot 2
flr = kx_createBody(2,0,0,0,0.2,0)
`Main Loop
do
`get object positions
x#=object position x(1)
y#=object position y(1)
z#=object position z(1)
`control object
if upkey()
frcx#=sin(ay#)*frc#
frcz#=cos(ay#)*frc#
endif
if downkey()
frcx#=-sin(ay#)*frc#
frcz#=-cos(ay#)*frc#
endif
if upkey()=0 and downkey()=0 then frcx#=0 : frcz#=0
if leftkey() then a#=-3
if rightkey() then a#=3
if leftkey()=0 and rightkey()=0 then a#=0
ay#=wrapvalue(ay#+a#)
if spacekey() and kx_getColliding(sph)
frcy#=200
else
frcy#=0
endif
kx_setVector(vec1,frcx#,frcy#,frcz#) : kx_setBodyForce(sph)
kx_setVector(vec2,frcz#,0,frcx#) : kx_setBodyTorque(sph)
kx_updateBody(sph)
`camera
ca#=curveangle(ay#,ca#,6.0)
cx#=x#-sin(ca#)*600
cy#=y#+300
cz#=z#-cos(ca#)*600
position camera cx#,cy#,cz#
point camera x#,y#,z#
sync
loop
`*Kinetix Physics Functions*
`---------------------------
function kx_startKinetix()
Global kxcontvec as integer
Global kxworlddata as tkxworlddata
dim kxbody(0) as tkxbodydata
dim kxmat(0) as tkxmatdata
dim kxvec(0) as vec3
endfunction
function kx_setupWorld(grav as float, dens as float)
kxworlddata.grav=grav
kxworlddata.dens=dens
endfunction
function kx_makeVector()
local vec as integer
array insert at bottom kxvec() : vec=array count(kxvec())
endfunction vec
function kx_setVector(vec,x as float, y as float, z as float)
kxcontvec=vec
kxvec(vec).x=x
kxvec(vec).y=y
kxvec(vec).z=z
endfunction
function kx_createMaterial(fric as float, cobj)
local mat as integer
array insert at bottom kxmat() : mat=array count(kxmat())
kxmat(mat).fric=fric
if cobj>0
kxmat(mat).cobj=cobj
kxmat(mat).coll=1
endif
endfunction mat
function kx_createBody(obj, mode, mass as float, dragc as float, dens as float, mat)
local body as integer
array insert at bottom kxbody() : body=array count(kxbody())
kxbody(body).obj=obj
kxbody(body).mass=mass
kxbody(body).dragc=dragc
kxbody(body).dens=dens
kxbody(body).size=object size(obj)
kxbody(body).mat=mat
kxbody(body).cobj=kxmat(mat).cobj
kxbody(body).mode=mode
kxbody(body).pos.x=object position x(obj)
kxbody(body).pos.y=object position y(obj)
kxbody(body).pos.z=object position z(obj)
kxbody(body).circ=kxbody(body).size*3.14
sc_setupComplexObject obj,0,2
endfunction body
function kx_setBodyForce(body)
kxbody(body).frct.x=kxvec(kxcontvec).x
kxbody(body).frct.y=kxvec(kxcontvec).y
kxbody(body).frct.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyTorque(body)
kxbody(body).trqt.x=kxvec(kxcontvec).x
kxbody(body).trqt.y=kxvec(kxcontvec).y
kxbody(body).trqt.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyVelocity(body)
kxbody(body).vel.x=kxvec(kxcontvec).x
kxbody(body).vel.y=kxvec(kxcontvec).y
kxbody(body).vel.z=kxvec(kxcontvec).z
endfunction
function kx_setBodyOmega(body)
kxbody(body).omega.x=kxvec(kxcontvec).x
kxbody(body).omega.y=kxvec(kxcontvec).y
kxbody(body).omega.z=kxvec(kxcontvec).z
endfunction
function kx_updateBody(body)
kxbody(body).mang.x=atanfull(kxbody(body).vel.y,kxbody(body).vel.z)
kxbody(body).mang.y=atanfull(kxbody(body).vel.x,kxbody(body).vel.z)
kxbody(body).mang.z=atanfull(kxbody(body).vel.x,kxbody(body).vel.y)
kxbody(body).fdir.x=sin(kxbody(body).ang.y)*cos(kxbody(body).ang.x)
kxbody(body).fdir.y=-sin(kxbody(body).ang.x)
kxbody(body).fdir.z=cos(kxbody(body).ang.y)*cos(kxbody(body).ang.x)
kxbody(body).mdir.x=sin(kxbody(body).mang.y)*cos(kxbody(body).mang.x)
kxbody(body).mdir.y=-sin(kxbody(body).mang.x)
kxbody(body).mdir.z=cos(kxbody(body).mang.y)*cos(kxbody(body).mang.x)
kxbody(body).frcw.x=0
kxbody(body).frcw.y=kxbody(body).mass*kxworlddata.grav
kxbody(body).frcw.z=0
kxbody(body).frcd.x=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.x^2)*sgn(kxbody(body).vel.x)))
kxbody(body).frcd.y=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.y^2)*sgn(kxbody(body).vel.y)))
kxbody(body).frcd.z=-(((kxbody(body).dragc+(kxmat(kxbody(body).mat).fric*kxbody(body).ccoll))/2)*kxbody(body).size*kxworlddata.dens*((kxbody(body).vel.z^2)*sgn(kxbody(body).vel.z)))
kxbody(body).frc.x=kxbody(body).frct.x+kxbody(body).frcw.x+kxbody(body).frcd.x+kxbody(body).frcc.x
kxbody(body).frc.y=kxbody(body).frct.y+(kxbody(body).frcw.y*(1-kxbody(body).ccoll))+kxbody(body).frcd.y+kxbody(body).frcc.y
kxbody(body).frc.z=kxbody(body).frct.z+kxbody(body).frcw.z+kxbody(body).frcd.z+kxbody(body).frcc.z
kxbody(body).acc.x=kxbody(body).frc.x/kxbody(body).mass
kxbody(body).acc.y=kxbody(body).frc.y/kxbody(body).mass
kxbody(body).acc.z=kxbody(body).frc.z/kxbody(body).mass
kxbody(body).vel.x=kxbody(body).vel.x+kxbody(body).acc.x
kxbody(body).vel.y=kxbody(body).vel.y+kxbody(body).acc.y
kxbody(body).vel.z=kxbody(body).vel.z+kxbody(body).acc.z
kxbody(body).speed=sqrt(kxbody(body).vel.x^2+kxbody(body).vel.y^2+kxbody(body).vel.z^2)
kxbody(body).pos.x=kxbody(body).pos.x+kxbody(body).vel.x
kxbody(body).pos.y=kxbody(body).pos.y+kxbody(body).vel.y
kxbody(body).pos.z=kxbody(body).pos.z+kxbody(body).vel.z
kxbody(body).trq.x=kxbody(body).trqt.x+kxbody(body).trqd.x+kxbody(body).trqc.x
kxbody(body).trq.y=kxbody(body).trqt.y+kxbody(body).trqd.y+kxbody(body).trqc.y
kxbody(body).trq.z=kxbody(body).trqt.z+kxbody(body).trqd.z+kxbody(body).trqc.z
kxbody(body).angacc.x=kxbody(body).trq.x/kxbody(body).mass
kxbody(body).angacc.y=kxbody(body).trq.y/kxbody(body).mass
kxbody(body).angacc.z=kxbody(body).trq.z/kxbody(body).mass
kxbody(body).omega.x=kxbody(body).omega.x+kxbody(body).angacc.x
kxbody(body).omega.y=kxbody(body).omega.y+kxbody(body).angacc.y
kxbody(body).omega.z=kxbody(body).omega.z+kxbody(body).angacc.z
kxbody(body).ang.x=kxbody(body).ang.x+kxbody(body).omega.x
kxbody(body).ang.y=kxbody(body).ang.y+kxbody(body).omega.y
kxbody(body).ang.z=kxbody(body).ang.z+kxbody(body).omega.z
if kxbody(body).mode=1
c=sc_sphereSlide(kxbody(body).cobj,kxbody(body).pos.x-kxbody(body).vel.x,kxbody(body).pos.y-kxbody(body).vel.y,kxbody(body).pos.z-kxbody(body).vel.z,kxbody(body).pos.x,kxbody(body).pos.y,kxbody(body).pos.z,kxbody(body).size,0)
if c>0 then kxbody(body).ccoll=1 else kxbody(body).ccoll=0
if kxbody(body).ccoll=1
kxbody(body).vel.x=sc_getCollisionbounceX()
kxbody(body).vel.y=sc_getCollisionbounceY()
kxbody(body).vel.z=sc_getCollisionbounceZ()
kxbody(body).cnorm.x=sc_getCollisionNormalX()
kxbody(body).cnorm.y=sc_getCollisionNormalY()
kxbody(body).cnorm.z=sc_getCollisionNormalZ()
endif
kxbody(body).mom.x=kxbody(body).mass*kxbody(body).vel.x
kxbody(body).mom.y=kxbody(body).mass*kxbody(body).vel.y
kxbody(body).mom.z=kxbody(body).mass*kxbody(body).vel.z
endif
position object kxbody(body).obj,kxbody(body).pos.x,kxbody(body).pos.y,kxbody(body).pos.z
rotate object kxbody(body).obj,kxbody(body).ang.x,kxbody(body).ang.y,kxbody(body).ang.z
sc_updateObject kxbody(body).obj
endfunction
function kx_getColliding(body)
local coll as boolean
coll=kxbody(body).ccoll
endfunction coll
function kx_getCollisionNormalX(body)
local nx as float
nx=kxbody(body).cnorm.x
endfunction nx
function kx_getCollisionNormalY(body)
local ny as float
ny=kxbody(body).cnorm.y
endfunction ny
function kx_getCollisionNormalZ(body)
local nz as float
nz=kxbody(body).cnorm.z
endfunction nz
function kx_getBodyVelocityX(body)
local vx as float
vx=kxbody(body).vel.x
endfunction vx
function kx_getBodyVelocityY(body)
local vy as float
vy=kxbody(body).vel.y
endfunction vy
function kx_getBodyVelocityZ(body)
local vz as float
vz=kxbody(body).vel.z
endfunction vz
function sgn(value#)
if value#>=0 then exitfunction 1
endfunction -1
function startsparky()
make memblock 1,0
delete memblock 1
endfunction
`Impotant Types
Type vec3
x as float
y as float
z as float
endtype
type tkxworlddata
grav as float
dens as float
endtype
type tkxmatdata
fric as float
cobj as integer
coll as boolean
endtype
type tkxbodydata
obj as integer
circ as float
pos as vec3
vel as vec3
acc as vec3
fdir as vec3
mdir as vec3
frct as vec3
frcw as vec3
frcd as vec3
frcc as vec3
frc as vec3
mom as vec3
mang as vec3
ang as vec3
omega as vec3
angacc as vec3
angmove as vec3
trqt as vec3
trqd as vec3
trqc as vec3
trq as vec3
mass as float
dens as float
mat as integer
dragc as float
size as float
speed as float
cobj as integer
coll as vec3
cnorm as vec3
ccoll as boolean
mode as boolean
endtype
`Deamon's Functions (Thanks)
function matrix_to_mesh(matrixnum,width,height,xtiles,ztiles,meshnum)
create_memblockobject(meshnum,xtiles*ztiles*6)
for x=0 to xtiles-1
for z=0 to ztiles-1
div#=2
create_vertex(meshnum,vertex,x*width/(xtiles+0.0),get matrix height(matrixnum,x,z),z*height/(ztiles+0.0),rgb(get matrix height(matrixnum,x,z)/div#,get matrix height(matrixnum,x,z)/div#,get matrix height(matrixnum,x,z)/div#),0,0)
create_vertex(meshnum,vertex+1,x*width/(xtiles+0.0),get matrix height(matrixnum,x,z+1),(z+1)*height/(ztiles+0.0),rgb(get matrix height(matrixnum,x,z+1)/div#,get matrix height(matrixnum,x,z+1)/div#,get matrix height(matrixnum,x,z+1)/div#),0,0)
create_vertex(meshnum,vertex+2,(x+1)*width/(xtiles+0.0),get matrix height(matrixnum,x+1,z),z*height/(ztiles+0.0),rgb(get matrix height(matrixnum,x+1,z)/div#,get matrix height(matrixnum,x+1,z)/div#,get matrix height(matrixnum,x+1,z)/div#),0,0)
create_vertex(meshnum,vertex+3,(x+1)*width/(xtiles+0.0),get matrix height(matrixnum,x+1,z),z*height/(ztiles+0.0),rgb(get matrix height(matrixnum,x+1,z)/div#,get matrix height(matrixnum,x+1,z)/div#,get matrix height(matrixnum,x+1,z)/div#),0,0)
create_vertex(meshnum,vertex+4,x*width/(xtiles+0.0),get matrix height(matrixnum,x,z+1),(z+1)*height/(ztiles+0.0),rgb(get matrix height(matrixnum,x,z+1)/div#,get matrix height(matrixnum,x,z+1)/div#,get matrix height(matrixnum,x,z+1)/div#),0,0)
create_vertex(meshnum,vertex+5,(x+1)*width/(xtiles+0.0),get matrix height(matrixnum,x+1,z+1),(z+1)*height/(ztiles+0.0),rgb(get matrix height(matrixnum,x+1,z+1)/div#,get matrix height(matrixnum,x+1,z+1)/div#,get matrix height(matrixnum,x+1,z+1)/div#),0,0)
for i=0 to 5
set_normal(meshnum,vertex+i,1,1,1)
next i
inc vertex,6
next z
next x
make mesh from memblock meshnum,meshnum
delete memblock meshnum
endfunction
function create_memblockobject(memnum,v)
make memblock memnum,12+(36*v)
write memblock dword memnum,0,338
write memblock dword memnum,4,36
write memblock dword memnum,8,v
endfunction
function create_vertex(memnum,v,vposx#,vposy#,vposz#,color as dword,u#,v#)
write memblock float memnum,v*36+12,vposx#
write memblock float memnum,v*36+16,vposy#
write memblock float memnum,v*36+20,vposz#
write memblock dword memnum,v*36+36,color
write memblock float memnum,v*36+40,u#
write memblock float memnum,v*36+44,v#
endfunction
function set_normal(memnum,v,normalx#,normaly#,normalz#)
write memblock float memnum,v*36+24,normalx#
write memblock float memnum,v*36+28,normaly#
write memblock float memnum,v*36+32,normalz#
endfunction
function make_object(objnum,meshnum,memnum,imgnum)
make mesh from memblock meshnum,memnum
make object objnum,meshnum,imgnum
endfunction
`*Command List*
`--------------
remstart
Ignore the stupid layout, they're like that so they can be put in a keywords file.
Main Functions:
kx_startKinetix==(*No Parameters*)
kx_setupWorld==(Gravity, Air Density)
kx_makeVector==(*No Parameters*)
kx_setVector==(Vec, X, Y, Z)
kx_createMaterial==(Friction, Collision Object)
kx_createBody==(Object, Dynamic mode, Mass, Drag Constant, Density, Material)
kx_setBodyForce==(Body)
kx_setBodyTorque==(Body)
kx_setBodyVelocity==(Body)
kx_setBodyOmega==(Body)
kx_updateBody==(Body)
kx_getColliding==(Body)
kx_getCollisionNormalX==(Body)
kx_getCollisionNormalY==(Body)
kx_getCollisionNormalZ==(Body)
kx_getBodyVelocityX==(body)
kx_getBodyVelocityY==(body)
kx_getBodyVelocityZ==(body)
Extra Functions:
sgn==(Value)
startsparky==(*No Parameters*)
You will need:
Sparky's DLL V2.0
remend
You will need:
Sparky's older collision dll. Not the one with the sphere collision commands and stuff... the older one.
Anyway, enjoy
[edit]
Attaching sparky's collision (I'm pretty sure that's it)