Continuous rotation of a vec2 polygon?

Is there a way to make a vec2 polygon continuously rotate on a fixed point? (It is classified as a variable)

Are you making this as a copy of Impossible Colours?

No, I don’t know what that is. I am making a 2D version of elite 1984.


displayMode(FULLSCREEN)

function setup()
    r=0
    vx=120
    vy=100
    pol={vec2(0,0),vec2(0,100),vec2(200,200),vec2(300,100),vec2(200,0)}
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    pushMatrix()
    translate(WIDTH/2,HEIGHT/2)
    r=r+1
    rotate(r)
    j=#pol
    for z=1,#pol do
        line(pol[z].x-vx,pol[z].y-vy,pol[j].x-vx,pol[j].y-vy)
        j=z
    end
    popMatrix()
end

@dave1707 thank you very much.

I generally store the poly in coords relative to its center, translate to the center, rotate, and draw. otherwise as dave shows.

@RonJeffries Instead of trying to figure out the vec2 coordinates around a center point, I just drew the polygon at random. Once it rotates, I adjust the rotation point so it looks the best. That’s what the vx and vy values are. I just vary those values to get the best rotation effect.

@dave1707 - wrt vx and vy, now there was a good reason for a code comment, lol.

Actually, I’m sure [Clear Code] Bob would say I’m wrong, and that it’s your variable names that should be clearer, like polCentre=vec2(120,100)

@Ignatz I’m very lazy when it comes to coding. My variable names tend to be small and I don’t waste keystrokes on comments. I figure if anyone really cares, they’ll ask me to explain it more. I like your variable polCentre. I guess I could have used pcx and pcy.

I’m not sure I would have guessed what pcx was, either! :))

@Ignatz I guess what it comes down to is the variable names really only matter to the coder. When an app is created, the person running the app doesn’t see any of the code and couldn’t care less what the variable names are. I know I should use better names and add more comments because it will help the new coders understand the code more. I code while watching TV, so I’m not giving that much attention to the code and don’t waste a lot of time thinking about variable names. I guess you can use me as an example of what a programmer shouldn’t be like.

Don’t worry, I’m not having a go at you ,my code is no better. :">

@MichaelWeedmark Here’s a version of the code to handle multiple polygons.


displayMode(FULLSCREEN)

function setup()
    tab={}  -- table for polygons
    table.insert(tab,poly(100,200,400,150,100,2,
        vec2(0,0),vec2(0,100),vec2(200,200),vec2(300,100),vec2(200,0)))
    table.insert(tab,poly(0,600,300,80,40,1,
        vec2(0,0),vec2(0,50),vec2(150,100),vec2(200,80),vec2(70,0)))
    table.insert(tab,poly(0,600,600,60,90,-3,
        vec2(0,0),vec2(0,75),vec2(80,200),vec2(120,50),vec2(30,0)))
end

function draw()
    background(40, 40, 50)
    for a,b in pairs(tab) do
        b:draw()
    end
end

poly=class()

function poly:init(r,x,y,cx,cy,s,...)
    self.pol={} -- table for points
    self.x=x    -- x pos
    self.y=y    -- y pos
    self.r=r    -- rotate angle
    self.cx=cx  -- center x
    self.cy=cy  -- center y
    self.speed=s    -- rotation speed
    for z=1,arg.n do
        table.insert(self.pol,arg[z])   -- create table of vec2 points
    end    
end

function poly:draw()
    stroke(255)
    strokeWidth(2)
    pushMatrix()
    translate(self.x,self.y)
    self.r=self.r+self.speed
    rotate(self.r)
    j=#self.pol
    for z=1,#self.pol do
        line(self.pol[z].x-self.cx,self.pol[z].y-self.cy,
                self.pol[j].x-self.cx,self.pol[j].y-self.cy)
        j=z
    end
    popMatrix()   
end

@Dave1707 I’m the same as you, I have semi-random variable names that make little to no sense to others. Heck, in my 100 line challenge entry I named the plane’s location “boat” because I wanted to use a “p” variable somewhere else. I’m a very sloppy and unprofessional coder, but I suppose it is to be expected of one who has only taught himself (and learned through these forums), and one who only does it for leisure, Though I do plan to go to college for it (fingers crossed), and hopefully then I can be broken of this awful habit which afflicts us both.

@Monkeyman32123 As long as you don’t write large programs that you’ll need to make changes to at a later time, the variable names aren’t that important. Once you get into large programs as a professional, then meaningful variable names and comments will become more important. Especially when someone else needs to change your code or you have to change someone else’s code and you need to do it fast. That’s when it become very important.

@Dave1707 true. I’ve tried to write large programs and I think my variable names (as well as my inability to figure out level editor making) are all that are stopping me, but I do plan to finish a larger program by the end of next March (or at least get it to an alpha level). It’s a pet project of mine I’ve had for years. I have the enemies, story line, protagonists, quests, side quests, dialogue (correction: monologue), all of it planned out. All that’s standing in the way is my darn programming ability (and 3D, 3D makes my head explode)

@Monkeyman32123 Just start small. Write small chunks of code that you need. Test it. Write another chunk. Test it. Just keep doing that and you’ll have your large program written.

@dave1707 are you Jesus? Because all of your advice is as if from the heavens (I’m not hitting on you though, just to be clear). How badly I want doge’s folder idea implemented so that those chunks can be better organized, though

Yes, I agree with dave, it’s ok for small programs.

But if you’re planning a career in programming, you should definitely start learning to write professional quality code, even for small programs. Otherwise you get into bad habits that will be hard to shake.

As dave says, it’s very important in business, that code is readable by other people.

@Monkeyman32123 You already have folders, they’re called tabs. Start you program with Main. Create a tab for one of your chunks. Write the code and test it. Create another tab for the next chunk. Just keep doing that. If you look at the Cargo-Bot example, you’ll see plenty of tabs. There’s no folders to hold all the chunks, just tabs.