[bb] NA_ODE by namar777 [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : NA_ODE
Author : namar777
Posted : 1+ years ago

Description : Nothing special, thought i'd might contribute some stuff that i'm currently not using. I mapped most of the "Useful" commands. There is much place for improvement, and maybe i hope not some corrections. The little plus here is you can place the Trimesh and scaleit directly from the code.

 The System tracks the Entities with number indexing in order to control them (positionentity,rotateentity and other commands). I also allowed realtime scaling of the meshes during runtime...

  ps: Just post any fixes on the comments section ;)
                                    Seee Ya
                                       Nassim Amar
                                        First Contribution
                                        Cheers


Code :
Code (blitzbasic) Select
;Title:NA_Ode(NassimAmar Ode) >>>>Ode 0.5b<<<<
;Description:Mapping Functions to B3d Standards
;
;
;Tutorial:
; Creating Entities:
; 1. NaOde_CreateCylinder(name,space,enable)
; 2. Cylinder=NaOde_CreateCylinder(name,space,enable)
; Naming Entities Differently is Most useful when you as a programmer
; would want to change the position or rotation of the latter.
; -NaOde_CreateCylinder(name,space,enable)
; -NaOde_CreateCylinder(name,space,enable)
; -NAOde_PositionEntity(name,180,0,0)--> This Will Move Both objects Named "x"
;----------------------------------------------------------------------------------------------
; Arrays:
; Dim Cylinders(20)
; For x=1 To 20
; NaOde_CreateCylinder(name,space,enable)
; NAOde_PositionEntity(name,Rnd(10),Rnd(10),Rnd(10))
; Next
; >>Put the x in the Name Place as NAOde. Doing So
;  will logically give the Various Entities Created
;  different numbers, Hence allowing the use of specific
;  functions. But since probably you'll be using numerous
;  arrays. Might as well as a string before the "x"
; -NAOde_PositionEntity(name,rnd(10),rnd(10),rnd(10))
;

Const dParamLoStop = 0
Const dParamHiStop = 1
Const dParamVel = 2
Const dParamVel2 = 258
Const dParamFMax = 3
Const dParamFMax2 = 259
Const dParamFudgeFactor = 4
Const dParamBounce = 5
Const dParamCFM = 6
Const dParamStopERP = 7
Const dParamStopCFM = 8
Const dParamSuspensionERP = 9
Const dParamSuspensionCFM = 10

Const dContactMu2 = $001
Const dContactFDir1 = $002
Const dContactBounce = $004
Const dContactSoftERP = $008
Const dContactSoftCFM = $010
Const dContactMotion1 = $020
Const dContactMotion2 = $040
Const dContactSlip1 = $080
Const dContactSlip2 = $100
Const dContactApprox0 = $0000
Const dContactApprox1_1 = $1000
Const dContactApprox1_2 = $2000
Const dContactApprox1 = $3000

Const NAOde_PrimitiveMeshes=1
Const NAOde_Meshes=2
Const NAOde_Joints=3

Const NAODE_Cube=1
Const NAODE_Sphere=2
Const NAODE_Cylinder=3

Const Hinge=1
Const Slider=2
Const Universal=3
Const Hinge2=4
Const Fixed=5
Const Motor=6
Const Ball=7
SeedRnd MilliSecs()

Type TODEGeom
Field body%,joint%,typjoint
Field geom%,tdata,enable,geo
Field mesh%,mesh2%,name$,cnt,id,flag
End Type

Global space%=ODE_dWorldCreate(1)
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>My Functions<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Function NAODE_Init(Gravity#=-1,Bounce#=0.2,BounceVelocity#=1,Friction#=0.6,Friction2#=0.6,EntitiesSoftness#=0,GroundSoftness#=0,Slip1#=0,Slip2#=0,Motion1#=0,Motion2#=0)
ODE_dWorldSetGravity(0, Gravity#, 0)
ODE_dSetBOUNCE(Bounce#)
ODE_dSetBOUNCE_VEL(BounceVelocity#)
ODE_dSetMU(Friction#); Friction
ODE_dSetMU2(Friction2#); Friction
ODE_dSetSOFT_ERP(EntitiesSoftness#)
ODE_dSetSOFT_CFM(GroundSoftness#)
ODE_dSetSLIP1(Slip1#)
ODE_dSetSLIP1(Slip2#)
ODE_dSetMOTION1(Motion1#)
ODE_dSetMOTION2(Motion2#)
End Function

Function NAODE_Features(all)
ODE_dSetContactMode(all)
End Function

Function NAODE_CountEntities(id)
num=0
For g.TodeGeom=Each TodeGeom
If gid=id Then num=num+1
Return num
Next
End Function

Function NAODE_CreateCylinder(name$,world,gravmode=0)
g.TOdeGeom=New TOdeGeom
g
ame$=Upper(name$)
gid=NAOde_PrimitiveMeshes
gcnt=NAOde_CountEntities(gid)
genable=1
ggeo=NAODE_Cylinder
gody=ODE_dBodyCreate()
ggeom=ODE_dCreateCCylinder(world,radius#,height#,mass#)
ODE_dGeomSetBody(ggeom,gody)
gmesh=CreateCylinder()
ScaleEntity gmesh,1,1,1
ODE_dBodySetPosition gody,0,0,0
ODE_dBodySetRotation gody,0,0,0
ODE_dBodySetGravityMode(gody,gravmode)
Return gmesh
End Function

Function NAODE_CreateCube(name$,world,gravmode=0)
g.TOdeGeom=New TOdeGeom
g
ame$=Upper(name$)
gid=NAOde_PrimitiveMeshes
gcnt=NAOde_CountEntities(gid)
ggeo=NAODE_Cube
gmesh=CreateCube()
genable=1
ScaleEntity gmesh,1,1,1
gody=ODE_dBodyCreate()
ggeom=ODE_dCreateBox(world,MeshWidth(gmesh),MeshHeight(gmesh),MeshDepth(gmesh),mass#)
ODE_dGeomSetBody(ggeom,gody)
ODE_dBodySetPosition gody,0,0,0
ODE_dBodySetRotation gody,0,0,0
ODE_dBodySetGravityMode(gody,gravmode)
Return gmesh
End Function
 
Function NAODE_CreateSphere(name$,world,gravmode=0)
g.TOdeGeom=New TOdeGeom
g
ame$=Upper(name$)
gid=NAOde_PrimitiveMeshes
gcnt=NAOde_CountEntities(gid)
ggeo=NAODE_Sphere
gody=ODE_dBodyCreate()
genable=1
ggeom=ODE_dCreateSphere%(world, radius#, mass#)
ODE_dGeomSetBody(ggeom,gody)
gmesh=CreateSphere()
ScaleEntity gmesh,1,1,1
ODE_dBodySetPosition gody,0,0,0
ODE_dBodySetRotation gody,0,0,0
ODE_dBodySetGravityMode(gody,gravmode)
Return gmesh
End Function


Function NAODE_SetMeshToBody(name$,meshpath$)
For g.TodeGeom=Each TodeGeom
If g
ame$=Upper(name$) And gid=NAODE_PrimitiveMeshes
gmesh=LoadMesh(meshpath$)
Return gmesh
EndIf
Next
End Function

Function NAODE_LoadMesh%(path$,name$,x#=0,y#=0,z#=0,scale#=1) ;rx#=0,ry#=0,rz#=0 On A later Version
g.TODEGeom=New TODEGeom
g data=t_data
g
ame$=Upper(name$)
gid=NAODE_Meshes
gcnt=NAODE_CountEntities(gid)
gmesh=LoadMesh(path$)
mesh%=CopyMesh(gmesh)
tris_count% = 0
vert_count% = 0
For i = 1 To CountSurfaces(mesh)
tris_count = tris_count + CountTriangles(GetSurface(mesh, i))
vert_count = vert_count + CountVertices(GetSurface(mesh, i))
Next
DebugLog "Tris count: " + tris_count
DebugLog "Vertices count: " + vert_count
DebugLog "surface count: "+CountSurfaces(mesh)
vertices_bank% = CreateBank(vert_count * 4 * 4)
indices_bank% = CreateBank(tris_count * 3 * 4)

offset% = 0
offset_tris% = 0
baseverts=0
For i = 1 To CountSurfaces(mesh)
sf = GetSurface(mesh, i)
For v = 0 To CountVertices(sf) - 1
PokeFloat vertices_bank, offset, VertexX(sf, v)*(scale#)+x#
offset = offset + 4
PokeFloat vertices_bank, offset, VertexY(sf, v)*(scale#)+y#
offset = offset + 4
PokeFloat vertices_bank, offset, VertexZ(sf, v)*(scale#)+z#
offset = offset + 8
Next
For t = 0 To CountTriangles(sf) - 1
For j = 0 To 2
v = TriangleVertex(sf, t, j)+baseverts
PokeInt indices_bank, offset_tris, v
offset_tris = offset_tris + 4
Next
Next
baseverts=baseverts+CountVertices(sf)
Next
FreeEntity mesh
PositionEntity gmesh,x#,y#,z#
RotateEntity gmesh,rx#,ry#,rz#
ScaleEntity gmesh,scale#,scale#,scale#
t_data = ODE_dGeomTriMeshDataCreate()
ODE_dGeomTriMeshDataBuildSimple(t_data, vertices_bank, vert_count, indices_bank, tris_count * 3)
  ODE_dCreateTriMesh(space, t_data)
Return gmesh
End Function

Function NAODE_PositionEntity(name$,x#,y#,z#)
For g.TOdeGeom=Each TOdeGeom
If g
ame$=Upper(name$) ODE_dBodySetPosition gody,x#,y#,z#
Next
End Function

Function NAODE_RotateEntity(name$,rx#,ry#,rz#)
For g.TOdeGeom=Each TOdeGeom
If g
ame$=Upper(name$) ODE_dBodySetRotation gody,rx#,ry#,rz#
Next
End Function

Function NAODE_ScaleEntity(name$,world,x#,y#,z#,mass#=1)
For g.TOdeGeom=Each TOdeGeom
If g
ame$=Upper(name$)
If ggeo=NAODE_Cube
ODE_dGeomDestroy(ggeom)
ggeom=ODE_dCreateBox(world,x#,y#,z#,mass#)
ODE_dGeomSetBody(ggeom,gody)
ScaleEntity gmesh,x#,y#,z#
EndIf
If ggeo=NAODE_Sphere
ODE_dGeomDestroy(ggeom)
ggeom=ODE_dCreateSphere(world, x#, mass#)
ODE_dGeomSetBody(ggeom,gody)
ScaleEntity gmesh,x#,x#,x#
EndIf
If ggeo=NAODE_Cylinder
ODE_dGeomDestroy(ggeom)
ggeom=ODE_dCreateCCylinder(world,x#,y#,mass#)
ODE_dGeomSetBody(ggeom,gody)
ScaleEntity gmesh,x#,y#,x#
EndIf
EndIf
Next
End Function

Function NAODE_PushEntity(name$,x#,y#,z#)
For g.TOdeGeom=Each TOdeGeom
If g
ame$=Upper(name$) ODE_dBodyAddForce gody,x#,y#,z#
Next
End Function

Function NAODE_TorqueEntity(name$,x#,y#,z#)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) ODE_dBodyAddTorque gody,x#,y#,z#
Next
End Function

Function NAODE_TranslateEntity(name$,x#,y#,z#)
For g.TOdeGeom=Each TOdeGeom
If g
ame$=Upper(name$) ODE_dBodyTranslateMass(gody,x#,y#,z#)
Next
End Function

Function NAODE_EntityX(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetPositionX#(gody)
Next
End Function

Function NAODE_EntityY(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetPositionY#(gody)
Next
End Function

Function NAODE_EntityZ(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetPositionZ#(gody)
Next
End Function

Function NAODE_EntityPitch(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetPitch#(gody)
Next
End Function

Function NAODE_EntityYaw(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetYaw#(gody)
Next
End Function

Function NAODE_EntityRoll(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetRoll#(gody)
Next
End Function

Function NAODE_EnableEntity(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) body=gody
genable=1
Next
ODE_dBodyEnable(body%)
End Function

Function NAODE_DisableEntity(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) body=gody
genable=0
Next
ODE_dBodyDisable(body%)
End Function

Function NAODE_FreeEntity(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$)
body=gody
FreeEntity gmesh
Delete g
EndIf
Next
ODE_dBodyDestroy(body%)
End Function

Function NAODE_EntityMass(name$,mass#)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) ODE_dBodySetMass(gody,mass#)
Next
End Function

Function NAODE_Gravity(x#,y#,z#)
ODE_dWorldSetGravity(x#,y#,z#)
End Function

Function NAODE_CountCollisions(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetCollisionsCount%(gody)
Next
End Function

Function NAODE_CollisionX(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetCollisionX#(gody, index%)
Next

End Function

Function NAODE_CollisionY(name$,index)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetCollisionY#(gody, index%)
Next

End Function

Function NAODE_CollisionZ(name$,index)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetCollisionZ#(gody, index%)
Next
End Function

Function NAODE_CollisionNX(name$,index)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetCollisionNX#(gody, index%)
Next
End Function

Function NAODE_CollisionNY(name$,index)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetCollisionNY#(gody,index%)
Next
End Function

Function NAODE_CollisionNZ(name$,index)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) Return ODE_dBodyGetCollisionNZ#(gody,index%)
Next
End Function


Function NAODE_EntityCollided(name$,index)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) body=gody
If gody=ODE_dBodyGetCollisionBody%(body%, index%)
Return g
ame$
EndIf
Next
End Function

Function NAODE_EntityAngularVelX(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$)
Return ODE_dBodyGetAngularVelX(gody)
End If
Next
End Function

Function NAODE_EntityAngularVelY(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$)
Return ODE_dBodyGetAngularVelY(gody)
End If
Next
End Function

Function NAODE_EntityAngularVelZ(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$)
Return ODE_dBodyGetAngularVelZ(gody)
End If
Next
End Function

Function NAODE_EntityAngularVel(name$,ax#,ay#,az#)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$)
ODE_dBodySetAngularVel(gody, ax#, ay#, az#)
End If
Next
End Function


Function NAODE_CreateJoint(name$,SelectWhichOne$)
g.TodeGeom=New TodeGeom
g
ame$=Upper(name$)
gid=NAODE_Joints
gcnt=NAODE_CountEntities(gid)
Select Upper(SelectWhichOne$)
Case "BALL"
gjoint=ODE_dJointCreateBall()
g ypjoint=Ball
Case "HINGE"
gjoint=ODE_dJointCreateHinge()
g ypjoint=Hinge
Case "SLIDER"
gjoint=ODE_dJointCreateSlider()
g ypjoint=Slider
Case "UNIVERSAL"
gjoint=ODE_dJointCreateUniversal()
g ypjoint=Universal
Case "HINGE2"
gjoint=ODE_dJointCreateHinge2()
g ypjoint=Hinge2
Case "FIXED"
gjoint=ODE_dJointCreateFixed()
g ypjoint=Fixed
Case "MOTOR"
gjoint=ODE_dJointCreateAMotor()
g ypjoint=Motor
Default
gjoint=ODE_dJointCreateHinge()
g ypjoint=Hinge
End Select
End Function



Function NAODE_AttachJoint(joint$,name1$,name2$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name2$) body2=gody
If g
ame$=Upper(joint$) joint=gjoint
If g
ame$=Upper(name1$) body1=gody
Next
ODE_dJointAttach(joint,body1,body2)
End Function

Function NAODE_DetachJoint(joint$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(joint$) joint=gjoint
Next
ODE_dJointDestroy(joint)
End Function

Function NAODE_DeleteVolume(name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) geom=ggeom
Next
ODE_dSpaceRemove(space%,geom)
End Function

Function NAODE_PositionJointAxises(jointname$,axis1x#,axis1y#,axis1z#,axis2x#,axis2y#,axis2z#)
For g.TodeGeom=Each TodeGeom
If g
ame$=Upper(jointname$) And gid=NAODE_Joints
If g ypjoint=Hinge2
ODE_dJointSetHinge2Axis1 (gjoint,axis1x#,axis1y#,axis1z#)
ODE_dJointSetHinge2Axis2 (gjoint,axis2x#,axis2y#,axis2z#)
EndIf
If g ypjoint=Hinge
ODE_dJointSetHingeAxis(gjoint,axis1x#,axis1y#,axis1z#)
EndIf
EndIf
Next
End Function

Function NAODE_SetJointParameter(name$,Parameter,Suspension#=.3)
For g.TodeGeom=Each TodeGeom
If g
ame$=Upper(jointname$) And gid=NAODE_Joints
ODE_dJointSetHinge2Param(gjoint, Parameter,Suspension#)
EndIf
Next
End Function

Function NAODE_JointHinge2Angle1(name$)
For g.TodeGeom=Each TodeGeom
If g
ame$=Upper(jointname$) And gid=NAODE_Joints
Return ODE_dJointGetHinge2Angle1(gjoint)
EndIf
Next
End Function

Function NAODE_CreateSimpleSpace(space)
Return ODE_dSimpleSpaceCreate(space)
End Function

Function NAODE_AddSpace(space,name$)
For g.TodeGeom=Each Todegeom
If g
ame$=Upper(name$) ODE_dSpaceAdd(space%,ggeom)
Next
End Function




Function NAODE_PositionJoint(name$,x#,y#,z#)
For g.TodeGeom=Each TodeGeom
If g
ame$=Upper(jointname$) And gid=NAODE_Joints
ODE_dJointSetHinge2Anchor(gjoint,x#,y#,z#)
EndIf
Next
End Function

Function NAODE_UpdateWorld(TypeofUpdate=1) ;Modified the Original One Just to Keep Track of Each Entity
For g.TODEGeom = Each TODEGeom
If gid=NAOde_PrimitiveMeshes
For cnt=1 To NAOde_CountEntities(gid)
 If gcnt=cnt
If genable=1
ODE_dBodyEnable gody
   PositionEntity gmesh, ODE_dGeomGetPositionX(ggeom), ODE_dGeomGetPositionY(ggeom), ODE_dGeomGetPositionZ(ggeom)
RotateEntity gmesh, ODE_dGeomGetPitch(ggeom), ODE_dGeomGetYaw(ggeom), ODE_dGeomGetRoll(ggeom), 1
 EndIf
EndIf
Next
EndIf
Next
Select TypeofUpdate
Case 2
ODE_dWorldStep(0.1)
Default
ODE_dWorldQuickStep(0.1)
End Select
End Function


Comments :


Fuller(Posted 1+ years ago)

 Needs an example


Fuller(Posted 1+ years ago)

 [double post sorry]