Newton trouble.

BlitzMax Forums/BlitzMax Programming/Newton trouble.

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.

Nevermind, I've cranked out some noob code. Here it is:

'my newton physics test

Framework BRL.Blitz 'core module
Import BRL.Random 'random number generator.
Import Pub.Newton 'newton dynamics interface module.
Import Irrlicht.Extended 'a slight extension by me that adds the CreateIrrlicht
                         'and ReleaseIrrlicht convenience functions.

SeedRnd MilliSecs() 'randomize based on the system timer -- the best way to get
                    'the most random results.

'irrlicht pointers
Global dvc:IrrlichtDevice
Global dvr:IVideoDriver
Global sne:ISceneManager

'create irrlicht context
Print "Successfully created Irrlicht Device context."
CreateIrrlicht EDT_OPENGL,640,480,32,True,True,True,dvc,dvr,sne

Print "Successfully generated font."
Global font:IGUIFont=dvc.getGUIEnvironment().getBuiltInFont()

'create skybox
sne.addSkyBoxSceneNode( ..
 dvr.getTexture("up.jpg"), ..
  dvr.getTexture("down.jpg"), ..
   dvr.getTexture("left.jpg"), ..
    dvr.getTexture("right.jpg"), ..
     dvr.getTexture("back.jpg"), ..
      dvr.getTexture("front.jpg"))
Print "Successfully added skybox."

'create scene camera (viewpoint)
Global cam:ICameraSceneNode=sne.addCameraSceneNodeFPS(Null,100,95)
cam.setPosition(_VECTOR3DF(0,40,-200))
Print "Successfully added camera."

'add a light source
sne.addLightSceneNode(Null,_VECTOR3DF(0,100,0))
Print "Successfully added light source."

'load a map mesh, nothing fancy, just a flat surface grid
Global grid:IAnimatedMesh=sne.getMesh("grid.b3d")
If Not grid.isValid() Then
	Notify "Could not load mesh GRID.B3D.",True
	End
End If
'add map node to scene
Global map:ISceneNode=sne.addAnimatedMeshSceneNode(grid)
map.setMaterialTexture(0,dvr.getTexture("grid.jpg"))
map.SetScale(_VECTOR3DF(10,10,10))
map.setPosition(_VECTOR3DF(0,-100,0))
map.setMaterialFlag EMF_LIGHTING,True
Print "Successfully added map."

'attach the newton api lib
If Not StartNewton() Then
	Print "Could not attach dynamics engine API."
	Notify "Could not attach dynamics engine API.",True
	End
End If
Print "Successfully attached dynamics engine API!"

'create a newton world using custom memory handlers by pointing
'to the functions; null argument selects standard memory handlers
Global world:Byte Ptr=NewtonCreate(MemAllocate,MemRelease)
Print "Successfully generated dynamics world!"

'add some cubes
WriteStdout "Adding cube nodes..."
Global boxnode:ISceneNode[20]
For j=0 To 19
	boxnode[j]=sne.addCubeSceneNode()
	boxnode[j].setPosition(_VECTOR3DF(Rnd(-100#,100#),Rnd(-100#,100#),Rnd(-100#,100#)))
	boxnode[j].setMaterialTexture(0,dvr.getTexture("grid.jpg"))
	boxnode[j].setMaterialFlag EMF_LIGHTING,True
Next
Print "done!"

Local collision:Byte Ptr=NewtonCreateBox(world,1#,1#,1#,Null)
Print "Successfully generated cube collision object."

WriteStdout "Generating cube collision bodies..."
Global box:Byte Ptr[20]
Local mat:Float[16]
Local omega:Float[3]
For j=0 To 19
	GetEulerMatrix boxnode[j],mat
	box[j]=NewtonCreateBody(world,collision)
	NewtonBodySetMassMatrix box[j],1#,1#,1#,1#
	NewtonBodySetMatrix box[j],Varptr mat[0]
	omega[0]=Rnd(-5#,5#)
	omega[1]=Rnd(-5#,5#)
	omega[2]=Rnd(-5#,5#)
	NewtonBodySetOmega box[j],Varptr omega[0]
Next
Print "done!"

'okay, here we go
WriteStdout "Begin simulation..."
fps=0
str$=""
timestep!=0

Repeat
	timestep=NewtonGetTimeStep(world)*1.2
	NewtonUpdate world,timestep
	
	For j=0 To 19
		NewtonBodyGetMatrix box[j],Varptr Mat[0]
		SetEulerMatrix boxnode[j],Mat
	Next

	dvr.beginScene(True,True)
		sne.drawAll()
	
		fps=dvr.getFPS()
		str="renderer is ~q"
		str:+dvr.getName()+"~q   dynamics engine is ~qNewton Dynamics Engine v"
		str:+NewtonWorldGetVersion(world)+"~q   "
		str:+"the frame per second count is "+fps+"   timestep is "+timestep
		font.draw str,_RECTI(10,10,300,50),_SCOLOR(255,255,255,255)
	dvr.endScene()
Until Not dvc.run()
Print "ended properly."

'now we clean up
NewtonReleaseCollision world,collision
Print "Successfully released cube collision object."
NewtonDestroyAllBodies world 'destroy all bodies in dynamics world
Print "Successfully destroyed all physical bodies."
NewtonDestroy world 'destroy dynamics world
Print "Successfully destroyed dynamics world."
StopNewton 'drop newton api
Print "Successfully dropped dynamics engine API."
ReleaseIrrlicht dvc 'release irrlicht context
Print "Successfully released Irrlicht Device context."

End

'Excellent Euler Matrix converter functions.  Not by me, and I would give credit where
'it was called for, except I honestly don't know who did this.
Function GetEulerMatrix(NSceneNode:ISceneNode,NewtonMat#[])
	Local Euler#[3]
	Euler[0]=NSceneNode.getRotation().getX()*(Pi/180#)
	Euler[1]=NSceneNode.getRotation().getY()*(Pi/180#)
	Euler[2]=NSceneNode.getRotation().getZ()*(Pi/180#)
	NewtonSetEulerAngle(Varptr Euler[0],Varptr NewtonMat[0])
	
	NewtonMat[12]=NSceneNode.getPosition().getX()
	NewtonMat[13]=NSceneNode.getPosition().getY()
	NewtonMat[14]=NSceneNode.getPosition().getZ()
End Function

Function SetEulerMatrix(NSceneNode:ISceneNode,NewtonMat#[])
	Local Euler#[3]
	NewtonGetEulerAngle(Varptr NewtonMat[0],Varptr Euler[0])
	NSceneNode.setPosition _VECTOR3DF(NewtonMat[12],NewtonMat[13],NewtonMat[14])
	NSceneNode.setRotation _VECTOR3DF(Euler[0]*(180/Pi),Euler[1]*(180/Pi),Euler[2]*(180/Pi))
End Function

'Overrides Newton's local memory functions with BMax native wrappers.
Function MemAllocate:Byte Ptr(sizeInBytes:Int)
	Return MemAlloc(sizeInBytes)
End Function
Function MemRelease(memptr:Byte Ptr,sizeInBytes:Int)
	MemFree(memptr)
End Function


Oh and here's Irrlicht.Extended :
Module Irrlicht.Extended

Import Irrlicht.Core
Import BRL.System
Private
	OnEnd byebye
	Function byebye()
		Print "Goodbye."
	End Function
Public

Function CreateIrrlicht(renderer,rezx,rezy,bits,fullscrn,stencil,vsync,DVC:IrrlichtDevice Var,DVR:IVideoDriver Var,SCN:ISceneManager Var)
	DVC=IrrlichtDevice.create(..
	 renderer,_DIMENSION2DI(rezx,rezy),rezdepth,..
	  fullscrn,stencil,vsync)
	
	If DVC.handle=0 Then
		Print "Could not create 3D device!"
		Notify "Could not create 3D device!",True
		End
	Else
		Print "Created 3D device successfully!"
	End If
	
	DVR=DVC.getVideoDriver()
	SCN=DVC.getSceneManager()
End Function

Function ReleaseIrrlicht(DVC:IrrlichtDevice)
	DVC.drop()
	DVC=Null
	If DVC Then
		Print "3D device was not released successfully!"
		Notify "3D device was not released successfully!",True
		End
	Else
		Print "3D device was released successfully!"
	End If
End Function


Problem is, a) they won't collide with each other, and b) they won't fall. Can someone enlighten me about this? I am brand new with Newton.