win32 physics test

BlitzMax Forums/MiniB3D Module/win32 physics test

klepto2(Posted 2007) [#1]
Hi, as said in the version 0.40 module thread i'm currently working on different fields regarding to minib3d one thing is
to embed the crossplattform physics library newton (http://newtondynamics.com) . As I haven't worked before with such a lib I firstly have to figure out how it works and how to integrate it to minib3d. So here is my first result:

a small test app, where some spheres are falling onto a plane and collide and act physically correct. As it is a modified version of the camerapick sample you could apply a force to an individual sphere by pressing the left mouse button.

Pleas post your results and how it performs (does it lag or something else)

Link: http://klepto2.kl.funpic.de/DL/newtontest.rar


TheSin(Posted 2007) [#2]
Could it be that you're reading my mind??? :-P

Edit: Smooth 60 FPS on my CoreDuo T2400 (1.8GHz) with Radeon Mobility x1600 (OMEGA drivers) :-)


Panno(Posted 2007) [#3]
smooth 60 fps sempron 2200 gf6200 xp sp2

cool


LarsG(Posted 2007) [#4]
60 fps, on my lappy (see sig)..


bradford6(Posted 2007) [#5]
Klepto. that is very cool. good job!

it is 60 fps here (perhaps change to a flip 0 to get real fps)


mongia2(Posted 2007) [#6]
cool work
60 fps!


klepto2(Posted 2007) [#7]
thx guys :)

I have stated to integrate a kind of interface with minib3d so
my current test code looks like this (everything oop yet, but I will provide procedural functions later).

I'm interrested what you guys currently think about how it looks in the code: (easiness, readability etc.)

Framework sidesign.minib3d

Strict


Local width=640,height=480,depth=16,mode=0

Graphics3D width,height,depth,mode

Local cam:TCamera=CreateCamera()
PositionEntity cam,0,0,-15

Local light:TLight=CreateLight()
RotateEntity light,45,0,0


' 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

Local Physic:TPhysic = TPhysic.Init() 'Will disapear to the inernals


'This will be replaced and also disapear
Local MatID:Int = NewtonMaterialGetDefaultGroupID(TPhysic.World)
NewtonMaterialSetDefaultElasticity(TPhysic.World,MatID,MatID,.6)


SeedRnd(MilliSecs())

'Creating some Balls and cubes to play with

For Local I:Int = 0 To 200
	If Rand(10) < 5 Then 
		Local sphere:TMesh=CreateSphere()
		EntityRadius sphere,2
		EntityPickMode sphere,2
		PositionEntity sphere,Rnd(-20,20),Rnd(0,100),Rnd(-20,20)
		EntityColor(sphere,Rand(255),Rand(255),Rand(255))
		
		Local NSphere:TNewtonObject = TNewtonObject.Create(Sphere,PHYS_SPHERE_HULL) 'Create the Collision Hull (Parameters are set automatically)
		NSphere.SetMass(1.0,1.0,1.0,1.0) ' Make it movable and a accurate ball
		Physic.AddObject(NSphere) 'Give it to the Physic engine
	Else
		Local cube:TMesh=CreateCube()
		EntityRadius cube,2
		EntityPickMode cube,2
		PositionEntity cube,Rnd(-20,20),Rnd(0,100),Rnd(-20,20)
		EntityColor(cube,Rand(255),Rand(255),Rand(255))
		
		Local NCube:TNewtonObject = TNewtonObject.Create(cube,PHYS_BOX_HULL) 'Create the Collision Hull (Parameters are set automatically)
		NCube.SetMass(1.0,1.0,1.0,1.0) ' Make it movable and a accurate cube
		Physic.AddObject(NCube) 'Give it to the Physic engine
	EndIf
Next


Local Plane:TMesh = CreateCube()
PositionEntity(Plane,0,-30,0)
ScaleEntity(Plane,80,5,80)
EntityColor plane,20,100,30


Local NPlane:TNewtonObject = TNewtonObject.Create(Plane,PHYS_BOX_HULL)
NPlane.SetMass(0.0,0.0,0.0,0.0) ' Set it solid
Physic.AddObject(NPlane) 'Give it to the Physic engine



Local Timestep:Int = MilliSecs()

Global  pick:TEntity
While Not KeyDown(KEY_ESCAPE)		
	
	Pick = Null
	
	If KeyHit(KEY_ENTER) Then DebugStop

	'' control camera
	
	' mouse look
	
	mxs#=mxs#+(MouseXSpeed()/5.0)
	mys#=mys#+(MouseYSpeed()/5.0)

	RotateEntity cam,mys#,-mxs#,0

	If KeyDown(KEY_SPACE)=False
		MoveMouse width/2,height/2
		MouseXSpeed() ' flush
		MouseYSpeed() ' flush
	EndIf

	' move camera forwards/backwards/left/right with cursor keys
	
	If KeyDown(KEY_UP)=True Then MoveEntity cam,0,0,move# ' move camera forward
	If KeyDown(KEY_DOWN)=True Then MoveEntity cam,0,0,-move# ' move camera back

	If KeyDown(KEY_LEFT)=True Then MoveEntity cam,-move#,0,0 ' move camera left
	If KeyDown(KEY_RIGHT)=True Then MoveEntity cam,move#,0,0 ' move camera right
	
	

	' if mousehit then perform camerapick
	If MouseHit(1)
	
				

		pick=CameraPick(cam,MouseX(),MouseY())
		
		If pick<>Null
			Local XP:Float = Rnd(-15.0,15.0)
			Local YP:Float = Rnd(-15.0,15.0)
			Local ZP:Float = Rnd(-15.0,15.0)
			' Apply a random impulse to the picked ball
			Physic.ApplyImpulse(pick,PickedX(),PickedY(),PickedZ(),XP,YP,ZP)
		EndIf
		
	
	EndIf
	
	
	Physic.Update() ' Update the physics (Update time is automatically calculated and passed as Deltatime within the engine)
	UpdateWorld()
	RenderWorld()
	renders=renders+1

	If MilliSecs()-old_ms>=1000
		
		old_ms=MilliSecs()
		fps=renders
		renders=0
	EndIf
		
	
	
	SetBlend AlphaBlend
	Text 0,0,"FPS: "+String(fps)
	Flip 

Wend
End



mongia2(Posted 2007) [#8]
it is a simple code

i prefere a create_A_newton_box
and a newton sphere
ecc

it is plus simple
mongia2


Mr. Write Errors Man(Posted 2007) [#9]
Cool stuff!

With my super powerful 1Ghz Athlon and Geforce 4 MX 440 combo I get around 30 FPS when lots of stuff is visible.

-AF


TheSin(Posted 2007) [#10]
The code is cool! It's neither complicated nor difficult to understand. Good stuff :-)


klepto2(Posted 2007) [#11]
thx again:)
I have choosen the PHYS_XXXX_HULL constants because the whole physic system should be internally work with newton but newton should stay in the background. I have oosen this path to provide an as easy as it can be interface to simulate the uncomplicated commands from minib3d and b3d.
Maybe the constants will change but not to something like CREATE_NEWTON_BOX or the like.


bradford6(Posted 2007) [#12]
i noticed the newton.dll in there. are you planning to make this accessible from MAC/Linux and Win?


klepto2(Posted 2007) [#13]
yes, as soon as the framework for the interface stays (not so long) I plan to first integrate it to linux (the only other OS I have) and after that I will search a mac user who will create the mac wrapper.


TheSin(Posted 2007) [#14]
Well, as soon as I sold my laptop I'll get an iMac (Intel). I could then look into it.
Shouldn't be too hard, the Newton API should be the same on every OS. It's only the dynamic library that has to be exchanged afaik.


bradford6(Posted 2007) [#15]
cool. I only do blitz stuff on a windows box but like the idea of cross-compat.

btw. that was a record for fast response!