Calculator Community > Lua

Chipmunk Physics

<< < (3/4) > >>

Nick:

--- Quote from: Augs on January 07, 2013, 02:37:14 pm ---Sounds interesting, any one have a video?

--- End quote ---
A video of the chipmunk engine? Well, if you have the student software a handheld, you could try the code Jim posted above (they're really nice tbh)


--- Quote from: ElementCoder on January 08, 2013, 10:55:21 am ---@Nick My eat nethams game seems to run somewhat faster on 3.2 than it did on 3.1. The lobsters definitely fall smoother when setting it to the 100% spawn rate. It's not much of an improvement, but to me it seems to be something like 5-10%. It also could be I'm just imagining it, sadly I do not own 2 CXes so I can't test :p

--- End quote ---
That's what I hoped, that it would be faster, but still :o pff, I guess we could never expect it to be superlightningthunderboltfast, but at least a little afster than now xs but yeah, any improvement is nice..

Rhombicuboctahedron:

--- Quote from: Nick on January 08, 2013, 12:09:09 pm ---
--- Quote from: Augs on January 07, 2013, 02:37:14 pm ---Sounds interesting, any one have a video?

--- End quote ---
A video of the chipmunk engine? Well, if you have the student software a handheld, you could try the code Jim posted above (they're really nice tbh)

--- End quote ---

Or watch this

AnToX98:
Thank you jim's !
It's so hard to start using physics, your example code really helped me :)

AnToX98:
Can we modify shapes position ? I

AnToX98:
Hi jims :)



I'm back with my falldown but I have 2 problems :
- The ball don't falls in the hole --'
- It slows down so much : why ?


--- Code: ---------------------------------------------------
----------- LUA FALLDOWN, BY ANTOX98 -----------
------------------------------------------------

platform.apilevel = "2.0"

require "physics"

----------------------
----- BALL CLASS -----
----------------------

Ball = class()
Seg = class()

function Ball:init(x, y, w, mass)   
    self.width = w
    self.body = physics.Body(mass, physics.misc.momentForCircle(mass, 0, 10, ZERO))
    self.body:setPos(physics.Vect(x, y))
    self.body:setMass(mass)
    self.shape = physics.CircleShape(self.body, w, ZERO)
    self.shape:setRestitution(0.6)
    self.shape:setFriction(0.6)
end

function Seg:init(x1, y1, x2, y2)
    local a, b = physics.Vect(x1, y1), physics.Vect(x2, y2)       
    local mass = physics.misc.INFINITY()
    self.coor = {x1,y1,x2,y2}
    self.body = physics.Body(mass, physics.misc.momentForSegment(mass, a, b))
    --self.body:setPos(physics.Vect(x1, y1))
    self.body:setMass(mass)
    self.shape = physics.SegmentShape(self.body, a, b, 18)
    self.shape:setRestitution(0.6)
    self.shape:setFriction(0.6)
end

function Ball:paint(gc)
    local p = self.body:pos()
    local x, y = p:x(), p:y()
    local r = self.width / 2
   
    gc:setColorRGB(255,0,0)
    gc:fillArc(x+r, y+r , self.width, self.width, 0, 360)
end

function Seg:paint(gc)
   
    local a = self.shape:a()
    local b = self.shape:b()
    gc:setPen("thick")
    gc:setColorRGB(0,0,0)
    gc:drawLine(a:x(), a:y(), b:x(), b:y())
end

function initGame()
    w = 318
    h = 212
   
    ZERO = physics.Vect(0,0)
    LARGE = physics.misc.INFINITY()
   
    space = physics.Space()
    space:setGravity(physics.Vect(0,9.8))
   
    count = 100
   
    walls = {Seg(0,0,0,h), Seg(0,h,w,h), Seg(w,h,w,0)}
    for i = 1, #walls do
        space:addShape(walls[i].shape)   
    end
   
    seg = {}
    --space:addBody(sol.body)

   
    ball = Ball(w/2-10, 30, 20, 1000)
   
    space:addBody(ball.body)
    space:addShape(ball.shape)
   
    timer.start(0.01)
end
initGame()

function on.timer()
    space:step(0.1)
   
    if count == 100 then
        count = 0
        generate()
    else   
        count = count + 1
    end   
    moveSeg(0.5)
    platform.window:invalidate()
end

function generate()
    r = math.random(0, w-40)
   
    seg[#seg+1] = Seg(0, h, r, h)
    seg[#seg+1] = Seg(r+40, h, w, h)
    space:addShape(seg[#seg-1].shape)
    space:addShape(seg[#seg].shape)
end

function moveSeg(n)
    for i = 1, #seg do
        seg[i].coor = {seg[i].coor[1],seg[i].coor[2]-n,seg[i].coor[3],seg[i].coor[4]-n}
        local a, b = physics.Vect(seg[i].coor[1], seg[i].coor[2]), physics.Vect(seg[i].coor[3], seg[i].coor[4])       
        local mass = physics.misc.INFINITY()       
        seg[i].body = physics.Body(mass, physics.misc.momentForSegment(mass, a, b))
        --self.body:setPos(physics.Vect(x1, y1))
        seg[i].body:setMass(mass)
        seg[i].shape = physics.SegmentShape(seg[i].body, a, b, 18)
        seg[i].shape:setRestitution(0.6)
        seg[i].shape:setFriction(0.6)
        space:addShape(seg[i].shape)
    end   
end

function on.arrowKey(key)
    if key == "right" then
        ball.body:setAngVel(5)
    elseif key == "left" then
        ball.body:setAngVel(-5)
    end
end

function on.paint(gc)
    ball:paint(gc)   
    for i = 1, #seg do
        seg[i]:paint(gc)
    end
 
   
end
--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version