Topic: Converting Blitz3D calls to BMax for PhysX wrapper.

I am currently trying to convert the PhysX functions from Blitz3D to BMax in order to handle solid and cloth objects to use with the Irrlicht Graphics engine, I'm not even sure if it's possible yet, but if it is it would mean a much better visualisation of the PhysX engine in BlitzMax. MiniB3D is a bit dated and limited. Here is my attempt so far:

CountChildren = getChildren()
GetChild = either getLeftChild() or getRightChild(), need help here!
EntityName = getName()
MeshWidth = getScale().X
MeshHeight = getScale().Y
MeshDepth = getScale().Z
EntityPitch = getRotation().X
EntityYaw = getRotation().Y
EntityRoll = getRotation().Z
MoveEntity = setPosition()
EntityX = getPosition().X
EntityY = getPosition().Y
EntityZ = getPosition().Z
RotateEntity = setRotation()
CountSurfaces = getTriangleCount() 'Not sure?!'
GetSurface = Dont Know!
CountVertices = getVertexCount()
VertexX = getVertex().X  'I think this is right?'
VertexY = getVertex().Y
VertexZ = getVertex().Z
CountTriangles = Not Sure? this is why I'm not sure about CountSurface()
GetMatElement = Not sure, maybe something like getMatrix()?
VertexCoords = Don't know.

I think that's it, once these functions are converted PhysX can be used with the Irrlicht engine in BlitzMax.
Any help here will be really appreciated.

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

hi slaine.  i dont have the time right now, but i think we can get all these.  have you checked out the code to the B3D mod yet?  it has most if not all these converted already in some form.  i probably wont be able to get you a detailed message for a few days.

3 (edited by Slaine 2007-05-22 02:36:52)

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

Thanks gman, I had a brief look at the MiniB3D and it said:

Function GetSurface()
        Return surfcount
End function

So I gave up on that at the time, but now I've realised that the surfcount would be in the TEntity, so I'll take another look today.  I'm also going to look at the Irrlicht Newton code, that may have some methods in there.

When you say it already has some converted, do you mean that the MiniB3D can be mixed with the Irrlicht? Or is that wishfull thinking on my behalf big_smile

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

hi Slaine.  sorry i was a bit vague.  i meant that the Irrlicht.B3D mod has many of those converted.  for example, a surface in Irrlicht is an instance of imeshbuffer.  so GetSurface would look something like:

Function GetSurface:IMeshBuffer(mesh:IMesh,index:Int,frame:Int = 0)
    Local surface:IMeshBuffer
    If IAnimatedMesh(mesh) Then 
        surface=IAnimatedMesh(mesh).getMesh(frame).getMeshBuffer(index)
    ElseIf IMesh(mesh) Then 
        surface=IMesh(mesh).getMeshBuffer(index)
    EndIf
EndFunction

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

Excellent!  So there are only two functions now needed for the PhysX engine to work.
TriangleVertex()
CountTriangles()

Nice work smile

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

I hope you dont mind but I've been trying to make the Method myself, do you think I am on the right track?

Method TriangleVertex:SURFACE(index:Int,vert_no:Int,corner:Int)
        
        Local mb:SURFACE=IMesh.getMeshBuffer(index)
        
        mb._mbuf=IMesh.getMeshBuffer(index)

        mb_indices[]=mb._mbuf.getIndices()
        
        Local vertex_corner_A = mb_indices[(3*vert_no)]
        Local vertex_corner_B = mb_indices[(3*vert_no) +1]
        Local vertex_corner_c = mb_indices[(3*vert_no) +2]
        
        Return mb
    End Method

smile

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

havent tested yet...  here is the method to add to SURFACE.  also, we are getting into a pretty untested area of the core mod...  i wouldnt be surprised if some bugs in core cropped up.

Method getTriangleVertex:Int(triangle_index:Int,corner:Short)
    If Not _mbuf Or Not _mbuf.isValid() Then RuntimeError "Surface meshbuffer is invalid"
    Return _mbuf.getIndices().ElementAt((triangle_index * 3) + corner)[0]
EndMethod

and here is the ib3d function:

Function ib3d_TriangleVertex:Int(surf:SURFACE,triangle_index:Int,corner:Short)
    If Not surf Then RuntimeError "Surface does not exist"
    Return surf.getTriangleVertex(triangle_index,corner)
EndFunction

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

and here is counttriangles:

Method getTriangleCount:Int()
    If Not _mbuf Or Not _mbuf.isValid() Then RuntimeError "Surface meshbuffer is invalid"
    Return _mbuf.getIndexCount()/3
EndMethod
Function ib3d_CountTriangles:Int(surf:SURFACE)
    If Not surf Then RuntimeError "Surface does not exist"
    Return surf.getTriangleCount()
EndFunction

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

Fantastic gman thanks, I am however getting a surface meshbuffer is invalid, here

For ns = 1 To nsurf
        Local surf:SURFACE = mesh.GetSurface(ns)
        nvert = nvert + ib3d_CountVertices(surf)    'HERE
    Next

Not sure what that is?

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

your probably going out of bounds.  iB3D is 0 based so your for loop should go from 0 Until nsurf

For ns = 0 Until nsurf

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

hmmm.  i just reliazed im inconsistant in 0 vs 1 based indexes when working with iB3D.  gonna have to make a decision on that and work towards consistancy.  probably will migrate towards 1 based since that is what B3D is founded on.

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

Import irrlicht.b3d
Import "PhysX_func.bmx"

'Strict
Global irr:ib3d_engine = _g_ib3d_engine


Local width=640,height=480,depth=16,mode=2,shadows=True


irr.Graphics3D(width,height,depth,mode,shadows)

Global vertnun, facnum

Global plane:PLANE = irr.createPlane(200,200,1,1,0)
ib3d_EntityColor(plane,20,40,200)

Local cam:CAMERA = irr.CreateCamera()
cam.Position(0,10,-60)

Local light:LIGHT = irr.CreateLight(2)
light.Position(-15,30,-150)
ib3d_LightColor(light,200,200,200)
ib3d_AmbientLight(32,32,32)
ib3d_LightRange(light,1000)
ib3d_LightCastShadows(light,1)


' used by camera code
Local mxs#=0
Local mys#=0
Local move#=0.5
MouseXSpeed() ' flush
MouseYSpeed() ' flush

' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps

'physic
dir$ = CurrentDir ()
dir = Replace$( dir$,"/","\" )
pxRegWriteDriverPath (dir.ToCString())
'Create World
PxCreateWorld(0, "key")
pxSetGravity(0, -1.5, 0)
pxRegWriteDriverPath ("C:\Program Files\AGEIA Technologies".ToCString())

planeBody = PxBodyCreateCube(200,1,200, 0)
pxBodySetPosition(planeBody ,0,0,0)

Rem
'Create TriMesh
Local ent:MESH = irr.LoadMesh("media/TriMesh.b3d")
ent.Position(0,0,0)
BodyCreateTriMesh(ent)
End Rem
'Create Hull
Local teapot:MESH = irr.LoadMesh("media/teapot.b3d")
teapot_body = BodyCreateHull%(teapot)
pxBodySetPosition(teapot_body, 0, 60,0)


'Create Sphere
Local sphere:PRIMITIVE = irr.CreateSphere()
sphere.Position(10,10,0)
ib3d_ScaleEntity(sphere,4,4,4)


'create Cube
Local cube:PRIMITIVE = irr.CreateCube()
ib3d_EntityColor(cube,0,120,0)
cube.Position(-15,10,0)




Global sphereBody = PxBodyCreateSphere(4, 10)
pxBodySetPosition(sphereBody,10, 20,0)

Global cubeBody = PxBodyCreateCube(4,4,4, 10)
pxBodySetPosition(cubeBody,5,30,0)


planeBody = PxBodyCreateCube(25,1,25, 0)
pxBodySetPosition(planeBody ,0,-1,0)


While Not irr.KeyDown(KEY_ESCAPE)        

    If KeyHit(KEY_ENTER) Then DebugStop

    'move camera forwards/backwards/Left/Right with cursor keys
    
    If irr.KeyDown(KEY_UP)=True Then cam.Move(0,0,move#) ' move camera forward
    If irr.KeyDown(KEY_DOWN)=True Then cam.Move(0,0,-move#) ' move camera back

    If irr.KeyDown(KEY_LEFT)=True Then cam.Move(-move#,0,0) ' move camera left
    If irr.KeyDown(KEY_RIGHT)=True Then cam.Move(move#,0,0) ' move camera right
    
    ''
    
    teapot.Position(pxBodyGetPositionX(teapot_body),pxBodyGetPositionY(teapot_body),pxBodyGetPositionZ(teapot_body))
    teapot.Rotate(pxBodyGetRotationPitch(teapot_body),pxBodyGetRotationYaw(teapot_body),pxBodyGetRotationRoll(teapot_body))
    
    sphere.Position(pxBodyGetPositionX(sphereBody),pxBodyGetPositionY(sphereBody),pxBodyGetPositionZ(sphereBody))
    sphere.Rotate(pxBodyGetRotationPitch(sphereBody),pxBodyGetRotationYaw(sphereBody),pxBodyGetRotationRoll(sphereBody))
    
    
    cube.Position(pxBodyGetPositionX(cubeBody),pxBodyGetPositionY(cubeBody),pxBodyGetPositionZ(cubeBody))
    cube.Rotate(pxBodyGetRotationPitch(cubeBody),pxBodyGetRotationYaw(cubeBody),pxBodyGetRotationRoll(cubeBody))
    
'    surf:SURFACE = ent.GetSurface(1)
    
    pxRenderPhysic(30, 0)
    irr.RenderWorld()
    renders=renders+1
    
    Flip 

Wend
irr.EndGraphics()
End

Function BodyCreateHull%(mesh:MESH)

    Local nsurf% = mesh.CountSurfaces()
    Local nvert% = 0
    For ns = 0 Until nsurf
        Local surf:SURFACE = mesh.GetSurface(ns)
        nvert = nvert + ib3d_CountVertices(surf)
    Next
         vbank:TBank = CreateBank(nvert*4*3)
    nv = 0
    For ns = 0 Until nsurf
        surf:SURFACE = mesh.GetSurface(ns)
        nvv = ib3d_CountVertices(surf)
        For nvc = 0 To nvv - 1
             PokeFloat vbank,nv*12+0,ib3d_VertexX(surf,nvc)
             PokeFloat vbank,nv*12+4,ib3d_VertexY(surf,nvc)
             PokeFloat vbank,nv*12+8,ib3d_VertexZ(surf,nvc)
            nv = nv+1
        Next
    Next
    
    Local vbp:Byte Ptr
    vbp = vbank.Buf()

    Local bbb%= pxBodyCreateHull(vbp, nvert, 10)
    Return bbb
End Function

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

I'm wondering if the teapot is the wrong way around in the collision world? trouble is, my 3D programming is pretty limited so I cant really debug your code.

14 (edited by Slaine 2007-05-30 03:48:38)

Re: Converting Blitz3D calls to BMax for PhysX wrapper.

It now looks like it has nothing to do with your code, because the spheres and cubes also act strangly, I think it's to do with the Irrlicht Rotation function, I tried If pitch > 360 Then pitch = pitch but that didn't seem to help, the teapot basically screws up when it goes upside down.  And the spheres rotate very strangly, you'll probably find that this will also happen in your Irrlicht.B3D lib.