Howdy, Stranger!

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

In this Discussion

Very simple cool looking physics example

edited March 2012 in Examples Posts: 2,820

Hello,
There are many physics examples out there, but I made one just now that is very simple to understand. Just click, drag and let go and you have an awsome flashing ball bouncing around. You can also tilt your iPad to make them move. Their mass is changed according to their size. They have random bouncynesses too. Try it out! Here's the code:


--# Main -- Use this function to perform your initial setup function setup() backingMode(RETAINED) displayMode(STANDARD) ChangeColor:init(.1) --gradient background and creates a fade away for circles bgimg = image(WIDTH,HEIGHT) setContext(bgimg) for i = 1, HEIGHT do ChangeColor:update() strokeWidth(2) stroke(ChangeColor:getColor()) line(0,i,WIDTH,i) end setContext() ball = {} currentBall = 1 canCreate = true physics.gravity(Gravity) wall1 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT)) wall2 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0)) wall3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT)) wall4 = physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(0,HEIGHT)) wall1.type = STATIC wall2.type = STATIC wall3.type = STATIC wall4.type = STATIC wall1.restitution = .8 wall2.restitution = .8 wall3.restitution = .8 wall4.restitution = .8 end -- This function gets called once every frame function draw() tint(255, 255, 255, 180) spriteMode(CENTER) sprite(bgimg,WIDTH/2,HEIGHT/2) stroke(0, 255, 5, 255) strokeWidth(5) physics.gravity(Gravity) currentBall = table.maxn(ball) if CurrentTouch.state == BEGAN and canCreate then table.insert(ball,physics.body(CIRCLE, math.random(5,50))) currentBall = table.maxn(ball) ball[currentBall].x = CurrentTouch.x ball[currentBall].y = CurrentTouch.y ball[currentBall].mass = ball[currentBall].radius / 10 ball[currentBall].restitution = 1 ball[currentBall].gravityScale = math.random(.25,5) canCreate = false elseif CurrentTouch.state == BEGAN and canCreate then table.insert(ball,physics.body(CIRCLE, math.random(5,50))) currentBall = table.maxn(ball) ball[currentBall].x = CurrentTouch.x ball[currentBall].y = CurrentTouch.y ball[currentBall].mass = ball[currentBall].radius / 10 ball[currentBall].restitution = 1 ball[currentBall].gravityScale = math.random(.25,5) canCreate = false end if CurrentTouch.state == MOVING or CurrentTouch.state == BEGAN then ball[currentBall]:applyForce(vec2((CurrentTouch.x-ball[currentBall].x)/.5, (CurrentTouch.y-ball[currentBall].y)/.5)) line(CurrentTouch.x,CurrentTouch.y,ball[currentBall].x,ball[currentBall].y) end if CurrentTouch.state == ENDED then canCreate = true end --print(currentBall) if currentBall > 0 then for i = 0, table.maxn(ball) do if ball[i] == nil then else stroke(math.random(255),math.random(255),math.random(255),math.random(255)) fill(math.random(255),math.random(255),math.random(255),math.random(230,255)) ellipse(ball[i].x,ball[i].y,ball[i].radius*2) end end end end --# ChangeColor --Changing color background --By Zoyt ChangeColor = class() function ChangeColor:init(cc,r,g,b) self.phase = 0 self.changingpace = cc self.bgr = r self.bgg = g self.bgb = b end function ChangeColor:update() self.bgr = 128+ 127 * math.sin(self.phase / (2*math.pi)) self.bgg = 128+ 127 * math.sin(self.phase * 2/ (2*math.pi)) self.bgb = 128+ 127 * math.sin(self.phase * 3/ (2*math.pi)) self.phase = self.phase + self.changingpace if self.phase > (360 * 6) then self.phase = 0 end end function ChangeColor:getColor() return self.bgr,self.bgg,self.bgb end

Tell me what you think! I'll post a video and screenshot as soon as possible.

Comments

  • Posts: 2,364

    Hi Zoyt,

    You're the man. Just what I've been looking for, once I decipher it.
    One question - you have an If..Elseif..end routine which appears to have two identical options present. I take it this is for future planned options?

    Thanks again.

    Bri_G

  • Posts: 2,820

    @Bri_G - Thanks. And where I'd the test statement? I'm working on an option right now to allow you to add squares.

Sign In or Register to comment.