Fixed Rotation on Falling Physics Objects

I’m fairly new to Corona, and I apologize if this has been answered somewhere before, but I’m having some trouble. I’m creating a platformer but I’m having an issue with the physics of the player. I want the sprite to not be able to rotate, even if it’s being affected by a collision or gravity. I tried adding in hero.isFixedRotation = true but it didn’t seem to change anything. Is there some other trick to use because of gravity?

Thanks [import]uid: 212518 topic_id: 35679 reply_id: 335679[/import]

isFixedRotation=true should do it. Can you post a working code snippet? [import]uid: 8271 topic_id: 35679 reply_id: 141876[/import]

Hi @afflicting.the.soul,

@horacebury is correct… “.isFixedRotation” should work. If we see your code, we might determine something else going wrong.

That being said, I have noticed that in some cases, an object with fixed rotation can still be “jarred” by a tiny amount, when involved in a collision with a “heavy” object that hits an outside region of the object that would cause it to rotate. The effect is hardly noticeable, but it does happen occasionally.

If you’re seeing a rotational effect more than that, I’m guessing something is quirky in your code.

Regards,
Brent [import]uid: 200026 topic_id: 35679 reply_id: 141877[/import]

– Load sprites
local heroSheet = sprite.newSpriteSheet( “spriteplayer.png”, 104, 151 )
local heroSet = sprite.newSpriteSet( heroSheet, 1, 12 )
sprite.add( heroSet, “hero”, 1, 6, 400, 0 )
sprite.add( heroSet, “hero2”, 7, 6, 400, 0 )
local hero = sprite.newSprite( heroSet )
hero:setReferencePoint(display.BottomLeftReferencePoint)
heroShape = { -26, -30, 26, -30, 26, 65, -26, 65 }
hero.x, hero.y = 47, _H -75
hero.xScale = .5 hero.yScale = .5
hero.isFixedRotation = true
physics.addBody( hero, “dynamic”, { friction=.5, bounce=0, shape=heroShape } )
hero:prepare( “hero” )
playergroup:insert( hero )

That’s my code for the sprite. I can post all my code if you think the error might be somewhere else. In it’s current form if the sprite runs into another static object or falls with gravity it rotates. I appreciate your help guys. [import]uid: 212518 topic_id: 35679 reply_id: 141881[/import]

Thanks! Try putting all modifications to a physics body (i.e. fixed rotation) after the declaration of the physics body. That should solve your problem.

Also, I see you’re using the deprecated “sprite” library/methods. I suggest that you change to the current methods, since the old library will not receive any support or updates going forward. Here’s a quick tutorial:
http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

Regards,
Brent [import]uid: 200026 topic_id: 35679 reply_id: 141882[/import]

Thanks!! Worked like a charm. I knew it had to be something simple I was missing. I appreciate the heads up on the new sprites as well! [import]uid: 212518 topic_id: 35679 reply_id: 141883[/import]

isFixedRotation=true should do it. Can you post a working code snippet? [import]uid: 8271 topic_id: 35679 reply_id: 141876[/import]

Hi @afflicting.the.soul,

@horacebury is correct… “.isFixedRotation” should work. If we see your code, we might determine something else going wrong.

That being said, I have noticed that in some cases, an object with fixed rotation can still be “jarred” by a tiny amount, when involved in a collision with a “heavy” object that hits an outside region of the object that would cause it to rotate. The effect is hardly noticeable, but it does happen occasionally.

If you’re seeing a rotational effect more than that, I’m guessing something is quirky in your code.

Regards,
Brent [import]uid: 200026 topic_id: 35679 reply_id: 141877[/import]

– Load sprites
local heroSheet = sprite.newSpriteSheet( “spriteplayer.png”, 104, 151 )
local heroSet = sprite.newSpriteSet( heroSheet, 1, 12 )
sprite.add( heroSet, “hero”, 1, 6, 400, 0 )
sprite.add( heroSet, “hero2”, 7, 6, 400, 0 )
local hero = sprite.newSprite( heroSet )
hero:setReferencePoint(display.BottomLeftReferencePoint)
heroShape = { -26, -30, 26, -30, 26, 65, -26, 65 }
hero.x, hero.y = 47, _H -75
hero.xScale = .5 hero.yScale = .5
hero.isFixedRotation = true
physics.addBody( hero, “dynamic”, { friction=.5, bounce=0, shape=heroShape } )
hero:prepare( “hero” )
playergroup:insert( hero )

That’s my code for the sprite. I can post all my code if you think the error might be somewhere else. In it’s current form if the sprite runs into another static object or falls with gravity it rotates. I appreciate your help guys. [import]uid: 212518 topic_id: 35679 reply_id: 141881[/import]

Thanks! Try putting all modifications to a physics body (i.e. fixed rotation) after the declaration of the physics body. That should solve your problem.

Also, I see you’re using the deprecated “sprite” library/methods. I suggest that you change to the current methods, since the old library will not receive any support or updates going forward. Here’s a quick tutorial:
http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

Regards,
Brent [import]uid: 200026 topic_id: 35679 reply_id: 141882[/import]

Thanks!! Worked like a charm. I knew it had to be something simple I was missing. I appreciate the heads up on the new sprites as well! [import]uid: 212518 topic_id: 35679 reply_id: 141883[/import]