Howdy, Stranger!

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

Quick Physics Example

in Examples Posts: 192

Hello all,
Fooling around on a long car ride today, I came up with this:

--# addRect addRect = class() function addRect:init(x,y,endX,endY) -- you can accept and set parameters here local w,h = endX-x,endY-y self.body = physics.body(POLYGON,vec2(w/2,h/2),vec2(w/2,-h/2),vec2(-w/2,-h/2),vec2(-w/2,h/2)) self.body.x = (x + endX)/2 self.body.y = (y + endY)/2 self.m = mesh() self.m.vertices = triangulate(self.body.points) self.m:setColors(255,255,255) self.body.fixedRotation = false self.body.friction = 0.2 self.body.restitution = 0.0 self.body.angle = 0 end function addRect:draw() -- Codea does not automatically call this method pushMatrix() translate(self.body.x,self.body.y) rotate(self.body.angle) self.m:draw() popMatrix() end function addRect:touched(touch) -- Codea does not automatically call this method end --# Main -- Creations testing -- Use this function to perform your initial setup function setup() print("Hello World!") objects = {} local a = generateWorld() m = mesh() m.vertices = triangulate(a) m:setColors(0,255,0) ground = physics.body(CHAIN,loop,unpack(a)) touches = {} tId = nil initPos = nil noSmooth() frame = 1 end -- This function gets called once every frame function draw() -- This sets a dark background color background(40, 40, 50) -- This sets the line thickness noStroke() fill(255) -- Do your drawing here m:draw() if tId then if touches[tId].state==ENDED then objects[#objects+1] = addRect(initPos.x,initPos.y,touches[tId].x,touches[tId].y) tId = nil initPos = nil else rect(initPos.x,initPos.y,touches[tId].x-initPos.x,touches[tId].y-initPos.y) end end for i,v in pairs(objects) do v:draw() end text(math.floor(1/DeltaTime),WIDTH-10,HEIGHT-10) frame = frame + 1 end function generateWorld() local verts = {} local control = math.random(0,100) for x=0,WIDTH, 5 do verts[#verts+1] = vec2(x,noise(x/100,control)*100+100) end verts[#verts+1] = vec2(WIDTH,verts[#verts].y) verts[#verts+1] = vec2(WIDTH,0) verts[#verts+1] = vec2(0,0) return verts end function touched(t) if not initPos and t.state==BEGAN then initPos = vec2(t.x,t.y) tId = t.id end touches[t.id] = t end

Just a quick little example of using physics bodies, nothing too complicated. Sorry no documentation-I really should get around to that.
Psst-set friction to 100 and fixedRotation to true to have some fun Tagged:

• Posts: 9,806

@TheSolderKing Nice job. It doesn't take much code to make interesting physics programs.

• Posts: 192

Thanks @dave1707! No it doesn't indeed...Im thinking about making a game where you create vehicles and fight them against each other for the game jam--this is just a start of that. It'll be multiplayer as well so that'll be fun.

• Posts: 5,759

@TheSolderKing that's pretty fun to play with

• Posts: 192

Thanks @Simeon! I had a great deal of fun making it and playing it. I was trying ot work on textures but oddly I cannot get them to work right now, but Ill keep at it and if I succeed Ill post the code above.

• Posts: 2,020

Looking forward to your game jam entry!

• Posts: 16

This is so cool!