[Solved] Need help with rotation.

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.
zoboomafoo
Prole
Posts: 7
Joined: Sun May 19, 2013 9:05 pm

[Solved] Need help with rotation.

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
Last edited by zoboomafoo on Wed May 22, 2013 9:48 pm, edited 4 times in total.

zoboomafoo
Prole
Posts: 7
Joined: Sun May 19, 2013 9:05 pm

Re: Rotate slowly to mouse.

No one can help me?

veethree
Inner party member
Posts: 820
Joined: Sat Dec 10, 2011 7:18 pm

Re: Rotate slowly to mouse.

So if the mouse is moved on for example the x axis the ship will rotate?

zoboomafoo
Prole
Posts: 7
Joined: Sun May 19, 2013 9:05 pm

Re: Rotate slowly to mouse.

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?

mode7
Prole
Posts: 35
Joined: Tue Aug 21, 2012 5:45 pm
Contact:

Re: Need help with rotation.

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

zoboomafoo
Prole
Posts: 7
Joined: Sun May 19, 2013 9:05 pm

Re: Need help with rotation.

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.
Attachments
space.love
Umm why dose it say conf.dat dosent exist when it is in the .love file?

mode7
Prole
Posts: 35
Joined: Tue Aug 21, 2012 5:45 pm
Contact:

Re: Need help with rotation.

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

micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Need help with rotation.

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)

zoboomafoo
Prole
Posts: 7
Joined: Sun May 19, 2013 9:05 pm

Re: [Solved] Need help with rotation.

Thank you micha that equation is exactly what i was looking for.
Tanks to everyone else, mode7

Who is online

Users browsing this forum: No registered users and 55 guests