Page 1 of 1

Re: How to get the position of a moving object once? without it being updated

Posted: Sun Feb 04, 2024 5:07 am
by Jonn
I have an object moving "freely" with body:applyForce( x, y ).

I want to track it's previous position.

I set a timer and with every second that goes by i get the position of the moving object, the problem is the position keeps updating to the object's current position. that's not what i want. I want it's position one second ago.

I have like 10 ideas on how to do what i want to do, i just think there has to be a simpler and easier way.

the closest i got was a small if statement that inserts the position into a table but again the problem is that the position keeps updating to the current position not the previous one.

Code: Select all

object = {}
object.x = 250
object.y = 200
object.body = love.physics.newBody(world, object.x, object.y, 'dynamic')
object.shape = love.physics.newCircleShape(5)
object.fixture = love.physics.newFixture(object.body, object.shape, 1)
--
object.body:applyForce( 5, 0 )
-- the object is moving to the right, on the x-axis
what i want is to track it's position while it's traveling. something like

Code: Select all

function objectTracking()
    timer = timer + dt
    if timer == 0 then
        varPreviousPositionX = object.body:getX()
        varPreviousPositionY = object.body:getY()
    elseif timer == 1 then
        timer = 0
    end
end
that doesn't work. just wanted to write something quick to help you see the problem.

Re: How to get the position of a moving object once? without it being updated

Posted: Sun Feb 04, 2024 3:24 pm
by Bobble68
Your example will record the position at 1 second intervals, which means that there's nothing to stop you from accessing it the frame after it was recorded. The main problem you're facing here is that (I assume) you don't know when the old position will need to be accessed, so you'll need to record the position every frame that could be needed a second from then. What this would mean practically would be this kind of system:

Every fame
> Add current position to record table with a time stamp
> Remove all positions from the table older than a second

When you need to access the position
> Look for the oldest value

This should be the way to do it, assuming I'm understanding what you want exactly.

Re: How to get the position of a moving object once? without it being updated

Posted: Mon Feb 05, 2024 1:23 am
by vilonis
If you’re hung up on efficiency, hence having trouble choking from your 10 ideas, try not to worry about it!

If that’s not possible, implement Bobbie68’s idea as a ring buffer.