Calculator Community > Lua

Lua Q&A

<< < (42/42)

Jim Bauwens:

--- Quote from: AnToX98 on March 30, 2014, 08:57:28 am ---Please could someone help me :( ?

--- End quote ---

Try calling the setPos function of the body you attached the segment to.

AnToX98:
Here's my code :

--- 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, 10)
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: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.)

sol = Seg(0,200,w,200)

ball = Ball(w/2-10, 30, 20, 1000)

sol.body:setPos(physics.Vect(0,-30))

timer.start(0.01)
end
initGame()

function on.timer()
space:step(0.1)
platform.window:invalidate()
end

function on.paint(gc)
ball:paint(gc)
sol:paint(gc)
end
--- End code ---

sol.body:setPos(physics.Vect(0,-30))
[/size]
[/size][size=78%]Strangely, the body moves but not the shape....[/size][/code]