Speed of physics body

In my code i “slingshot” a disc (physics body) across a board, but I want something to happen (advance to the next player’s turn) when the disc has stopped moving. I use applyForce to fling the disc, but it seems that this isn’t actually affecting the body’s linearVelocity, which is what I’m trying to use to determine when to end the current player’s turn, I.e. if body.linearVelocity == vec2(0,0) then nextPlayer().

A i missing something stupid? Is my syntax wrong in the example above? I can think of a way to work around this, but just wanted to check first and see if this is expected behavior (applyForce() not affecting linearVelocity).

Yep!
Vec2(0,0) is a vec2 object different of body.linearVelocity vec2 object, so you are comparing their adress, not their values! Try to compare v:len()==0 instead, or, better, v:len()<0.1

Boom! Thank you, learned something new today!

One detail though: when I check the value of body.linearVelocity by printing it to the console, it shows (0.00000,0.000000) before and after i apply force. I haven’t tried your fix yet, but wanted to see if anyone knew why that was?

You should share the code. It is difficult to answer without.
The force ay be too small, and/or the application duration too short. Or you dont really apply it… Etc etc…

You could also check the x,y value of the disc. When it doesn’t change, it’s stopped.

@dave1707 that is my backup plan :).

I’ll share more code once i get off the couch and to a proper computer. Thanks all for your help so far!

I never shared any code, I’m sure you were all on the edge of your seats with excitement.

The short version is that it appears my code is working as intended, if not exactly as expected. I actually moved the linearVelocity == vec2(0,0) test into the draw function, which makes more sense anyway, and it actually appears to work, even though it shouldn’t. The problem is that it takes a looong time relatively speaking.

If I crank up the linearDamping then it takes less time, which makes me think that long after the physics body is showing a vec2 of 0.00000 it is actually slightly moving below the threshold of the significant digits?

The questions is why does it work at all? It seems like if you are comparing linearVelocity to a vec2 of zero, then it figures out what you’re trying to do. If I try to set vec2 to something a little higher, like do a comparison of velocity <= vec2(0.1,0.1) then it throws up the comparing userdata error. Again, using vec2(0,0) gives me no errors and it works, but takes too long to be useful.

In other words, it works but not well enough, so I’ll be switching to one of the earlier suggestions in this thread. Obviously I could just make an “I’m done with my turn” button but I’d rather avoid that “cheat”.

I’m making a prototype of Crokinole if anyone is interested. You can play the game fully although it doesn’t quite know how to score yet, once I get that implemented maybe i’ll pop it into the Code Sharing forum.

Thanks again for the suggestions everyone!

Use vec2:len() to get the length of the vector and if its under say 5 then skip to the next scene, use if disc.linearVelocity:len() < 5

Yeah, it works like a charm using len() of course. I’m sometimes stubborn and want to figure out why my way is wrong before I take good advice and do it the right way!