Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Independent Rotation

edited December 2011 in Code Sharing Posts: 622

This may have been obvious to some but I had to look at the code of a few others to figure out how to do this.

It doesn't really have to be done in functions. Basically whereever (x,y) you want things to rotate around, you translate() that, rotate, and translate() back within a Push/Pop Matrix to isolate it from all other drawing.

The functions below assume you want to rotate the center of the objects. In the case of the larger star, the center of the sprite is not the center of the visible portion.

--rotate

function setup()
    r = 0
end

function draw()
    rect(0,0,1,1)
    background(0, 0, 0)
    fill(255, 0, 0, 255)
    rellipse(123,234,111,222,r)
    rrect(345,567,222,111,r)    
    rectMode(CENTER)
    rect(567,567,101,151)
    sprite("Planet Cute:Star" ,567,567)
    rsprite("Small World:Mote Happy",123,567,nil,nil,r)
    r = r + 1
end

function rsprite(name,x,y,w,h,r)
    pushMatrix()
    spriteMode(CENTER)
    translate(x,y)
    rotate(r)
    translate(-x,-y)
    if w == nil then sprite(name,x,y)
    elseif h == nil then sprite(name,x,y,w)
    else sprite(name,x,y,w,h) end
    popMatrix()
end

function rellipse(x,y,w,h,r)
    pushMatrix()
    ellipseMode(CENTER)
    translate(x,y)
    rotate(r)
    translate(-x,-y)
    ellipse(x,y,w,h)
    popMatrix()
end

function rrect(x,y,w,h,r)
    pushMatrix()
    rectMode(CENTER)
    translate(x,y)
    rotate(r)
    translate(-x,-y)
    rect(x,y,w,h)
    popMatrix()
end
Tagged:

Comments

  • BortelsBortels Mod
    Posts: 1,557

    This is the basic concept behind a scenegraph - I'm actually building one myself for a project. I wonder if it might not be worth abstracting out to a class. It is not an obvious concept!

  • Posts: 384

    Thanks @Ipda41001, this was helpful!

  • Really helpful, thank you! Exactly what i was Going to ask for in a few seconds :)

Sign In or Register to comment.