Physics - rotate Body around point
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Physics - rotate Body around point
Hello! Can I rotate Body around some point, it's center for example? I found Body:setAngle but it isn't what I exactly want, because it rotates Body around (0; 0).
Last edited by egorcod on Sun Apr 22, 2018 2:12 pm, edited 1 time in total.
Re: Physics - rotate Body around point
There is no Body:setRotation (your link leads to a blank page). if you mean Body:setAngularVelocity, that rotates the body around its centre of mass.
I suspect that your problem is with where you draw your sprites vs. where the physical shape actually is. You can change the location of the centre of mass, but if I'm right, you would obtain a very weird result.
You can try this library to draw the physics world schematically: viewtopic.php?t=77140 but note most colours are all dark, so you need a light background (e.g. white).
It's also for an older version of LÖVE, so you also need to edit it to change love.graphics.point to love.graphics.points (there are 4 calls in total), otherwise it will give an error.
With that, you can see whether you're drawing your shapes where your physical shapes actually are.
I suspect that your problem is with where you draw your sprites vs. where the physical shape actually is. You can change the location of the centre of mass, but if I'm right, you would obtain a very weird result.
You can try this library to draw the physics world schematically: viewtopic.php?t=77140 but note most colours are all dark, so you need a light background (e.g. white).
It's also for an older version of LÖVE, so you also need to edit it to change love.graphics.point to love.graphics.points (there are 4 calls in total), otherwise it will give an error.
With that, you can see whether you're drawing your shapes where your physical shapes actually are.
Re: Physics - rotate Body around point
Sorry, I'm wrong with link. The right link is Body:setAngle. No, I don't need setAngularVelocity, because I need to rotate Body immediately. I just need to immediately rotate Body around some point.
Re: Physics - rotate Body around point
Ahh, okay, sorry for the misunderstanding. Well, there are two possibilities: one is to create the shape so that it's already centred in the body, for example, if it's a 6x4 rectangle, the shape could be created as: love.physics.newRectangleShape(-3, -2, 6, 4), or in general, if the rectangle is wxh: love.physics.newRectangleShape(-w/2, -h/2, w, h)
The other possibility is to displace the body after the rotation so that it's in the correct location. For example, if you want to rotate it around its centre of mass:
The other possibility is to displace the body after the rotation so that it's in the correct location. For example, if you want to rotate it around its centre of mass:
Code: Select all
local beforeX, beforeY = body:getWorldCenter()
body:setAngle(newAngle)
local afterX, afterY = body:getWorldCenter()
body:setPosition(body:getX() - afterX + beforeX, body:getY() - afterY + beforeY)
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 11 guests