Page 1 of 1

How do I respawn an object with Windfield?

Posted: Wed Jan 04, 2023 1:55 pm
by MaxGamz
I'm trying to develop a simple sports game where when a goal is scored, the ball goes back to it's original position after it bounces twice (it's a tennis like game). However when I tried implementing it in wind field, the ball and it's collision shape become detached and it doesn't work as intended.

Re: How do I respawn an object with Windfield?

Posted: Wed Jan 04, 2023 2:54 pm
by MrFariator
You'll need to either remove and reinsert the collider to the physics world, or update the collider's location to the spawn point without removing it. Decoupling probably happens if you're updating the visual representation, but not the physics representation, or vice versa.

Re: How do I respawn an object with Windfield?

Posted: Wed Jan 04, 2023 3:53 pm
by MaxGamz
MrFariator wrote: Wed Jan 04, 2023 2:54 pm You'll need to either remove and reinsert the collider to the physics world, or update the collider's location to the spawn point without removing it. Decoupling probably happens if you're updating the visual representation, but not the physics representation, or vice versa.
I tried to see if I can destroy and create a new collider but I'm having the same issue

(the code is under the ball.lua file)

Re: How do I respawn an object with Windfield?

Posted: Wed Jan 04, 2023 4:13 pm
by MrFariator
You seem to be amplifying your draw coordinates when rendering the ball, by a factor of two. As a proof of concept of that, this fixed the issue:

Code: Select all

function Ball:winOrLose()
    if bounceCounter == 2  and self.x < 320 then
        playerTwoScore = playerTwoScore + 1

        self.ball:destroy()
        self.x = 600
        self.y = 100
        self.ball = World:newCircleCollider(self.x, self.y, self.rad)
        self.x = 600/2 -- render the ball at half of the physics' representation coordinates
        self.y = 100/2
        -- ...rest of code
As such, this doesn't seem to be an issue with windfield, but rather with how you're rendering the visuals. Somewhere in your code the visual and physics coordinates stop matching, creating this sort of decoupling issue when you try to reset the ball. Would need to look further into your main.lua to better understand the issue.

Re: How do I respawn an object with Windfield?

Posted: Wed Jan 04, 2023 7:09 pm
by MaxGamz
MrFariator wrote: Wed Jan 04, 2023 4:13 pm You seem to be amplifying your draw coordinates when rendering the ball, by a factor of two. As a proof of concept of that, this fixed the issue:

Code: Select all

function Ball:winOrLose()
    if bounceCounter == 2  and self.x < 320 then
        playerTwoScore = playerTwoScore + 1

        self.ball:destroy()
        self.x = 600
        self.y = 100
        self.ball = World:newCircleCollider(self.x, self.y, self.rad)
        self.x = 600/2 -- render the ball at half of the physics' representation coordinates
        self.y = 100/2
        -- ...rest of code
As such, this doesn't seem to be an issue with windfield, but rather with how you're rendering the visuals. Somewhere in your code the visual and physics coordinates stop matching, creating this sort of decoupling issue when you try to reset the ball. Would need to look further into your main.lua to better understand the issue.
The ball did get set back to the original position but it's not falling. I tried setting the type to dynamic but it still stayed in place

Re: How do I respawn an object with Windfield?

Posted: Wed Jan 04, 2023 8:13 pm
by MrFariator
Oh, right. That's because you never set the bounceCounter back to 0, so the ball is repeatedly getting reset back to its starting position due to the code in the winOrLose function.

Re: How do I respawn an object with Windfield?

Posted: Thu Jan 05, 2023 1:24 am
by MaxGamz
Oh I didn't think of that. Sorry for overlooking something so simple. Thanks for the correction!