A probably nooby question about rotating a sprite

Hello, I have been an unexperienced self learner of programming and I am stuck trying to rotate a sprite (or anything) on the same spot of the screen. I tried learning about the rotate command and it seems to be rotating the coordinate the sprite is drawn instead and it ends up showing my sprite moving while rotating.

If anyone have the time and understand my confusion, please kindly post up how I can do that. Thank you very much >.<

The rotate() command rotates the entire screen around the origin (0, 0). Or whatever the origin is set to using translate() and scale(). So to rotate a sprite about its center, you can do the following:

function draw()
    background(0)
    
    -- The position of your sprite
    x = 300
    y = 200
    
    translate( x , y )
    rotate( angle ) -- Rotate by some angle
    
    sprite( "Your Sprite:Here", 0, 0 ) -- Sprite is drawn at 0, 0
end

To be more versatile, you can wrap it up in a class like this:


Sprite = class()

function Sprite:init( name )
    self.name = name
    self.position = vec2(0,0)
    self.angle = 0
end

function Sprite:draw()
    pushMatrix()
    
    translate( self.position.x, self.position.y )
    rotate( self.angle )
    
    sprite( self.name, 0, 0 )
    
    popMatrix()
end

Then you can use it like this:


function setup()
    mySprite = Sprite( "Planet Cute:Character Boy" )
    mySprite.position = vec2( 300, 200 )
    mySprite.angle = 45
end

function draw()
    background(0)

    mySprite:draw()
end

Thank you so much Simeon :slight_smile:
It helps me a lot being able to rotate stuff now.

Why not build the rotation into the Codeas sprite drawer? wouldn’t that be brilliant?. This method is way too stupid.

@looo78 - you have to translate before you rotate, would you include that as well?

It needs to be the way it is … And there’s no need to be rude about it…

I’m sorry, didn’t mean to be rude. please read this thread: http://twolivesleft.com/Codea/Talk/discussion/3673/codeas-sprite-drawer-should-have-rotation#Item_10

The only thing ‘stupid’ here would be not understanding the efficiency and capability of the already built in functions to transform the sprite.

I think you all misunderstand his usage of “stupid” - its a comment on the capabilities of the api function. Think “its limited”…

I don’t get this game this is really stupid and complicated because this code isn’t even finished

@lruizlopez137 It’s not a game. Read it all the way through. Please stop bumping old discussions.

And don’t call things stupid, it’s offensive and immature.

@SkyTheCoder Actually, @kruizlopez137 wasn’t the one to bump it this time!

@Prynok Maybe not the first, but the first was asking a genuine question, not saying this “game” is stupid.

@Iruizlopez137 .Experts in this forum to learn and share codes implement new features every day, if you want a game that you like I invite you to search applestore made codea games (two lifes lefts)