Tweens on physics objects.

UPDATE: I figured out how not to call it, using tweens and @yojimbo2000’s way. I set a tween.delay for a function which set the same tween delay, so every 5 seconds the direction would reverse.

@MattthewLXXIII You could use tween.loop.pingpong which would cause a tween to reverse directions between 2 points. And adjusting the time value, you could set the speed to whatever you wanted. That’s what I’m using in my example above to move the object going up and down. You don’t have to set a delay to set another delay.

@dave1707 but you still have to redo the position in the draw function. Why is this? Doesn’t changing the x value of a vector change whatever runs off that vector?

@MattthewLXXIII Way above you said you wanted a sliding object that moved back and forth. That’s perfect for a tween. You said just above but you still have to redo the position in the draw function. The tween is doing the work of calculation the new x,y values based on where it is and the time frame you want it to move in. In the draw function, you’re just drawing the object at whatever the x,y values are. You don’t have to do anything, the tween is doing it all for you. Maybe I don’t understand exactly what you’re after.

function setup()
    sp={x=0,y=HEIGHT/2}
    tween(5,sp,{x=WIDTH,y=HEIGHT/2},{loop=tween.loop.pingpong})
end

function draw()
    background(0)
    sprite("Planet Cute:Character Boy",sp.x,sp.y)
end

In your draw() function you have:

    c1.x=sp1.x
    c1.y=sp1.y

What I meant was: why do you have to do this, as opposed to just changing c1.x and c1.y in the tween?
If I put that in a draw function then I would have to check which map it was, which I am not too keen to do (IMO it looks neater without references to specific maps in the general draw() function).

@MattthewLXXIII I understand what you mean now. I guess I was in a hurry and just took one of my existing tween programs and made some quick changes to it. Here’s some code where I removed what you were asking about.

displayMode(FULLSCREEN)

function setup()
    p1={x=100,y=100}
    p2={x=100,y=HEIGHT-100}
    p3={x=WIDTH-100,y=HEIGHT-100}
    p4={x=WIDTH-100,y=100}
    p5={x=100,y=100}

    physics.continuous=true
    c1=physics.body(CIRCLE,40)
    c1.x=WIDTH/2
    c1.y=40
    c1.type=KINEMATIC
    c1.restitution=1

    c2=physics.body(CIRCLE,40)
    c2.x=40
    c2.y=HEIGHT/2
    c2.type=KINEMATIC
    c2.restitution=1
    
    e1=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    e2=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    e3=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    e4=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT)) 
    
    tab={}
    for z=1,20 do
        b=physics.body(CIRCLE,20)
        b.x=math.random(WIDTH)
        b.y=math.random(HEIGHT-50,HEIGHT)
        b.restitution=1.1
        table.insert(tab,b)
    end
    
    tween(4,c1,{x=WIDTH/2,y=HEIGHT-40},{loop=tween.loop.pingpong})
    tween.path(8,c2,{p1,p3,p4,p2,p5},{easing=tween.easing.linear,loop=tween.loop.forever})
end

function draw()
    background(40, 40, 50)
    fill(255, 0, 0, 255)
    ellipse(c1.x,c1.y,80)
    ellipse(c2.x,c2.y,80)
    fill(255)
    for a,b in pairs(tab) do
        ellipse(b.x,b.y,40)
    end
end

Huh. That works perfectly fine. I don’t know what I did wrong then. Thanks @dave1707.