Self-uprighting physics? (Solved)

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
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Self-uprighting physics? (Solved)

Post by Taehl »

Currently, I use this bit of code to make the physics-based player character try to stand upright:

Code: Select all

local angle, avelocity, damp = player.b:getAngle(), player.b:getAngularVelocity(), 1.7
player.b:setAngularVelocity( avelocity + (-angle * damp) )
However, that can be buggy: If the player tumbles a full rotation or more, the self-uprighting will try to flip it all the way back over again, rather than just upright. How can I fix this?
Last edited by Taehl on Sat Jan 16, 2010 12:04 pm, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Self-uprighting physics?

Post by TechnoCat »

If it rotates to -2 radians, just add 2 radians. If it rotates to 1.5 radians, first subtract a radian before rotating to 0. Try that sort of thing.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Self-uprighting physics?

Post by Taehl »

I think I've hacked out a solution:

Code: Select all

local angle, avelocity, damp = player.b:getAngle(), player.b:getAngularVelocity(), 1.7
local n = (angle + math.pi) % (2 * math.pi) - math.pi
player.b:setAngularVelocity( avelocity + (-n * damp)
Is there anyone here familiar enough with radians to double-check that for me? It appears to work, but I'd like to be sure.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: Self-uprighting physics?

Post by kalle2990 »

If you prefer to use degrees, you can convert from radians to degrees with math.deg and from degrees to radians with math.rad ;)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Self-uprighting physics?

Post by Robin »

Taehl wrote:Is there anyone here familiar enough with radians to double-check that for me? It appears to work, but I'd like to be sure.
It seems right. ;)
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 202 guests