Car physics

Blitz3D Forums/Blitz3D Beginners Area/Car physics

Hi all, i have made some car models (See here) and was trying to make them move and stuff (car physics). Since i have never done this before i had to find an example, which eventually i found. The problem:
I have almost no clue what any of it means :/
Here's the code:
; Simple car physics by Jeppe Nielsen 2003


Const gravity#=-0.01 ;gravity constant


Graphics3D 640,480,16,2

Const car_col=1
Const world_col=2

Collisions car_col,world_col,2,2


light=CreateLight(1)

RotateEntity light,30,20,0

plane=CreatePlane()

EntityType plane,world_col
EntityColor plane,255,0,0

For n=1 To 100

If Rnd(10)<5

sphere=CreateSphere(16)

Else

sphere=CreateCube()

EndIf

EntityType sphere,world_col
PositionEntity sphere,Rnd(-40,40),Rnd(2),Rnd(-40,40)
EntityColor sphere,Rnd(255),Rnd(255),Rnd(255)

Next

sp=CreateSphere()
ScaleEntity sp,100,100,100
FlipMesh sp

camera=CreateCamera()
CameraClsColor camera,0,0,255


car.car=carnew(0,5,0)

Repeat
TFormPoint 0,3,-5,car\e,0

dx#=(TFormedX()-EntityX(camera))*.1
dy#=(TFormedY()-EntityY(camera))*.1
dz#=(TFormedZ()-EntityZ(camera))*.1

TranslateEntity camera,dx,dy,dz

PointEntity camera,car\e

carcontrol()
carupdate()

RenderWorld()

Flip


Until KeyDown(1)
End


Type car

Field e ;entity

Field x#,y#,z# ; position in 3d-space

Field vx#,vy#,vz# ; velocity

Field ax#,ay#,az# ; acceleration

End Type


Function carnew.car(x#,y#,z#)
	
	c.car=New car
	
	c\x#=x#
	c\y#=y#
	c\z#=z#
	
	c\e=CreateCube()
	cube=CreateCube()
	ScaleEntity cube,0.3,0.3,0.3
	PositionEntity cube,0,0,1
	EntityParent cube,c\e
	
	EntityType c\e,car_col
	EntityRadius c\e,1
	
	PositionEntity c\e,c\x,c\y,c\z
	
	Return c
	
End Function

Function carupdate()
	
	For c.car=Each car
	
		c\vy#=c\vy#+gravity#
		
		c\vx#=c\vx#+c\ax#
		c\vy#=c\vy#+c\ay#
		c\vz#=c\vz#+c\az#
		
		c\x#=EntityX(c\e)
		c\y#=EntityY(c\e)
		c\z#=EntityZ(c\e)
		
		TranslateEntity c\e,c\vx,c\vy,c\vz
	
	Next
	
	UpdateWorld()
	
	For c.car=Each car
		
		;correct velocity if collided
		c\vx=(EntityX(c\e)-c\x)
		c\vy=(EntityY(c\e)-c\y)
		c\vz=(EntityZ(c\e)-c\z)
		
		
		;slow down due to friction
		If EntityCollided(c\e,world_col)
			
			c\vx#=c\vx*0.98
			c\vy#=c\vy*0.98
			c\vz#=c\vz*0.98
		
		EndIf
		
		c\ax#=0
		c\ay#=0
		c\az#=0
		
	Next
	
End Function


Function carcontrol()
	
	For c.car=Each car
		
		If KeyDown(200)
			
			TFormVector 0,0,0.02,c\e,0
			
			c\ax#=TFormedX()
			c\ay#=TFormedY()
			c\az#=TFormedZ()
			
		EndIf
		
		If KeyDown(208)
			
			c\vx=c\vx*0.99
			c\vy=c\vy*0.99
			c\vz=c\vz*0.99
			
		EndIf
		
		If KeyDown(57)
			
			TFormVector 0,0.05,0,c\e,0
			
			c\ax#=c\ax+TFormedX()
			c\ay#=c\ay+TFormedY()
			c\az#=c\az+TFormedZ()
			
									
		EndIf
		
		If KeyDown(203)
			
			TurnEntity c\e,0,2,0
			
		EndIf
		
		If KeyDown(205)
			
			TurnEntity c\e,0,-2,0
			
		EndIf
		
		
	Next
	
	
	
End Function


Can somebody please explain to me what this means. Even if you know parts please help me understand

Thanks

If anyone has more understandable or better car physics then please help me and post it up, i'd much appreciate it.
The stuff above is really what i am looking for but i would like to incorporate these to my own models..not the cube. Unfortunately, since i dont know which parts of the code are which, i am unable to do this.

Please help if you can
Thanks

if anyone has more understandable or better car physics...

If you understand nothing from the code above then I advise
you to start learning Blitz3D commands.
If you have specific question then ask

I understand all hte commands above, its just that i dont understand the authors' coding. For example, i am having trouble finding which part makes the 'car' move up when it collides.. stuff like that. I get the code until about half way. Here is where i get lost:

car.car=carnew(0,5,0)

Repeat
TFormPoint 0,3,-5,car\e,0

dx#=(TFormedX()-EntityX(camera))*.1
dy#=(TFormedY()-EntityY(camera))*.1
dz#=(TFormedZ()-EntityZ(camera))*.1

TranslateEntity camera,dx,dy,dz

PointEntity camera,car\e

carcontrol()
carupdate()

RenderWorld()

Flip


Until KeyDown(1)
End


Type car

Field e ;entity

Field x#,y#,z# ; position in 3d-space

Field vx#,vy#,vz# ; velocity

Field ax#,ay#,az# ; acceleration

End Type


Function carnew.car(x#,y#,z#)
	
	c.car=New car
	
	c\x#=x#
	c\y#=y#
	c\z#=z#
	
	c\e=CreateCube()
	cube=CreateCube()
	ScaleEntity cube,0.3,0.3,0.3
	PositionEntity cube,0,0,1
	EntityParent cube,c\e
	
	EntityType c\e,car_col
	EntityRadius c\e,1
	
	PositionEntity c\e,c\x,c\y,c\z
	
	Return c
	
End Function

Function carupdate()
	
	For c.car=Each car
	
		c\vy#=c\vy#+gravity#
		
		c\vx#=c\vx#+c\ax#
		c\vy#=c\vy#+c\ay#
		c\vz#=c\vz#+c\az#
		
		c\x#=EntityX(c\e)
		c\y#=EntityY(c\e)
		c\z#=EntityZ(c\e)
		
		TranslateEntity c\e,c\vx,c\vy,c\vz
	
	Next
	
	UpdateWorld()
	
	For c.car=Each car
		
		;correct velocity if collided
		c\vx=(EntityX(c\e)-c\x)
		c\vy=(EntityY(c\e)-c\y)
		c\vz=(EntityZ(c\e)-c\z)
		
		
		;slow down due to friction
		If EntityCollided(c\e,world_col)
			
			c\vx#=c\vx*0.98
			c\vy#=c\vy*0.98
			c\vz#=c\vz*0.98
		
		EndIf
		
		c\ax#=0
		c\ay#=0
		c\az#=0
		
	Next
	
End Function


Function carcontrol()
	
	For c.car=Each car
		
		If KeyDown(200)
			
			TFormVector 0,0,0.02,c\e,0
			
			c\ax#=TFormedX()
			c\ay#=TFormedY()
			c\az#=TFormedZ()
			
		EndIf
		
		If KeyDown(208)
			
			c\vx=c\vx*0.99
			c\vy=c\vy*0.99
			c\vz=c\vz*0.99
			
		EndIf
		
		If KeyDown(57)
			
			TFormVector 0,0.05,0,c\e,0
			
			c\ax#=c\ax+TFormedX()
			c\ay#=c\ay+TFormedY()
			c\az#=c\az+TFormedZ()
			
									
		EndIf
		
		If KeyDown(203)
			
			TurnEntity c\e,0,2,0
			
		EndIf
		
		If KeyDown(205)
			
			TurnEntity c\e,0,-2,0
			
		EndIf
		
		
	Next
	
	
	
End Function


if anyone knows what this means, or at least parts of it, then please tell me or add comments (;) to the code as it has none.

Thanks

It's using staight blitz collsions nothing special. The lines below set this up.

[code]
Const car_col=1
Const world_col=2

Collisions car_col,world_col,2,2
[\code]

The car is created with type car_col and everything else with world_col. When the "car" collides with somthing it slides over and around it as per the collision parameters above.

ok, i have read and reread that code about 100 times and i still dont understand what i would replace or add/take away from the code to allow my model to have the exact same properties as the cube does in this example.
Lets say my model is a sphere instead of the cube that acts as the car above. What parts of the code would i take away, replace or change so that the player would now drive a sphere around the 'world' instead of that cube.

Please does anyone know
Quite desperate... :0(

I agree, you should learn more blitz ;o)

From the above, he uses a create_car function. That gives you a clue :o) And since he's creating a cube, the CreateCube() function further narrows it down:

Function carnew.car(x#,y#,z#)
	
	c.car=New car
	
	c\x#=x#
	c\y#=y#
	c\z#=z#
	
	c\e=CreateCube()
	cube=CreateCube()
	ScaleEntity cube,0.3,0.3,0.3
	PositionEntity cube,0,0,1
	EntityParent cube,c\e
	
	EntityType c\e,car_col
	EntityRadius c\e,1
	
	PositionEntity c\e,c\x,c\y,c\z
	
	Return c
	
End Function


Try changing the two above CreateCube() functions to whatevershape you want and see if that works :o)

ok erm...
here is the problem, the models which i want to use (which are modeled in blitz3d using cubes and spheres *See Here*) are made up of much more code than just createsphere/cube as i used many spheres/cubes. Then i parented everything to one of the parts of the model (a cube named 'body').

So...where do i paste all the code and what will i need to change? :(

Thanks

In here:

	c\e= loadmesh("yourmodel.b3d")
        cube= loadmesh("yourmodel.b3d")


What i'm unsure of though, is the two cubes. I assume one of them is for collisions, so play about with it and i'm sure you'll get it. Or, send me the code + model you want to my email and i'll have a play about with it.

ok Ross, thanks so much. I have sent the code of one of the models to your e-mail together with the code of the example.
Please tell me if you have figuered something out, i'll be trying too in the meantime.

Thanks again.

Then i parented everything to one of the parts of the model (a cube named 'body').

Change the following function:
Function carnew.car(x#,y#,z#)
	
	c.car=New car
	
	c\x#=x#
	c\y#=y#
	c\z#=z#
	
	c\e=CreateCube()
	cube=CreateCube()
	ScaleEntity cube,0.3,0.3,0.3
	PositionEntity cube,0,0,1
	EntityParent cube,c\e
	
	EntityType c\e,car_col
	EntityRadius c\e,1
	
	PositionEntity c\e,c\x,c\y,c\z
	
	Return c
	
End Function


to this:
Function carnew.car(x#,y#,z#, carmodel%)
	
	c.car=New car
	
	c\x#=x#
	c\y#=y#
	c\z#=z#
	
	c\e=CreateCube()
	PositionEntity carmodel,0,0,1
	EntityParent carmodel,c\e
	
	EntityType c\e,car_col
	EntityRadius c\e,1
	
	PositionEntity c\e,c\x,c\y,c\z
	
	Return c
	
End Function


and change the following line:
car.car=carnew(0,5,0)


to
car.car=carnew(0,5,0, body)


Thank you so much, it finally works, i just ave one problem...
when i run the program, i see my model with a bigish cube sticking out of the top..
When i try scaling it the collisions dont really work anymore, any ideas on how to fix this?

Thanks so much anyway

Try to change the value of 1 in line of function 'carnew'
EntityRadius c\e,1

to a larger or smaller value
But I insist to read the Blitz3D manual... :)

Oh, its ok , i figuered it out.
I set EntityAlpha to 0 for c\e:
EntityAlpha c\e, 0


Thanks for all the help, i will probably be back asking for more later anyway.

Bye for now

Dam, just got back too :oP Glad you got it fixed though!