When camera position x and body position are greater than 100 (approximately), the raycast can't find the body each time. That's strange but I don't think it come from my code, because I've got the same problem with the second demo of newton (try to drag an object on the right). Perhaps a problem with U5.9 and U6 (I've tried both versions).
sync on
sync rate 60
hide mouse
rem newton
ndb_newtoncreate
NDB_SetVector 0.0, -98.1, 0.0
NDB_SetStandardGravity
rem floor
make object box 1,500,10,500
position object 1,0,-5,0:color object 1,255
make_box(1,0)
rem box
make object cube 2,50
position object 2,200,100,0
make_box(2,10)
rem box
make object cube 3,50
position object 3,50,100,0
make_box(3,10)
position camera 0,100,-100
autocam off
make object sphere 10,5
rem wall
remstart
make object box 11,5,200,500
position object 11,100,100,0
ghost object on 11
set object emissive 11,rgb(255,0,0)
set object ambience 11,rgb(255,0,0)
remend
time# = NDB_GetElapsedTimeInSec()
time# = NDB_GetElapsedTimeInSec()
do
circle 320,240,5 : dot 320,240
ax#=wrapvalue(ax#+mousemovey()*0.2)
ay#=wrapvalue(ay#+mousemovex()*0.2)
rotate camera ax#,ay#,0
move camera upkey()-downkey()
time# = NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate time#
if nobj>0
if object exist(nobj) then delete object nobj
endif
if controlkey()
nobj = NDB_DebugMakeNewtonObject()
set object light nobj, 0
else
nobj = 0
endif
gosub ObjectDragging
sync
loop
function make_box(dbobj,mass#)
x#=object position x(dbobj)
y#=object position y(dbobj)
z#=object position z(dbobj)
sizex#=object size x(dbobj):sizey#=object size y(dbobj):sizez#=object size z(dbobj)
rem on crée les données des collisions
index = NDB_NewtonCreateBox(sizex#, sizey#, sizez#)
rem on crée un corp
Body = NDB_NewtonCreateBody(index)
rem calculs liés a la masse (moment d'inertie)
if mass#>0
NDB_BodySetGravity Body, 1
NDB_CalculateMIBoxSolid mass#, sizex#,sizey#,sizez#
NDB_NewtonBodySetMassMatrix Body, mass#
endif
rem positions/angles du solide
NDB_BuildMatrix ax#, ay#, az#, x#, y#, z#
NDB_NewtonBodySetMatrix body
NDB_NewtonReleaseCollision index
NDB_BodySetDBProData body,dbobj
NDB_NewtonBodySetDestructorCallback Body
endfunction body
delete memblock 1
ObjectDragging:
`this part is for object dragging... slightly complicated, ignore it if you don't understand it! :P
if (mouseclick() = 2) and (MOUSE = 0)
MOUSE = 1
`shoot a ray!
raydist# = 1000.0
x1# = camera position x() : y1# = camera position y() : z1# = camera position z()
move camera raydist#
x2# = camera position x() : y2# = camera position y() : z2# = camera position z()
move camera 0-raydist#
unitx# = (x2#-x1#) / raydist# : unity# = (y2#-y1#) / raydist# : unitz# = (z2#-z1#) / raydist#
NDB_SetVector 1, x1#, y1#, z1#
NDB_SetVector 2, x2#, y2#, z2#
dist# = NDB_NewtonWorldRayCast( 1 )
if dist# > 1.0
MOUSE = 0
DragBody = 0
return
endif
Body = NDB_RayCastGetBody()
GrabDist# = dist#*raydist#
x# = x1# + (unitx#*GrabDist#)
y# = y1# + (unity#*GrabDist#)
z# = z1# + (unitz#*GrabDist#)
DragBody = body
DragObj = NDB_GetDBPro(body)
`okay get the rotation matrix for the body.
NDB_NewtonWorldUnFreezeBody DragBody
NDB_NewtonBodySetAutoFreeze DragBody, 0
NDB_NewtonBodyGetMatrix DragBody
NDB_SetVector 1, x#, y#, z#
`unrotate the point by this matrix
NDB_UntransformVector 1, 1
`now vector2 should be the local point of contact!
GrabX# = NDB_GetVector_X(1)
GrabY# = NDB_GetVector_Y(1)
GrabZ# = NDB_GetVector_Z(1)
endif
if mouseclick()=2 and MOUSE = 1
if DragBody <> 0
`drag the object here!
NDB_NewtonBodyGetMatrix DragBody
NDB_SetVector 1, GrabX#, GrabY#, GrabZ#
NDB_TransformVector 1, 2
x1# = NDB_GetVector_X(2)
y1# = NDB_GetVector_Y(2)
z1# = NDB_GetVector_Z(2)
position object 10,x1#,y1#,z1#
move camera 0,GrabDist#
x2# = camera position x()
y2# = camera position y()
z2# = camera position z()
move camera 0,-grabdist#
`calculate the force between the points
DragX# = (x2# - x1#) * 50.0
DragY# = (y2# - y1#) * 50.0
DragZ# = (z2# - z1#) * 50.0
NDB_NewtonBodyGetVelocity DragBody
dec DragX#, NDB_GetVector_X()*(2)
dec DragY#, NDB_GetVector_Y()*(2)
dec DragZ#, NDB_GetVector_Z()*(2)
`setup global force.
NDB_SetVector 1, x1#, y1#, z1#
NDB_SetVector 2, DragX#/5, DragY#/5, DragZ#/5
NDB_BodyAddForceGlobal DragBody
endif
endif
if mouseclick() = 0
MOUSE = 0
if DragBody <> 0
NDB_NewtonBodySetAutoFreeze DragBody, 1
DragBody = 0
endif
GrabX# = 0.0
GrabY# = 0.0
GrabZ# = 0.0
DragX# = 0.0
DragY# = 0.0
DragZ# = 0.0
GrabDist# = 0.0
endif
return
function free_obj()
for o=1 to 65535
if object exist(o)=0 then exitfunction o
next o
endfunction 0