Bumping cars and reacting accordingly

Blitz3D Forums/Blitz3D Beginners Area/Bumping cars and reacting accordingly

pimpom(Posted 2008) [#1]
Hi, I have two cars, and am using distance (as if they were circles) to check if they are colliding or not. Goal is to get affected by each other speed, not very interested in ading friction / mass. I don't need accuracy on the reaction, but something visible that looks "real" (for a game, that is).

After determining they have "collided" (if distance is less than radius1 + radius2 + small treshold), how can I affect their movement?

I think I need to convert angle and speed of each car to x,y components, using current speed as magnitude for the vector:

        angle=entityyaw(car1,1)
	cX1#=speed#*Cos(angle) 
	cY1#=speed#*Sin(angle); that would be z on 3d world
        
        angle=entityyaw(car2,1)
	cX2#=speed#*Cos(angle) 
	cY2#=speed#*Sin(angle); that would be z on 3d world

;and then add data to get resulting speed
        rX#=cX1#+cX2#
        rY#=cY1#+cY2#



I'm using keypress to add speed or decrease, left and right turns entity +/- 1.

How can I add those external forces into my cars?

Is this approach ok?
Some other solution? (without using physics libraries).

Thanks.


Stevie G(Posted 2008) [#2]
This may look ok ....

Distance# = EntityDistance( Car1 , Car2 )

If Distance < ( Rad1 + Rad2 )

	Overlap# = Rad1 + Rad2 - Distance
	TotalSpeed# = Speed1 + Speed2
	
	;get collision normal
	Nx# = ( EntityX( Car1 ) - EntityX( Car2 ) ) / Distance
	Nz# = ( EntityZ( Car1 ) - EntityZ( Car2 ) ) / Distance
		
	;move car 1 out of collision dependent on speed factor
	Factor# = Overlap * ( Speed2 / TotalSpeed )
	TranslateEntity Car1, Nx * Factor , 0 , Nz * Factor
	;turn car slightly in direction of normal
	AlignToVector Car1, Nx , 0 , Nz , 3, .1
	;reduce speed slghtly
	Speed1 = Speed1 * .75
	
	;move car 2 out of collision dependent on speed factor
	Factor# = Overlap * ( Speed1 / TotalSpeed )
	TranslateEntity Car2, -Nx * Factor , 0, -Nz * Factor
	;turn car slightly in direction of normal
	AlignToVector Car2, -Nx , 0 , -Nz , 3, .1
	;reduce speed slghtly
	Speed2 = Speed2 * .75

EndIf


If you want proper collisions ( not necesarily physically accurate ) you'll need to hold the velocity x,z components separately for each vehicle and use some vector reflection code. Also, holding the velocity components separately allows you to do many other tricks .. like drifting and whatnot ..

Stevie


Rob Farley(Posted 2008) [#3]
Personally with this sort of thing I find it easiest to have a 'physics pivot' to keep track of all the forces on the car.

So to accelerate the car simply rotate the physpiv to the correct angle and apply a forward force. Each update move it towards the origin (I usually multiply the x,y and z coords by .9) this adds the friction.
Any external forces just translate the physpiv in the direction of the force.

Once you've added all external forces simply translate your main model by the x,y and z coords of the physpiv.


pimpom(Posted 2008) [#4]
Hey, thank you very much!!

At the time being I better study more on vectors. And maybe look further into drifting and adding some friction.

I will try both methods, seems a nice idea to have extra pivot (was wondering on how to better do some car animation in code).

thanks again!!