[RESOLVED] Setting Rotation Axis

Hi guys! I’ve never been able to figure out how to set the axis of rotation for the rotation matrix function. I don’t know why it wants a z which is probley part of the problem. When I give it the x and y for the axis and a z = 0 it doesnt seem to rotate at all. I’ve tried it with and without a z. I need to do this for an important optimization so any help would be appreciated, thanks!

why not show us the code you used?

try a forum search. The 2nd link goes to http://codea.io/talk/discussion/1801/rotate-an-object-in-3d/p1 where you’ll find some discussion and examples.

@Ignatz

    pushMatrix()

    for i = 1, #self.lineTable do

        self.lineTable[i]:draw(vec2(self.ball.x, self.ball.y))

    end
    rotate(self.ball.angle,self.ball.x,self.ball.y, 0)
    popMatrix()

    popStyle()

@Jmv38 I did look around the fourm search but I thought that you were trying to rotate a 3D object. Does the z rotation apply to non-3D objects? If so how? I only want an x,y rotation.

@Goatboy76 - the x,y,z stuff is only for 3D

If you are in 2D, then you simply write rotate(a) where a is the angle in degrees (positive to rotate left, negative to rotate right)

@Ignatz So there isnt any way to change the axis for a 2D rotation?

@Goatboy76 - there is only one way to rotate on a flat surface

what did you have in mind?

By axis do you by chance mean pivot point?

As Ignatz said in 2D there’s only one way to rotate, unless you a) want to rotate relative to an axis or b) are dealing with a 3d object.

@Ignatz I was wanting to set the pivot point for the rotation instead of rotating around the origin on the 2D plane.

@Serapth Yes, pivot point. Im not sure what you mean by rotate relative to an axis.

Ok, if you are simply looking at rotating around a pivot point, that’s easy.

Just take the item you want to rotate, push the matrix, translate it, apply the rotation and pop the matrix.

For example, the following code is what I use to rotate all the objects in the table about their own centre.

for i,v in pairs(yourTableHere) do
  pushMatrix()
  translate(v.x,v.y)
  rotate(v.rotation)
  sprite(v.img,0,0)
  popMatrix()
end

In this case V is a simple class containing x,y,rotation and an Image.

That code will cause the object to rotate v.rotation degrees ( up to 360 ) around it’s own center. If you want to rotate about a different point, that’s reflected in the values you pass to translate.

@GoatBoy76 Here’s an example of a rotation point and being able to change it with parameter sliders.


supportedOrientations(LANDSCAPE_ANY)

function setup()
    parameter.integer("Xoffset",-129,129,0)
    parameter.integer("Yoffset",-150,150,0)
    angle=0
end

function draw()
    background(40, 40, 50)
    pushMatrix()
    translate(WIDTH/2,HEIGHT/2) -- rotation points
    rotate(angle)   -- angle of rotation
    sprite("Cargo Bot:Toolbox",Xoffset,Yoffset)
    fill(255, 0, 0, 255)    
    ellipse(0,0,10) -- center of rotation
    angle=angle+1   -- increase angle of rotation
    popMatrix()
end

@dave1707 @Serapth Im trying to not use translate becuase its dragging down the framerate.

@Goatboy What are you trying to rotate, lines, sprites, etc and how many are there. Can you say what you’re trying to do. Maybe once I know I can try different things if you don’t want to use translate.

@dave1707 There’s 8 lines (a class) in self.lineTable. The lines need to follow the moving x,y position of a ball and rotate with its angle.

@Goatboy Is this something like what your talking about. If not give me more details. I tried rotate using the x,y,z parameters, but I don’t think that will work for 2D. So I’m back to translate and rotate.

EDIT: Removed unneeded code from my original.


--# Main

function setup()
    parameter.watch("1/DeltaTime")   
    bx=WIDTH/2
    by=HEIGHT-100
    ba=0
end
function draw()
    background(40, 40, 50)
    fill(255)
    stroke(255)
    strokeWidth(2)
    pushMatrix()
    translate(bx,by)
    rotate(ba)
    ellipse(0,0,30)
    for z=1,4 do
        rotate(45)
        line(-50,0,50,0)
    end
    popMatrix()
    ba=ba+1
    by=by-1
end

Here’s the above code using a tween for movement.


--# Main

function setup()
    parameter.watch("1/DeltaTime")   
    ba=0   
    ball={x=100,y=100}
    b1={x=100,y=100}
    b2={x=WIDTH/2,y=HEIGHT-100}
    b3={x=400,y=100}
    b4={x=100,y=600}
    tween.path(10,ball,{b1,b2,b3,b4},{loop=tween.loop.pingpong}) 
end

function draw()
    background(40, 40, 50)
    fill(255)
    stroke(255)
    strokeWidth(2)
    pushMatrix()
    translate(ball.x,ball.y)
    rotate(ba)
    ellipse(0,0,30)
    for z=1,4 do
        rotate(45)
        line(-50,0,50,0)
    end
    popMatrix()
    ba=ba+1
end

@dave1707 That is what I’m trying to do, just with out using translate. Its possible if you can change the pivot point because I can adjust the the lines are drawn at by telling them the pos of the ball. I don’t think it’s possible, oh well.

@Goatboy76 - unless you’re drawing dozens of objects, translating shouldn’t have a big effect at all.

@Ignatz I’m probley jumping at shadows. I’ll move on for now and come back to it later. Thanks for the assistance.