Page 1 of 2

Is it possible to offset rotation axis in Physics?

Posted: Thu Mar 27, 2014 1:56 pm
by Cucurbitacée
Hi,

The title says everything, I'm toying with love.physics at the moment and while I can do everything I want there is one thing that I'm missing.

I have a spaceship at the centre of the screen and I get the angle from the mouse pointer:

Code: Select all

function love.load()
	world = love.physics.newWorld(0, 0, true)
	...
	ship.body = love.physics.newBody(world, ship.x, ship.y, "dynamic")
	ship.shape = love.physics.newRectangleShape(8, 8)
	ship.fixture = love.physics.newFixture(ship.body, ship.shape)
	...
end
function love.update(dt)
	world:update(dt)
	...
	ship.angle = math.atan2((pointer.y - ship.y), (pointer.x - ship.x))
	ship.body:setAngle(ship.angle)
	...
end
function love.draw()
	...
	love.graphics.polygon("line", ship.body:getWorldPoints(ship.shape:getPoints()))
	...
end
It works, but the rotation axis is the top left corner of the shape, and I would like it to be in the middle of the shape. Is it possible? I couldn't find any information about this in the wiki. :huh:

Thanks. :3

Re: Is it possible to offset rotation axis in Physics?

Posted: Tue May 20, 2014 1:32 pm
by Cucurbitacée
Hi again,

It's been a long time but I still couldn't find any answer, so I'll try to up my message once, in case someone with the answer didn't saw it at the time. :P

Re: Is it possible to offset rotation axis in Physics?

Posted: Tue May 20, 2014 2:16 pm
by DaedalusYoung
I can't tell much about this, I have no experience with physics, and only very little with polygons, but it looks like you need to offset the x and y values in your ship's shape. I don't know how your ship is constructed, so I can't tell you if that's as easy as just adding and subtracting some values, or that you need to mess with math.cos and math.sin to properly rotate the verts.

Re: Is it possible to offset rotation axis in Physics?

Posted: Wed May 21, 2014 9:05 am
by Cucurbitacée
Hi,

Thanks for your answer. :) Unfortunately I tried to offset the shape. Visually it works, but the body is still rotating around the same spot and the collision don't match the visual. :oops:

To illustrate that:
Image
The ship is following the mouse pointer, the rotation point of the image of the ship is at the centre. But the white square which is the actual "hit box" is rotating around the top left corner.

My guess is that there should be a way to to change that in the fixture, but I can't find anything in the Wiki. :?

Re: Is it possible to offset rotation axis in Physics?

Posted: Wed May 21, 2014 1:49 pm
by ArchAngel075
ive dealt with this before in the past and i think its the following :

you would need to draw the hit box from its center, at the moment you are drawing it from the top left,

OR

you would need to draw the image from its true center as the hitbox it drawn correctly.

--

As a question to help, are you offsetting the image when drawing it?

Re: Is it possible to offset rotation axis in Physics?

Posted: Thu May 22, 2014 12:39 pm
by Cucurbitacée
ArchAngel075 wrote:ive dealt with this before in the past and i think its the following :

you would need to draw the hit box from its center, at the moment you are drawing it from the top left,

OR

you would need to draw the image from its true center as the hitbox it drawn correctly.

--

As a question to help, are you offsetting the image when drawing it?
Thanks for the answer. First, yes, I'm offsetting the image when drawing it. If I draw it without offset the hitbox is perfectly aligned, but the rotation doesn't feel natural. :(
About drawing the hitbox, I use the function ship.body:getWorldPoints(ship.shape:getPoints()), if I offset the drawing of the hitbox from these points I have the drawing aligned with the image of the ship, but the "real" hitbox" is still off.

Sorry if I'm missing something, I feel that it should something easy to overcome. :?

Re: Is it possible to offset rotation axis in Physics?

Posted: Thu May 22, 2014 1:46 pm
by ArchAngel075
The fact that offsetting the drawing of the box corrects the visual issue shows the initial problem :

(revert the drawing of the hit box to not be offset if it is still offset)

Then you will need to offset the created hitbox shape-->

Try using this:

ship.shape = love.physics.newPolygonShape( -4,-4, 4,-4, 4,4, -4,4 )

Instead of:

ship.shape = love.physics.newRectangleShape(8, 8)


It should offset the rectangle correctly and retain its size and maintain any previous code related to the shape?

Re: Is it possible to offset rotation axis in Physics?

Posted: Thu May 22, 2014 2:06 pm
by Cucurbitacée
ArchAngel075 wrote:The fact that offsetting the drawing of the box corrects the visual issue shows the initial problem :

(revert the drawing of the hit box to not be offset if it is still offset)

Then you will need to offset the created hitbox shape-->

Try using this:

ship.shape = love.physics.newPolygonShape( -4,-4, 4,-4, 4,4, -4,4 )

Instead of:

ship.shape = love.physics.newRectangleShape(8, 8)


It should offset the rectangle correctly and retain its size and maintain any previous code related to the shape?
Thanks for the answer, reading it I thought it would totally do the trick. :awesome: But I just implemented it, and it now rotates around the opposite corner... :oops:

This really puzzles me. :?

Re: Is it possible to offset rotation axis in Physics?

Posted: Thu May 22, 2014 7:33 pm
by ArchAngel075
could perhaps provide the .love so that I and others following this can trouble shoot ourselves and provide a solution?

Re: Is it possible to offset rotation axis in Physics?

Posted: Fri May 23, 2014 11:24 am
by Cucurbitacée
ArchAngel075 wrote:could perhaps provide the .love so that I and others following this can trouble shoot ourselves and provide a solution?
Sure, I've tried to make it light, as it a bit of a mess at the moment. Please note that only the mouse controls are more or less working at the moment.

Thanks for having a look. ;)