JV-ODE Physics Thread 11

Blitz3D Forums/Blitz3D Userlibs/JV-ODE Physics Thread 11

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Question about rays:
dGeomRaySet(ray,px#,py#,pz#, dx#,dy#,dz#)

Ogre docs said:
Ogre::Vector3 diff = dest - pos;
diff.normalise();
dGeomRaySet(mRay, pos.x, pos.y, pos.z, diff.x, diff.y, diff.z);


How do you find out which values there should be for dx, dy, and dz? I'm using the following:
px# = EntityX(gun)
py# = EntityY(gun)
pz# = EntityZ(gun)

dx# = EntityX(pivot)-EntityX(gun)
dy# = EntityY(pivot)-EntityY(gun)
dz# = EntityZ(pivot)-EntityZ(gun)

dGeomRaySet(RayGeom, px#, py#, pz#, dx#, dy#, dz# )


EDIT: Managed to get this working. Sweet! :)

Here's the "shoot the ground (or that box)" code.
; ; ###################################################################################################
; #                                     JV-ODE - Ray Pick Demo                                      #
; #                                  Code by Jim Williams (VIP3R)                                   #
; #                              Devious Codeworks - Copyright © 2008                               #
; ###################################################################################################

Include "JV-ODE.bb"

AppTitle "JV-ODE - Ray Pick Demo"

Graphics3D 800,600,0,2

Global RayGeom
Global RayCollision=0

Type ODEGeom
	Field body
	Field geom
	Field mesh
	
	Field name$
End Type

; ###################################################################################################

; ### Setup ODE

dInitODE()

Global World=dWorldCreate()
Global Space=dHashSpaceCreate(0)
Global ContactGroup=dJointGroupCreate(0)

dWorldSetAutoDisableFlag(World,1)
dWorldSetGravity(World,0,-0.98,0)
dContactSetMode(dContactBounce)
dContactSetBounce(0.01)
dContactSetMu(48)

; ### Create Light

Global Light=CreateLight()
RotateEntity Light,50,-50,0
LightColor Light,255,255,255
AmbientLight 130,130,130

; ### Create Camera

Global Camera=CreateCamera()
CameraClsColor Camera,0,0,0
CameraRange Camera,.1,1000
PositionEntity Camera,0,30,-40

;PositionEntity Camera,2,15,-30


RotateEntity Camera,50,0,0

Global gun = CreateCube()
EntityColor gun, 0, 0 ,200
;EntityAlpha gun, 0.5



; ### Create Plane

dCreatePlane(Space,0,1,0,0)

Global Plane=CreatePlane()
EntityAlpha Plane,0.8

EntityTexture Plane,PlaneTexture()

EntityPickMode plane, 2
Global pivot = CreateSphere()
ScaleEntity pivot, .3, .3, .3

;PositionEntity pivot,2,5,-30
PointEntity Camera, pivot

;CreateMirror()

; ### Create Cylinder

Global Cylinder=CreateCylinder()
ScaleMesh Cylinder,0.2,3,0.2
PositionEntity Cylinder,0,3,-5
RotateEntity Cylinder,90,0,0
EntityColor Cylinder,230,0,0
EntityShininess Cylinder,0.7

; ###################################################################################################

AddObject()



While Not KeyHit(1)

	pick = CameraPick(camera, MouseX(), MouseY())
	PositionEntity(pivot, PickedX(), PickedY()+1, PickedZ())
	

	If (KeyHit(57) )
		
		
		DebugLog "pressed space"
	EndIf
	


	UpdateGeoms()

	PTime=MilliSecs()

	dSpaceCollide(Space,World,ContactGroup)
	dWorldQuickStep(World,0.1)
	dJointGroupEmpty(ContactGroup)

	PhysicsTime#=MilliSecs()-PTime

	RenderWorld

	Text 0,0,"JV-ODE Version "+dGetVersion()
	Text 0,15,"Physics Time:"+PhysicsTime
	Text 0,70,"Cylinder = Ray Geom Position"
	Text 0,95,"Green = Collision"
	Text 0,110,"Red = No Collision"

	AddRay()

	Flip 1

Wend

dJointGroupDestroy(ContactGroup)
dSpaceDestroy(Space)
dWorldDestroy(World)
dCloseODE()

End

; ###################################################################################################

Function AddObject()

ode.ODEGeom=New ODEGeom
ode\body=dBodyCreate(World)
dBodySetPosition(ode\body,0,20,0)
dBodySetRotation(ode\body,0,20,20)
dBodySetAutoDisableFlag(ode\body,1)
;ode\geom=dCreateSphere(Space,3)
ode\geom=dCreateBox(Space, 6, 6, 6)

dGeomSetBody(ode\geom,ode\body)
ode\mesh=CreateCube()
ScaleMesh ode\mesh,3,3,3
EntityColor ode\mesh,100,100,255
EntityShininess ode\mesh,0.7
EntityAlpha ode\mesh,0.8

ode\name$ = "target..."

End Function

; ###################################################################################################

Function AddRay()

RayGeom=dCreateRay(Space,300)
;dGeomRaySet(RayGeom,0,3,-5, 0,0,10)
PositionEntity gun, 2, 5, -10
PositionEntity gun, EntityX(camera), EntityY(camera), EntityZ(camera)



PointEntity gun, pivot

;dGeomRaySet(RayGeom, EntityX(gun), EntityY(gun), EntityZ(gun), EntityPitch(pivot), EntityYaw(pivot), EntityRoll(pivot))
px# = EntityX(gun)
py# = EntityY(gun)
pz# = EntityZ(gun)

dx# = EntityX(pivot)-EntityX(gun)
dy# = EntityY(pivot)-EntityY(gun)
dz# = EntityZ(pivot)-EntityZ(gun)

dGeomRaySet(RayGeom, px#, py#, pz#, dx#, dy#, dz# )



; ### Perform Ray Collision Check
dSpaceCollide(Space,World,ContactGroup)
count1=dGeom1CountCollisions(RayGeom)
count2=dGeom2CountCollisions(RayGeom)
RayCollision=count1+count2
dJointGroupEmpty(ContactGroup)

ms = MilliSecs()

mousehit1 = MouseHit(1)

For i=1 To count1
;loop collission count
	CollisionGeom1=dGeom1CollisionGeom(RayGeom,i)
	;DebugLog "collided1 with:"+CollisionGeom1
	For ode.ODEGeom=Each ODEGeom
		If (ode\geom = CollisionGeom1 )
			;DebugLog "ode name:"+ode\name$
		EndIf
	Next

	cx# = dGeom1CollisionX(RayGeom,i)
	cy# = dGeom1CollisionY(RayGeom,i)
	cz# = dGeom1CollisionZ(RayGeom,i)
		
	If (mousehit1)
		temp = CreateSphere()
		EntityColor temp, 200, 100, 200
		size# = .3
		ScaleEntity temp, size#, size#, size#
	;	EntityAlpha temp, .1
		PositionEntity temp, cx, cy, cz#
		
		c.ODEGeom = First ODEGeom
		EntityParent temp, c\mesh
		
		DebugLog "created thingy"
	EndIf
Next


For i=1 To count2
;loop collission count
	CollisionGeom2=dGeom2CollisionGeom(RayGeom,i)
	;DebugLog "collided1 with:"+CollisionGeom2
	For ode.ODEGeom=Each ODEGeom
		If (ode\geom = CollisionGeom2 )
			;DebugLog "ode name:"+ode\name$
		EndIf
	Next
	
	cx# = dGeom2CollisionX(RayGeom,i)
	cy# = dGeom2CollisionY(RayGeom,i)
	cz# = dGeom2CollisionZ(RayGeom,i)

	If (mousehit1)
		temp = CreateSphere()
		EntityColor temp, 200, 100, 200
		size# = .3
		ScaleEntity temp, size#, size#, size#
	;	EntityAlpha temp, .1
		PositionEntity temp, cx, cy, cz#
		
		c.ODEGeom = First ODEGeom
		EntityParent temp, c\mesh
		
		DebugLog "created thingy"
	EndIf

Next



If RayCollision>0 

	EntityColor Cylinder,0,200,100
	Else
	EntityColor Cylinder,200,0,100
	End If

;DebugLog RayCollision+" --- "+ dx+", "+dy+", "+dz

dGeomDestroy(RayGeom)

End Function

; ###################################################################################################

Function UpdateGeoms()

For ode.ODEGeom=Each ODEGeom
	PositionEntity ode\mesh,dGeomGetPositionX(ode\geom),dGeomGetPositionY(ode\geom),dGeomGetPositionZ(ode\geom)
	RotateEntity ode\mesh,dGeomGetPitch(ode\geom),dGeomGetYaw(ode\geom),dGeomGetRoll(ode\geom)
Next

End Function

; ###################################################################################################