Problems with Angular Movement

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
Manello
Prole
Posts: 7
Joined: Thu May 29, 2014 5:11 pm

Problems with Angular Movement

Post 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)
Attachments
VIR.love
(12.21 KiB) Downloaded 118 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Problems with Angular Movement

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Manello
Prole
Posts: 7
Joined: Thu May 29, 2014 5:11 pm

Re: Problems with Angular Movement

Post by Manello »

Nice it works, thanks!
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Problems with Angular Movement

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Manello
Prole
Posts: 7
Joined: Thu May 29, 2014 5:11 pm

Re: Problems with Angular Movement

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests