Code: Select all
#include "openb3d.bi"
#macro atan2deg( a, b )
atan2(a,b)*180/3.1415926535897
#endmacro
const maxjoints=2
screen 18, 32, , &h10002
Graphics3d 640,480,32,1,1
var camera=createcamera(0)
moveentity camera,0,0,-10
var target=createsphere(16)
entitycolor target,255,0,0
dim key as string
dim joint(maxjoints) as any ptr
var j=createcone()
fitmesh j,-.2,0,-.2,.4,2,.4
rotatemesh j,-90,0,0
joint(0)=copyentity(j)
for i as integer=1 to maxjoints
joint(i)=copyentity(j,joint(i-1))
positionentity joint(i),0,0,2
next
freeentity j
var endpoint=createsphere(16,joint(maxjoints))
scalemesh endpoint,.2,.2,.2
positionentity endpoint,0,0,2
do
key=inkey
if key="a" then Moveentity target,0,0,1
if key="z" then Moveentity target,0,0,-1
if key=chr(255)+"H" then Moveentity target,0,1,0
if key=chr(255)+"P" then Moveentity target,0,-1,0
if key=chr(255)+"M" then Moveentity target,1,0,0
if key=chr(255)+"K" then Moveentity target,-1,0,0
for i as integer=maxjoints to 0 step -1
if i>0 then entityparent joint(i),0,1
dim as single endx=entityx(joint(i),1)-entityx(endpoint,1)
dim as single endy=entityy(joint(i),1)-entityy(endpoint,1)
dim as single endz=entityz(joint(i),1)-entityz(endpoint,1)
dim as single targetx=entityx(joint(i),1)-entityx(target,1)
dim as single targety=entityy(joint(i),1)-entityy(target,1)
dim as single targetz=entityz(joint(i),1)-entityz(target,1)
dim as single end_pitch=atan2deg(-endy, sqr(endz*endz+endx*endx))
dim as single end_yaw=atan2deg(endx, -endz)
dim as single target_pitch=atan2deg(-targety, sqr(targetz*targetz+targetx*targetx))
dim as single target_yaw=atan2deg(targetx, -targetz)
turnentity joint(i),0,-end_yaw,0,1
turnentity joint(i),-end_pitch,0,0,1
turnentity joint(i),target_pitch,0,0,1
turnentity joint(i),0,target_yaw,0,1
if i>0 then
entityparent joint(i),joint(i-1),1
positionentity joint(i),0,0,2
end if
next
updateworld 1
renderworld
sleep 1
flip
loop until key=chr(27)