#### Howdy, Stranger!

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

# Continuous rotation of a vec2 polygon?

edited October 2014 Posts: 17

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

Tagged:

• Posts: 103

Are you making this as a copy of Impossible Colours?

• Posts: 17

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

• Mod
Posts: 9,730
``````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

``````
• Posts: 17

@dave1707 thank you very much.

• Mod
Posts: 1,277

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

• Mod
Posts: 9,730

@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.

• Mod
Posts: 5,396

@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)

• Mod
Posts: 9,730

@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.

• Mod
Posts: 5,396

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

• Mod
Posts: 9,730

@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.

• Mod
Posts: 5,396

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

• Mod
Posts: 9,730

@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

``````
• edited October 2014 Posts: 342

@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.

• Mod
Posts: 9,730

@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.

• edited October 2014 Posts: 342

@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)

• Mod
edited October 2014 Posts: 9,730

@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.

• edited October 2014 Posts: 342

@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

• Mod
Posts: 5,396

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.

• Mod
edited October 2014 Posts: 9,730

@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.

• Mod
Posts: 5,396

@Monkeyman32123 - I use a trick based on the fact Codea compiles from left to right, so if you have two copies of a function, it will overwrite the first and use the second.

So I create a Test tab on the right, and put a setup and draw function in there. I can then use this to test any parts of my project by just running parts of it. When I'm done testing, I just comment out the test tab. But I use this constantly when I make changes and add new functions.

• Posts: 1,976

@dave1707 I thought @Doge's suggestions was for folders in the project browser, not the code editor?

• Mod
Posts: 9,730

@SkyTheCoder That's correct, folders to keep similar projects together in one place. But you don't need folders to keep code for the same project together. I was just pointing out that using tabs will do the same thing, keeping chunks of code for the same project together. So he doesn't have to wait for actual folders to come out before he starts his large program.