Yes, but he wanted something to happen when the program had been running 1 second - not just once in a bajillion times, but every time the program runs.
@Ignatz That was a good example of, the code doesn't give you what you want, even though it looks correct. Anyways, math on computers isn’t exact and you need to code for that.
We have == for equal, ~= for not equal, <= for less than or equal, and >= for greater than or equal. What we need is ?= for about equal. This would compare 2 numbers and be true if the numbers were within some limit of each other. The aboutEqual function in the code below shows how the program being discussed would run. I have the limit set to .01, so if ElapsedTime is within +/- .01 of 1, then the ellipse will be drawn. Or, the aboutEqual function could just be used instead of waiting for ?= .
function setup()
dostuff=0
end
function draw()
background(255, 255, 255, 255)
if aboutEqual(ElapsedTime,1,.01) then
dostuff=dostuff+1
end
strokeWidth(5)
fill(0)
if dostuff == 1 then
ellipse(100,100,100)
end
end
function aboutEqual(v1,v2,limit)
if math.abs(v1-v2) < limit then
return true
end
return false
end
Yes, you would think that Simeon, living in Australia, would have included a function based on the Aussie maxim “near enough is good enough” ![]()
Hi again,
I’ve just started to play around with physics bodies.
-- Bouncy thing
function setup()
bodies={}
box=physics.body(CHAIN,vec2(0,HEIGHT),vec2(0,0),vec2(WIDTH,0),vec2(WIDTH,HEIGHT),vec2(0,HEIGHT))
ball=physics.body(CIRCLE,50)
ball.x=WIDTH/2
ball.y=HEIGHT/2
ball.restitution=0.5
ball.gravityScale=0
ball.type=DYNAMIC
ball.bullet=true
ball.interpolate=true
ball.positions={}
ball.positions[1]=ball.position
ball.positions[2]=ball.position
i=1
player=physics.body(CIRCLE,50)
player.type=STATIC
table.insert(bodies,box)
table.insert(bodies,ball)
table.insert(bodies,player)
end
function draw()
background(0)
for k,v in pairs(bodies) do
drawBody(v)
end
ball.positions[i]=ball.position
ball.linearVelocity=(ball.positions[i]-ball.positions[3-i])*0.99/DeltaTime
i=3-i
end
function touched(t)
player.x=t.x
player.y=t.y
end
function drawBody(body)
pushStyle()
pushMatrix()
noFill()
strokeWidth(5)
stroke(255)
translate(body.x, body.y)
rotate(body.angle)
if body.shapeType == POLYGON then
local points = body.points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
elseif body.shapeType == CHAIN or body.shapeType == EDGE then
local points = body.points
for j = 1,#points-1 do
a = points[j]
b = points[j+1]
line(a.x, a.y, b.x, b.y)
end
elseif body.shapeType == CIRCLE then
-- line(0,0,body.radius-strokeWidth(),0)
ellipse(0,0,body.radius*2)
end
popMatrix()
popStyle()
end
EDIT: the problem seems to not be there any more. Is there a more elegant way to do the sliding? What are the drawbacks of setting bullet to true?