Jump to content

Entity/Body conversion problem


macklebee
 Share

Recommended Posts

Looking at how to get a randomly picked entity's body so to add a force to it. The bullet.lua just loops until it gets the parent that is the model. The example in the wiki for c++'s AddBodyForceAtPoint just does the GetParent() on the picked entity. Trying this in bmax several ways, all I get are either Int to TBody conversion error using the lua method, or TEntity to TBody conversion error using the c++ method. What is the correct way to convert your pick.entity to either a model or body in bmax?

 

Here is the bmax code with the lua method involved...

SuperStrict
Framework leadwerks.engine
Import "c:\program files\leadwerks engine sdk\bmx\framework\framework.bmx"
Include "lua-gluefunctions.bmx"
GCSetMode(2)

RegisterAbstractPath ("c:\program files\leadwerks engine sdk")
Graphics(800, 600)

Global fw:TFramework = CreateFramework()
If Not fw RuntimeError "Failed to initialize engine."

MoveEntity fw.Main.camera, Vec3(0, 0, - 5)

Collisions 1,1,True

Global oildrum:TModel = LoadModel("abstract::oildrum.gmf")
EntityType(oildrum, 1, 1)

Local body:TBody = CreateBodyBox()
SetBodyMass body, 1.0
EntityType body, 1

Local mesh:TMesh = CreateCube()
EntityParent mesh, body
Local material:TMaterial
material = CreateMaterial()
SetMaterialColor material, Vec4(1, 0, 0, 1)
PaintEntity mesh, material

Local ground:TBody=CreateBodyBox(10,0.1,10)
PositionEntity ground,vec3(0,-2,0)
EntityType ground,1

Local groundmesh:TMesh=CreateCube()
ScaleEntity groundmesh,vec3(10,0.1,10)
EntityParent groundmesh,ground,0
material:TMaterial = CreateMaterial()
SetMaterialColor material,vec4(0,1,1,1)
PaintEntity groundmesh,material

Local light:TLight=CreateDirectionalLight()
RotateEntity light,vec3(35,55)

Local piv:TPivot = CreatePivot()
EntityParent fw.Main.camera, piv

While Not KeyHit(KEY_ESCAPE)

If KeyDown(KEY_LEFT) TurnEntity piv,vec3(0,1,0)
If KeyDown(KEY_RIGHT) TurnEntity piv,vec3(0,1,0)
If KeyDown(KEY_UP) MoveEntity fw.Main.camera, Vec3(0, 0, 0.1)
If KeyDown(KEY_DOWN) MoveEntity fw.Main.camera, Vec3(0, 0, - 0.1)

Local pick:TPick
If MouseHit(1)
	pick = CameraPick(fw.Main.camera, Vec3(MouseX(), MouseY(), 1000))
	If pick
			Local campos:TVec3 = EntityPosition(fw.Main.camera, True)
			Local force:TVec3=vec3(pick.x-campos.x,pick.y-campos.y,pick.z-campos.z).normalize()
			force.x:*100.0
			force.y:*100.0
			force.z:*100.0
			AddBodyForceAtPoint(GetMeshModel(pick.entity), force, Vec3(pick.x, pick.y, pick.z))
	EndIf
EndIf

fw.Update()
fw.Render()

DrawText "Click on the body to apply force.", 0, 40
DrawText "Use the arrow keys to move the camera.", 0, 60
Flip(0)	
Wend

GCCollect()
End

Function GetMeshModel(entity:TEntity)
While entity <> Null
	If GetEntityClass(entity) = ENTITY_MODEL
		return entity
	EndIf
	entity=entity.parent
Wend
EndFunction

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Hey Mac.

 

Did you find a solution to this problem??

 

Ive been looking at the BlitzMax code on the wiki and that works for a mesh , but i would also like this to work on a loaded model.

 

Thanks

Gimpy73 :)

Link to comment
Share on other sites

If i dont' remeber wrong the conversion is made like this :

Body:TBody = TBody (entity)

 

So it should be like this:

 

Function GetMeshModel:TBody(entity:TEntity)
       While entity <> Null
               If GetEntityClass(entity) = ENTITY_MODEL
                       local Body:TBody = TBody (entity)
                       return Body
               EndIf
               entity=entity.parent

       Wend
EndFunction

 

I'll check in my old code and i tell you..

  • Upvote 2

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

yes thank you Joh. I was leaving out the conversion. :)

 

I am also looking at being able to interact with not only loaded models but also meshes/bodies created through code. Take a look at this and let me know what you think. It works for the program I listed above, but perhaps there is a shortfall on how I am doing this or perhaps a better way? This allows me to move both the oildrum and the code-created cube.

Function GetMeshModel:TBody(entity:TEntity)
       While entity <> Null
	If GetEntityClass(entity) = ENTITY_BODY Or GetEntityClass(entity) = ENTITY_MODEL
               	Return TBody(entity)
	EndIf
               entity = entity.parent
       Wend
EndFunction

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...