It will probably not occur at all because the interval between drawing is not exactly 1/60 second, so instead of 1 you will get 1.000329 or something like it.
@RonJeffries Actually, the ellipse would be drawn constantly. As ElapsedTime increases and is equal to 1, doStuff increments to 1. If doStuff is 1, the ellipse is drawn. As ElapsedTime increases, it will never be equal to 1 again, so doStuff will never increase and will stay at 1, which will continue to draw the ellipse. But this is assuming that ElapsedTime will equal 1 as it increases.
@Ignatz I was going by the way the code was originally written and assuming that ElapsedTime would equal 1. Then the ellipse would be drawn from that point forward. I'm sure if that code was run enough times, ElapsedTime would equal 1 at least once, but I'm not going to try and verify that.
Just run this code. It will print a message if ElapsedTime is ever an integer, not just 1, but 2,3,4,...
Don't hold your breath waiting...
function setup()
parameter.text("Time","")
end
function draw()
if ElapsedTime==math.floor(ElapsedTime) then
print("success after "..ElapsedTime.." seconds")
end
Time=math.floor(ElapsedTime)
end
@Ignatz If something can happen, given enough time, it will. If your code ran long enough, or was restarted enough times, it will print success after some integer at least once. I'm not saying it would happen in my lifetime or yours or anyone's, but it would happen. If you take a zillion coins and throw them in the air enough times, they'll all land on heads or tails at least once. If you can create a truely random number, then every number, no matter how large or small, has the same chance of be chosen.
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
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?
Comments
Yes it is, but it is used to turn on an indicator, which is what decides if the ellipse is drawn
ISTM that ElapsedTime == 1 will occur at most once.
It will probably not occur at all because the interval between drawing is not exactly 1/60 second, so instead of 1 you will get 1.000329 or something like it.
right, thus "at most once"
@RonJeffries Actually, the ellipse would be drawn constantly. As ElapsedTime increases and is equal to 1, doStuff increments to 1. If doStuff is 1, the ellipse is drawn. As ElapsedTime increases, it will never be equal to 1 again, so doStuff will never increase and will stay at 1, which will continue to draw the ellipse. But this is assuming that ElapsedTime will equal 1 as it increases.
ElapsedTime won't ever exactly equal 1, that's why I used a > test.
@Ignatz I was going by the way the code was originally written and assuming that ElapsedTime would equal 1. Then the ellipse would be drawn from that point forward. I'm sure if that code was run enough times, ElapsedTime would equal 1 at least once, but I'm not going to try and verify that.
I don't know how many times I need to say this.
ElapsedTime is NEVER likely to equal 1.
Just run this code. It will print a message if ElapsedTime is ever an integer, not just 1, but 2,3,4,...
Don't hold your breath waiting...
@Ignatz If something can happen, given enough time, it will. If your code ran long enough, or was restarted enough times, it will print
success after
some integer at least once. I'm not saying it would happen in my lifetime or yours or anyone's, but it would happen. If you take a zillion coins and throw them in the air enough times, they'll all land on heads or tails at least once. If you can create a truely random number, then every number, no matter how large or small, has the same chance of be chosen.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 ?= .
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.
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?