2D Car Physics

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Jakus
Prole
Posts: 6
Joined: Tue May 28, 2013 7:56 am

2D Car Physics

Post by Jakus »

Hello everyone! :awesome:

This is my first post on here, but have been using Love for about 6 month on and off as a hobbyist. It's simple, fun and awesome to work with. 3D version would be nice... but hey, I just got here haha. Keep up the good work! :)

Anyway, I was looking to see if someone had a good example of 2D top view 'realistic' car physics? Something with mass, friction and gear implementation. Even a good tutorial with source codes in Love would be perfect. I've tried a bunch of different tutorials, but cant get any of them to look or feel the way I want.

Thanks in advance.
Jakus
Prole
Posts: 6
Joined: Tue May 28, 2013 7:56 am

Re: 2D Car Physics

Post by Jakus »

I've ported some of Marco Monster's code now, but cant get it to work.

Heres my version of the code:

Code: Select all

function love.load()
	cartype = {
		b = 1,
		c = 1,
		wheelBase = 1 + 1,
		h = 1,
		mass = 1000,
		inertia = 1500,
		width = 1.5,
		length = 3.0,
		wheellength = 0.7,
		wheelwidth = 0.3
	}

	car = {
		cartype = cartype,
		position = {x = 400,y = 400},
		velocity = {x = 0,y = 0},
		angle = 0,
		avelocity = 0,
		steerangle = 0,
		throttle = 0,
		brake = 0
	}
end

function math.sign(x)
   if x<0 then
     return -1
   elseif x>0 then
     return 1
   else
     return 0
   end
end

function do_physics(dt)

	local drag,rest,caR,caF,maxGrip = 5.0,30.0,-5.20,-5.0,2.0
	
	local sn = math.sin(car.angle)
	local cs = math.cos(car.angle)
	
	velocity = {}
	velocity.x = cs * car.velocity.y + sn * car.velocity.x;
	velocity.y = -sn * car.velocity.y + cs * car.velocity.x;
	
	yawspeed = car.cartype.wheelBase * 0.5 * car.avelocity
	
	if velocity.x == 0 then
		rot_angle = 0
	else
		rot_angle = math.atan2(yawspeed,velocity.x)
	end
	
	if velocity.x == 0 then
		sideslip = 0
	else
		sideslip = math.atan2( velocity.y, velocity.x);
	end
	
	slipanglefront = sideslip + rot_angle - car.steerangle
	slipanglerear  = sideslip - rot_angle
	
	weight = car.cartype.mass * 9.8 * 0.5
	
	flatf = {}
	flatf.x = 0;
	flatf.y = caF * slipanglefront;
	flatf.y = math.min(maxGrip, flatf.y);
	flatf.y = math.max(-maxGrip, flatf.y);
	flatf.y = flatf.y  * weight;

	flatr = {}
	flatr.x = 0;
	flatr.y = caR * slipanglerear;
	flatr.y = math.min(maxGrip, flatr.y);
	flatr.y = math.max(-maxGrip, flatr.y);
	flatr.y = flatr.y  * weight;
	
	ftraction = {}
	ftraction.x = 100*(car.throttle - car.brake*math.sign(velocity.x));	
	ftraction.y = 0;
	
	resistance = {}
	resistance.x = -(rest*velocity.x+drag*velocity.x*math.abs(velocity.x))
	resistance.y = -(rest*velocity.y+drag*velocity.y*math.abs(velocity.y))
	
	force = {}
	force.x = ftraction.x + math.sin(car.steerangle) * flatf.x + flatr.x + resistance.x;
	force.y = ftraction.y + math.cos(car.steerangle) * flatf.y + flatr.y + resistance.y;

	torque = car.cartype.b * flatf.y - car.cartype.c * flatr.y;	
	
	acceleration = {}
	acceleration.x = force.x/car.cartype.mass;
	acceleration.y = force.y/car.cartype.mass;
	
	angular_acceleration = torque / car.cartype.inertia;
	
	acceleration_wc = {}
	acceleration_wc.x =  cs * acceleration.y + sn * acceleration.x;
	acceleration_wc.y = -sn * acceleration.y + cs * acceleration.x;

	car.velocity.x = car.velocity.x + dt * acceleration_wc.x;
	car.velocity.y = car.velocity.y + dt * acceleration_wc.y;

	car.position.x = car.position.x + dt * car.velocity.x;
	car.position.y = car.position.y + dt * car.velocity.y;
	
	car.avelocity = car.avelocity + dt * angular_acceleration;

	car.angle = car.angle + dt * car.avelocity ;
	
end


function love.update(dt)
	if love.keyboard.isDown("w") then car.throttle = car.throttle + 100 end
	
	if love.keyboard.isDown("a") then
		car.steerangle = car.steerangle - math.rad(15)
	end
	
	if love.keyboard.isDown("d") then 
		car.steerangle = car.steerangle + math.rad(15)
	end
	do_physics(dt)
end

function love.draw()
	gr.circle("fill",car.position.x, car.position.y,3)
end
Might be something to do with deg to rads or vice-versa?
User avatar
easy82
Party member
Posts: 184
Joined: Thu Apr 18, 2013 10:46 pm
Location: Hungary

Re: 2D Car Physics

Post by easy82 »

Use the built-in physics for this task if you want proper collisions later (with cars, other objects), because when it comes to rotation, higher speeds, other objects, it gets non-trivial very quickly.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: 2D Car Physics

Post by micha »

Hi Jakus,

welcome to the forum.

You can do two things, to make it easier for us to help you:
1) upload a .love-file (put the source code into a .zip file and rename the file-extension to .love)
2) say exactly, what is not working. What are you expecting and what happens, when you run your code?
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: 2D Car Physics

Post by veethree »

easy82 wrote:Use the built-in physics for this task if you want proper collisions later (with cars, other objects), because when it comes to rotation, higher speeds, other objects, it gets non-trivial very quickly.
Or you could use HardonCollider.
Jakus
Prole
Posts: 6
Joined: Tue May 28, 2013 7:56 am

Re: 2D Car Physics

Post by Jakus »

Haha, yep gave up on custom physics for now. Trying to mess around with love.physics now and get a good feeling car. Any tips on this? I've got it feeling almost right
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests