[Solved] Offset turret help

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.
SPlice
Prole
Posts: 48
Joined: Thu Jul 28, 2011 8:53 am

[Solved] Offset turret help

Post by SPlice »

So i have been working on getting turrets to track the mouse. They need to be offset but the offset needs to rotate with the ship =S i tried using the rotated vector but that does not work, anyone have any tips or know of a similar bit of code I can pull apart?

Code: Select all

t1 = vector(pos.x+50, pos.y+ 50)
t1r = center:rotated(-angle) -- this is where i tried to rotate the vector
t1x, t1y = t1r:unpack()
love.mouse.setVisible( false )

cam = camera(vector(pos.x, pos.y), s)

cam:predraw()
love.graphics.draw(stars, 0,0 )
    love.graphics.draw(ship, pos.x, pos.y, angle, .5, .5, offx, offy)
	love.graphics.draw(turret,t1x, t1y, thed, .5, .5, 7, turret:getHeight()/2 )
Last edited by SPlice on Sat Aug 27, 2011 10:33 pm, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Offset turret help

Post by Robin »

Didn't we have a thread like this yesterday?

Code: Select all

angle = math.atan2(yobject - ymouse, xmouse - xobject)
Replace [x/y][mouse/object] by the appropriate variable names.
Help us help you: attach a .love.
SPlice
Prole
Posts: 48
Joined: Thu Jul 28, 2011 8:53 am

Re: Offset turret help

Post by SPlice »

I think you misunderstood the problem, I can get stuff to track the mouse, but having the turret offset and still move with the ship is the problem here take a look you will understand when you rotate the ship. this is my first love project and I haven't done any coding before I am sure its a mess =S but the part that deals with the turret offset is on line 163. I know it has tracking issues when you move around but one thing at a time...
Attachments
planetfall.love
(2.56 MiB) Downloaded 256 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Offset turret help

Post by Robin »

Then I still don't know what your question is. Unless it has to do with the fact that as you move the ship around, the "camera" coordinates aren't added up to the mouse cursor, causing those issues with steering the turret.
Help us help you: attach a .love.
SPlice
Prole
Posts: 48
Joined: Thu Jul 28, 2011 8:53 am

Re: Offset turret help

Post by SPlice »

when rotating the ship the turret wanders from where it should be. notice how it moves away from where it should be when the ship moves. The turret is offset because there will be groups of 4 turrets, so the x and y values that tell the turret image where to draw need to move around the center of the ship as the ship rotates. the turret is offset x+15, y+15 from the center where the ship rotates, somehow that offset needs to translate into a rotated vector or something. I really appreciate you taking the time to look at this, if your sick of it don't worry about it =/
Attachments
problem.png
problem.png (168.05 KiB) Viewed 8120 times
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Offset turret help

Post by kraftman »

SPlice wrote:when rotating the ship the turret wanders from where it should be. notice how it moves away from where it should be when the ship moves. The turret is offset because there will be groups of 4 turrets, so the x and y values that tell the turret image where to draw need to move around the center of the ship as the ship rotates. the turret is offset x+15, y+15 from the center where the ship rotates, somehow that offset needs to translate into a rotated vector or something. I really appreciate you taking the time to look at this, if your sick of it don't worry about it =/
Its the same as the equation you used to rotate the turret, but rearranged.

When you work out the angle of the turret to the mouse, you calculate the opposite and adjacent((yobject-ymouse) and (xobject-xmouse)) to calculate the angle between the turret and the mouse.

When positioning the turret on the ship, you know the angle, and initial opposite and adjacent, however to calculate the new opposite and adjacent, you need to work out the hypotenuse.

My maths isn't too hot but:

sin(angle) = opp/hyp
cos(angle) = adj/hyp
tan(angle) = opp/adj

so you can rearrange to get what you need.
I'm sure this could be more helpful, but at least i understood the question ><
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Offset turret help

Post by TechnoCat »

Or, use the x and y offsets in love.graphics.draw (ox/oy). Then it will rotate around a specified point without doing any trigonometry beforehand.
SPlice
Prole
Posts: 48
Joined: Thu Jul 28, 2011 8:53 am

Re: Offset turret help

Post by SPlice »

Well I got this all worked out and I thought I would post the code I used so anyone facing a similar problem might be able to find and use it.

Code: Select all

dist = math.sqrt(a^2+b^2)
if b < 0 then
am = -1
elseif b >= 0 then
am = 1
end
a2 = (math.asin (a / (math.sqrt(a^2 + b^2))))
tx = math.cos((r  * am) + a2) * dist  * am + x 
ty = math.sin((r * am) + a2) * dist + y
r = rotation of the base object
x and y = for the position of the base object
a and b = the input positions of the turret or whatever else you want to move with the base object
tx and ty the real world positions that get used to place the turret in its correct location

work out the rotation for the turret however you want. you can give it whatever a and b values you like to move it around on its own independent Cartesian plane that will rotate correctly with respect to the base.

basically it converts from x,y to polar and back again after adding the angles

Thanks everyone who helped me figure this out :)

does anyone know how I could create a function or something out of this so that it could be easily applied to many objects?
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: [Solved] Offset turret help

Post by Rad3k »

I think the most general and reusable solution would be to write functions to convert between global and local coordinates. Here's a function to translate from local to global, which is what you're doing now:

Code: Select all

function local2global (x, y, a, refx, refy, refa)
	local dist = (x^2 + y^2) ^ 0.5
	local angle = math.atan2(y, x)

	local angle2 = refa + angle
	local globx = refx + dist * math.cos(angle2)
	local globy = refy + dist * math.sin(angle2)
	local globa = refa + a
	return globx, globy, globa
end
In the attachment there's a simple demo to illustrate the use. Controls are: W, A, D to move the ship, and Q, E to rotate the turret.

Depending on your needs, you might also write a reverse function, i.e. one that converts from global coordinates to local.

EDIT:
If you want to apply the same transformation to multiple objects (e.g. if you have many turrets attached to the same ship), you might want to use transformation matrices for this.
Attachments
Coordinates.love
(1.83 KiB) Downloaded 262 times
kefka
Prole
Posts: 9
Joined: Thu Feb 17, 2011 3:34 am

Re: Offset turret help

Post by kefka »

SPlice wrote:does anyone know how I could create a function or something out of this so that it could be easily applied to many objects?
Yeah, look into Scene Graphs. That is the canonical solution to the problem of having groups of objects that need to move and rotate with each other, while giving you the ability to ask objects their local-to-world coordinates, or world-to-local coordinates.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Majestic-12 [Bot], slime and 3 guests