Turret turning/angle example with decceleration

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Turret turning/angle example with decceleration

Post by markgo »

Left click to turn the turret.

Keywords: turret,angle,acceleration,decceleration,rotation

The code in case the demo goes down:

Code: Select all

turn_line = {400,300,0,0}
look_angle= 0
turn_speed= 2
accel     = 0
turret_r  = 100

function love.mousepressed(x,y,b)
	if b == 'l' then 
		turn_line[3],turn_line[4] = x,y
	end
end

function love.update(dt)
	x,y,x2,y2 = unpack(turn_line)
	old_turn  = turn_angle
	turn_angle= math.atan2( y2-y, x2-x ) % (2*math.pi)
	look_angle= look_angle % (2*math.pi)
	
	min_disp_angle = turn_angle-look_angle
	if min_disp_angle < -math.pi then min_disp_angle = 2*math.pi+min_disp_angle end
	if min_disp_angle > math.pi then min_disp_angle = -2*math.pi+min_disp_angle end
	
	if math.abs(min_disp_angle) < 0.01 then turn_speed = 0; accel = 0
	elseif old_turn ~= turn_angle then
		if min_disp_angle < 0 then turn_speed = -2 
		elseif min_disp_angle > 0 then turn_speed = 2 end
		
		-- http://hyperphysics.phy-astr.gsu.edu/hbase/mot.html
		accel      = -(turn_speed^2)/(2*min_disp_angle)
	end
	
	dv         = accel*dt
	look_angle = look_angle + dt*(turn_speed + dv/2)
	turn_speed = turn_speed+dv
end

function love.draw()
	love.graphics.line(unpack(turn_line))
	
	dx,dy = math.cos(look_angle),math.sin(look_angle)
	dx,dy = dx*turret_r,dy*turret_r
	love.graphics.line(400,300,400+dx,300+dy)
	
	love.graphics.print('Displacement angle: '..math.deg(min_disp_angle),0,0)
	love.graphics.print('Turn angle: '..math.deg(turn_angle),0,15)
	love.graphics.print('Look angle: '..math.deg(look_angle),0,30)
end
Attachments
turret.love
(700 Bytes) Downloaded 139 times
Last edited by markgo on Tue Jun 18, 2013 6:15 pm, edited 1 time in total.
User avatar
NightKawata
Party member
Posts: 294
Joined: Tue Jan 01, 2013 9:18 pm
Location: Cyberspace, Room 6502
Contact:

Re: Turret turning/angle example with decceleration

Post by NightKawata »

Where's the turret? All I see are a bunch of demos including parallax scrolling and isotometric maps.
Plus, this is a demo, not a youtube video; 's with the keywords? :P

EDIT: Oh. You fixed it.
"I view Python for game usage about the same as going fishing with a stick of dynamite. It will do the job but it's big, noisy, you'll probably get soaking wet and you've still got to get the damn fish out of the water." -taylor
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Turret turning/angle example with decceleration

Post by Ref »

OK!
So now he has a turret.
And the next thing, he probably wants to fire it.
Attachments
turret_plus.love
Just added a turret
(65.64 KiB) Downloaded 123 times
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Turret turning/angle example with decceleration

Post by jjmafiae »

nice :3
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests