Physic engine freeze ?

Everything related to 3D programming
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Physic engine freeze ?

Post by Azur »

This example show a simple cube falling on a ground
Using the space key you can apply a force to the cube in the Z direction
Here, when the cube stopped bouncing on the ground, using the space key dosent have any effect
It's like the physic engine had frozen
What am i doing wrong ?

Code: Select all

EnableExplicit

InitEngine3D()
InitSprite()
EnableWorldPhysics(1)
EnableWorldCollisions(1)

InitKeyboard()

Enumeration
	#window1
	#camera
	#light
	#ground
	#ground_e
	#cube
	#cube_e
	#mat_cube
	#tex_cube
	#mat_gnd
	#tex_gnd
EndEnumeration

Global cubes

Procedure exit()
	End
EndProcedure

Procedure scan()
	Define ev=WindowEvent()
	Select ev
		Case #PB_Event_CloseWindow
			exit()
	EndSelect
	ExamineKeyboard()
	If KeyboardPushed(#PB_Key_Escape)
		exit()
	EndIf
	If KeyboardPushed(#PB_Key_Space)
		ApplyEntityForce(#cube_e,0,0,2)
	EndIf
EndProcedure

Procedure render()
	RenderWorld()
	FlipBuffers()
EndProcedure

Procedure create_world()

	CreateTexture(#tex_cube,64,64)
	
	StartDrawing(TextureOutput(#tex_cube))
		FillArea(1,1,-1,$e354e5)
	StopDrawing()
	
	CreateTexture(#tex_gnd,64,64)
	
	StartDrawing(TextureOutput(#tex_gnd))
		FillArea(1,1,-1,$788e52)
	StopDrawing()
	
	CreateMaterial(#mat_cube,TextureID(#tex_cube))
	CreateMaterial(#mat_gnd,TextureID(#tex_gnd))
	CreateCube(#cube,1)
	CreateEntity(#cube_e,MeshID(#cube),MaterialID(#mat_cube),0,5,0)
	EntityPhysicBody(#cube_e,#PB_Entity_BoxBody,1,0.8,0.5)
	
	CreatePlane(#ground,100,100,1,1,1,1)
	CreateEntity(#ground_e,MeshID(#ground),MaterialID(#mat_gnd))	
	EntityPhysicBody(#ground_e,#PB_Entity_StaticBody,1,0.5,0.1)
	
	CreateCamera(#camera,0,0,1024,768)
	CameraLocate(#camera,0,5,-10)
	CameraLookAt(#camera,0,0,0)
	
	CreateLight(#light,RGB(255,255,255),0,20,-5)

EndProcedure

If OpenWindow(#window1,0,0,1024,768,"Physic test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	If OpenWindowedScreen(WindowID(#window1),0,0,1024,768,0,0,0)
		WorldShadows(#PB_Shadow_Modulative)
		create_world()
		Repeat
			scan()
			render()
		ForEver	
	EndIf
EndIf
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Physic engine freeze ?

Post by Comtois »

Your body is sleeping, use DisableEntityBody(#cube_e, #false) before ApplyEntityForce()
Please correct my english
http://purebasic.developpez.com/
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: Physic engine freeze ?

Post by Azur »

Im not sure understanding the background of this solution but it works.
I Thought disabling physic body will make the cube falling trough the ground and not beeing affected by forces anymore.
Your answer give me a good way to follow.
I had the same issue with a wall consisting of many cubes.
I'll try to reproduce this wall and fix it.
Thanx
Merci comtois, bonjour de région d'Epinal
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: Physic engine freeze ?

Post by Azur »

Here is a example with a wall consisting of cubes
The commented lines into the render() procedure, cause the world to freeze.

By looping the entitys list and disabling their physicbody before each frame rendering , everything looks fine.

Code: Select all

EnableExplicit

InitEngine3D()
InitSprite()
InitKeyboard()
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)


Enumeration
	#fenetre
	#light
	#camera
	#cube_texture
	#cube_material
	#gnd_texture
	#gnd_material
	#cube_mesh
	#gnd_mesh
	#gnd_entity
EndEnumeration

Global Dim cube(1000)

Procedure exit()
	End
EndProcedure

Procedure scan()
	Define ev=WindowEvent()
	Select ev
		Case #PB_Event_CloseWindow
			exit()
	EndSelect
	ExamineKeyboard()
	If KeyboardPushed(#PB_Key_Escape)
		exit()
	EndIf
EndProcedure

Procedure render()

	;*************************************************
	
	;define i
	;For i=0 To 101
	;	DisableEntityBody(cube(i),#False)
	;Next i
	
	;*************************************************

	RenderWorld()
	FlipBuffers()
EndProcedure

Procedure init_world()
	CreateCamera(#camera,0,0,1024,768)
	CameraLocate(#camera,3,10,-30)
	CameraLookAt(#camera,3,0,0)
	
	CreateLight(#light,RGB(255,255,255),0,20,-5)
	
	CreateTexture(#cube_texture,64,64)
	StartDrawing(TextureOutput(#cube_texture))
		FillArea(1,1,-1,$e354e5)
	StopDrawing()
	CreateMaterial(#cube_material,TextureID(#cube_texture))
	
	CreateTexture(#gnd_texture,64,64)
	StartDrawing(TextureOutput(#gnd_texture))
		FillArea(1,1,-1,$aec663)
	StopDrawing()
	CreateMaterial(#gnd_material,TextureID(#gnd_texture))
	
	CreatePlane(#gnd_mesh,100,100,1,1,1,1)
	CreateEntity(#gnd_entity,MeshID(#gnd_mesh),MaterialID(#gnd_material),0,0,0)
	EntityPhysicBody(#gnd_entity,#PB_Entity_StaticBody)
	
	CreateCube(#cube_mesh,1)
	
	Define x=-5
	Define y=0.5
	
	Define i
	
	Define cmpt=0
	
	For i=0 To 101
		cube(cmpt)=CreateEntity(#PB_Any,MeshID(#cube_mesh),MaterialID(#cube_material),x,y,0)
		EntityPhysicBody(cube(cmpt),#PB_Entity_BoxBody,1)
		x+1
		If x=12
			x=-5
			y+1
		EndIf
		cmpt+1
	Next i
	
EndProcedure

If OpenWindow(#fenetre,0,0,1024,768,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
	If OpenWindowedScreen(WindowID(#fenetre),0,0,1024,768,0,0,0)
		WorldShadows(#PB_Shadow_Modulative)
		init_world()
		Repeat
			scan()
			render()
		ForEver
	EndIf
EndIf
Post Reply