Howdy, Stranger!

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

An assortment of questions.

2»

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    Yes it is, but it is used to turn on an indicator, which is what decides if the ellipse is drawn

  • Posts: 825

    ISTM that ElapsedTime == 1 will occur at most once.

  • IgnatzIgnatz Mod
    Posts: 5,396

    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.

  • Posts: 825

    right, thus "at most once" :smile:

  • dave1707dave1707 Mod
    Posts: 8,791

    @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.

  • IgnatzIgnatz Mod
    Posts: 5,396

    ElapsedTime won't ever exactly equal 1, that's why I used a > test.

  • dave1707dave1707 Mod
    Posts: 8,791

    @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.

  • IgnatzIgnatz Mod
    Posts: 5,396

    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...

    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
    
  • dave1707dave1707 Mod
    Posts: 8,791

    @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.

  • IgnatzIgnatz Mod
    Posts: 5,396

    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.

  • dave1707dave1707 Mod
    Posts: 8,791

    @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.

  • dave1707dave1707 Mod
    Posts: 8,791

    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
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    Yes, you would think that Simeon, living in Australia, would have included a function based on the Aussie maxim "near enough is good enough" :D

  • edited June 2016 Posts: 110

    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?

Sign In or Register to comment.