Page 1 of 1

[Solved] Need help with rotation.

Posted: Sun May 19, 2013 9:25 pm
by zoboomafoo
This is my 1st time posting on a forum so i don't really know if this is in the right section or what ever but...

I just started using love2d this morning and been doing good using the documentation until a couple of minutes ago.
I'm trying to make a 2d space combat game and i'm trying to make the ship rotate slowly to the mouse in the shortest distance. I have no idea how to do this and all my attempts just end up spinning wildly in one direction or dose fine until it goes from 180deg to -180deg where it goes back all the way around the other direction... please help :cry:

Re: Rotate slowly to mouse.

Posted: Sun May 19, 2013 11:57 pm
by zoboomafoo
No one can help me? :(

Re: Rotate slowly to mouse.

Posted: Mon May 20, 2013 12:08 am
by veethree
So if the mouse is moved on for example the x axis the ship will rotate?

Re: Rotate slowly to mouse.

Posted: Mon May 20, 2013 12:31 am
by zoboomafoo
veethree wrote:So if the mouse is moved on for example the x axis the ship will rotate?
Any axis. I want the ship to slowly turn towards the mouse so you cant just instantly turn around backward and blap something behind you. so lets say the angle the ship is is 1.8 rads and the desired angle is -2 rads how do i get the ship to rotate at a set, constant speed toward the desired angle in the shortest direction?

Re: Need help with rotation.

Posted: Mon May 20, 2013 8:18 am
by mode7
zoboomaffoo,
what you*re trying to do is quite possible. For doing thse things I'd strongly reccomend a vector library. HUMP has one and I'm using my own with operator overloading support which simplifies vector operations a lot. Using a vector library really makes your life much easier when doing these operations.

Ok let's try to solve your problems:If I understood you right you want to rotate the ship towards the mouse x and y position.
I'm doing this in a pseudo code kind of way as I don't know your code!

Step 1: Getting a distance vector from the ship to the mouse --> ships position - mouse position

Distance.x, Distance.y = ship.x - mouse.x, ship.x - mouse.y

Step2: Geting the angular direction of our distance vector

Direction = math.atan2(Distance.y, Distance.x)

Step3: Comparing the shortest distance between the two angles.

angleDistance = math.min(Direction - ship.rotation, ship.rotation - Direction)

I'm storing the target rotation in the ships table:
ship.target = ship.rotation + angleDistance

Step 4: Rotation the ship towards the target in one second

Do this in love.udate()

if ship.rotation ~= ship.target then
ship.rotation = ship.rotation + angleDistance*dt
end

This is totally untested but you might get the idea. Feel free to ask if you don't understand a part or if it doesn't work as expected

Re: Need help with rotation.

Posted: Tue May 21, 2013 8:39 pm
by zoboomafoo
mode7 wrote: Feel free to ask if you don't understand a part or if it doesn't work as expected
The ship is just spinning around counter clockwise :| i will just post the .love file.
For doing thse things I'd strongly reccomend a vector library. HUMP has one and I'm using my own with operator overloading support which simplifies vector operations a lot. Using a vector library really makes your life much easier when doing these operations.
I got the HUMP library but dont get what it dose.

Re: Need help with rotation.

Posted: Wed May 22, 2013 3:10 pm
by mode7
Okay I kinda fixed it, the bigger problem seems to be to get the shortest distance to rotate the ship towards especially if it crosses a rotation of 0. Maybe I'm just stupid right now.

Code: Select all

function ship_Update(dt)

	ship.speed = math.ceil(math.sqrt((math.pow(ship.xVel*10,2))+(math.pow(ship.yVel*10,2)))*10)
	mouse = {}
	mouse.x = love.mouse.getX()
	mouse.y = love.mouse.getY()

	rotspeed = 1

	newAngle = math.atan2((mouse.y - ship.y), (mouse.x - ship.x))
	newAngle = newAngle % (2*math.pi)
	
	angleDistance = 0
	
	if math.abs(newAngle - ship.angle) < math.abs(ship.angle - newAngle) then
		angleDistance = newAngle - ship.angle
	else
		angleDistance = newAngle - ship.angle
	end
	
	ship.target = ship.angle + angleDistance

	if ship.angle ~= ship.target then
		ship.angle  = ship.angle + angleDistance * (dt*rotspeed)
	end
	--ship.angle = newAngle 
	ship.exel = ship.thrust/ship.mass*0.1

	ship.xVel = ship.xVel + ship.exel * math.cos(ship.angle) * dt
    ship.yVel = ship.yVel + ship.exel * math.sin(ship.angle) * dt

	ship.x = ship.x + ship.xVel
	ship.y = ship.y + ship.yVel
	
	ship.yVel = ship.yVel * (1 - math.min(dt * 1, 1))
	ship.xVel = ship.xVel * (1 - math.min(dt * 1, 1))


	if love.keyboard.isDown("w") then
		if ship.thrust < ship.maxThrust then
			ship.thrust = ship.thrust + (ship.maxThrust/5) * dt
    	else
    		thrust = maxThrust
    	end
    end

    if love.keyboard.isDown("s") then
    	if ship.thrust > 0 then
    		ship.thrust = ship.thrust - (ship.maxThrust/10) * dt
    	else
    		ship.thrust = 0
    	end
    end

    if love.keyboard.isDown("x") then
    	ship.thrust=0
    end
end

Re: Need help with rotation.

Posted: Wed May 22, 2013 3:26 pm
by micha
mode7 wrote:

Code: Select all

	if math.abs(newAngle - ship.angle) < math.abs(ship.angle - newAngle) then
		angleDistance = newAngle - ship.angle
	else
		angleDistance = newAngle - ship.angle
	end
This is an error I suppose? The then and else branch both do the same thing.

Besides that: Starting from the ship.angle and the newAngle you can calculate the relative/difference-angle by this:

Code: Select all

angleDistance = (newAngle-ship.angle+math.pi)%(2*math.pi)-math.pi
The result is always between -pi and pi (which is -180° and 180°).
The sign of angleDistance tells you, if you need to turn clockwise or counter-clockwise. (Or do it like you already do and multiply angleDistance by a rotational speed and dt)

Re: [Solved] Need help with rotation.

Posted: Wed May 22, 2013 9:51 pm
by zoboomafoo
Thank you micha that equation is exactly what i was looking for.
Tanks to everyone else, mode7 ;)