Is it possible to offset rotation axis in Physics?

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.
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

Is it possible to offset rotation axis in Physics?

Post 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
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

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

Post 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
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

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

Post 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.
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

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

Post 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. :?
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

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

Post 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?
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

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

Post 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. :?
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

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

Post 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?
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

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

Post 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. :?
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

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

Post by ArchAngel075 »

could perhaps provide the .love so that I and others following this can trouble shoot ourselves and provide a solution?
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

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

Post 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. ;)
Attachments
Sinistar.love
Sinistar inspired game - very early prototype.
(426.94 KiB) Downloaded 148 times
Post Reply

Who is online

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