Page 1 of 1

Problems with Angular Movement

Posted: Sun Sep 13, 2015 9:29 pm
by Manello
Hello There,

the spaceship in my program should be moving in the direction which the front of it is pointing to. So it should move when i press "W" into the angular direction in which the spaceship is pointing. You can move in the opposite direction with "S", and with "A" and "D" the spaceship will rotate.

Now, as long as you dont rotate anything it will move up and down just as it should do it. And if you rotate the spaceship, it will get displayed in the correct angle, but when you try to move in the direction it is pointing, ehr, yeah it is doing what it wants to do, and not what it sould do.

Just have a look at the love file and you will see the problem.
(Ignore the improvised tilemap in the background, it is only optical and was a test)

Here's the part of code which I think the problem is in:

Code: Select all

		--Generate Speed
		checkW = love.keyboard.isDown(cConfig[0],cConfig[1])
		checkS = love.keyboard.isDown(cConfig[2],cConfig[3])
		checkA = love.keyboard.isDown(cConfig[4],cConfig[5])
		checkD = love.keyboard.isDown(cConfig[6],cConfig[7])
		
		
		if checkA == true then
			units[unitselect].body:applyTorque(-units[unitselect].rotspeed)
		end
		if checkD == true then
			units[unitselect].body:applyTorque(units[unitselect].rotspeed)
		end

		local forceX = ((100/360)*math.deg(units[unitselect].body:getAngle()))*units[unitselect].accleration
		local forceY = units[unitselect].accleration-forceX
		if checkW == true then
			local angle = math.deg(units[unitselect].body:getAngle())
			if angle < 90 then
				units[unitselect].body:applyForce(forceX, -forceY)
			elseif angle < 180 then
				units[unitselect].body:applyForce(forceX, forceY)
			elseif angle < 270 then
				units[unitselect].body:applyForce(-forceX, forceY)
			elseif angle < 360 then
				units[unitselect].body:applyForce(-forceX, -forceY)
			end
		end
		if checkS == true then
			angle = math.deg(units[unitselect].body:getAngle())
			if angle < 90 then
				units[unitselect].body:applyForce(-forceX, forceY)
			elseif angle < 180 then
				units[unitselect].body:applyForce(-forceX, -forceY)
			elseif angle < 270 then
				units[unitselect].body:applyForce(forceX, -forceY)
			elseif angle < 360 then
				units[unitselect].body:applyForce(forceX, forceY)
			end
		end
		
		
		level[roomselect].world:update(dt)

Re: Problems with Angular Movement

Posted: Sun Sep 13, 2015 9:34 pm
by s-ol
Your x/y force calculation is... weird. What you want is basic trigonometry:

Image

Code: Select all

local angle = units[unitselect].body:getAngle()
local x = math.cos(angle) * units[unitselect].accleration
local y = math.sin(angle) * units[unitselect].accleration
units[unitselect].body:applyForce(y, x)
is all you need.

Re: Problems with Angular Movement

Posted: Mon Sep 14, 2015 6:39 pm
by Manello
Nice it works, thanks!

Re: Problems with Angular Movement

Posted: Mon Sep 14, 2015 7:25 pm
by s-ol
Manello wrote:Nice it works, thanks!
If you don't know why/how that works, invest some time in learning trig with a youtube video or something.

I don't know if this is good, might be worth a shot: http://www.raywenderlich.com/35866/trig ... ing-part-1

Re: Problems with Angular Movement

Posted: Mon Sep 14, 2015 7:39 pm
by Manello
Afterwarts i know how this is working, schools trigonometry isnt that far back, but i think
the topics in this article like bouncing are also helpful.