Page 1 of 2

[Solved] Offset turret help

Posted: Mon Aug 08, 2011 2:30 am
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 )

Re: Offset turret help

Posted: Mon Aug 08, 2011 7:05 am
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.

Re: Offset turret help

Posted: Mon Aug 08, 2011 8:41 am
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...

Re: Offset turret help

Posted: Mon Aug 08, 2011 9:20 am
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.

Re: Offset turret help

Posted: Mon Aug 08, 2011 9:42 am
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 =/

Re: Offset turret help

Posted: Mon Aug 08, 2011 1:37 pm
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 ><

Re: Offset turret help

Posted: Mon Aug 08, 2011 2:29 pm
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.

Re: Offset turret help

Posted: Sat Aug 27, 2011 10:25 pm
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?

Re: [Solved] Offset turret help

Posted: Sun Aug 28, 2011 1:35 pm
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.

Re: Offset turret help

Posted: Thu Sep 01, 2011 5:05 pm
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.