how to rotate an object with a rotating platform for orthographic view?

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
smunnelsnakzruule
Prole
Posts: 8
Joined: Thu Oct 13, 2016 12:36 pm

how to rotate an object with a rotating platform for orthographic view?

Post by smunnelsnakzruule »

to explain what effect I am trying to achieve please watch these video:







I basically want objects to become relative to certain objects movement and rotation when they overlap them.

I read up on it and one example is to:

Code: Select all

--fake BAD code

--set all objects origins to center (x, y)

obj.radius = distance(obj.X, obj.Y, overlapped_platform.X, overlapped_platform.Y) * 1.0

obj.AngleToFaceObject = angleToFacePosition(obj.X, obj.Y, overlapped_platform.X, overlapped_platform.Y) * 1.0 - 180 + overlapped_platform.Angle

--set x and y position
obj.X = overlapped_platform.X + (cosine(obj.AngleToFaceObject - overlapped_platform.Angle) * obj.radius )
obj.y = overlapped_platform.Y + (sine(obj.AngleToFaceObject - overlapped_platform.Angle) * obj.radius )
well to be honest, I dont know what the hell im doing here.

This problem seems to work only sometimes. I'm trying to figure out what im doing wrong, and then I get confused.

I'm pretty sure the issue is here "obj.AngleToFaceObject = angleToFacePosition(obj.X, obj.Y, overlapped_platform.X, overlapped_platform.Y) * 1.0 - 180 + overlapped_platform.Angle"

I think.
dvdfu
Prole
Posts: 7
Joined: Sun Sep 27, 2015 6:22 pm

Re: how to rotate an object with a rotating platform for orthographic view?

Post by dvdfu »

I'm not sure what your exact question is, but I think you're asking: "how do I fix the distance of an object that should be standing just on top of some angled surface?"

For this solution I'm assuming (x, y) is the object's center, and the platform has some (angle) value.

Code: Select all

local dx, dy = obj.x - plat.x, obj.y - plat.y   -- difference between obj and plat coordinates
local radius = math.sqrt(dx * dx, dy * dy)      -- distance of vector between obj and plat
local angleTo = math.atan2(dy, dx) + plat.angle -- angle of vector between obj and plat

obj.x = plat.x + radius * math.cos(angleTo)
obj.y = plat.y + radius * math.sin(angleTo)
It's almost identical to your solution, but uses radians throughout.

You should definitely look into using a 2D vector library for your object coordinates, since it will make calculations a lot more convenient.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: how to rotate an object with a rotating platform for orthographic view?

Post by Positive07 »

Well LÖVE have transformation functions, you usually do something like:

Code: Select all

--x and y are to the center of the object
local ship = {x = 100, y = 100, w = 200, h = 140, angle = math.pi * .5}
local passenger = {x = 40, y = 30, w = 10, h = 10, angle = math.pi * .2} --Relative to the ship

love.draw = function ()
   love.graphics.push() --Inside here you draw everything that belongs to the ship
      love.graphics.translate(ship.x, ship.y)
      love.graphics.rotate(ship.angle)
      love.graphics.translate(-ship.w/2, -ship.h/2)

      love.graphics.draw(ship.image, 0. 0)

      love.graphics.draw(passenger.image, x, y, angle)

   love.graphics.pop()
end
To get linear distance from an object that is not inside the ship you need trigonometry which is basically what you were talking about, but that is not needed to simply draw
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
smunnelsnakzruule
Prole
Posts: 8
Joined: Thu Oct 13, 2016 12:36 pm

Re: how to rotate an object with a rotating platform for orthographic view?

Post by smunnelsnakzruule »

Thanks to you both I will try it out and research vectoring 2d engines.

im not sure about what this does:

Code: Select all

love.graphics.translate(-ship.w/2, -ship.h/2)
why do we subtract the translate by half which I assume is from the origin center.
This seems a little limited since I would end up drawing each ship from their own coordinates and individual drawing objects. I was hoping to have one general list of objects to consider relative drawing based on their own internal lists. But I guess I could apply it individually from the objects and not the ships.

Well thank you both I'm sure this has increased my understanding of it greatly.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: how to rotate an object with a rotating platform for orthographic view?

Post by Positive07 »

Well as I said in the comments the x and y coordinates are to the center of the object, so once we translate to that center, we rotate, then we have to draw the object, but love.draw thinks the origin is in the top left corner so first we move the origin to half the width and half the height, then the origin is right so we can draw the ship and every children element it may have, if one of those elements has got a child then you do the same, without poping the current state.

I would implement this with a hierarchy, parent draws all of it childrens, if one of those child has childrens it draws them and so on...

You would do this with a for loop in an array of children... Not so hard, but if you have to implement this and you already have some sort of hierarchy then it may get complicated since you have to get to that style of order
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests