Problem with "while loop"

Hello everybody

I’m trying to achieve a more realistic result of an object bouncing than I get by just using the material elasticity (I’m quite new to Blender and haven’t understood yet how this elasticity exactly works)
But the script I use crashed Blender, it seems to be stuck in a while loop.
I have some experience with Python but I don’t understand what the problem in this code is:

from bge import logic as G

cont = G.getCurrentController()
S = G.getCurrentScene().objects[“Sphere”]
v = 10

while v >= 1:
if cont.sensors[“Collision”].positive:
S.setLinearVelocity([0,0,v],0)
v /= 2

Thank you for your answers

P.S. sorry for this probably bad english xD

Not really sure but maybe replacing ‘v = 10’ with ‘v = 10.0’ will fix it. (Maybe ‘v /= 2’ with ‘v /= 2.0’ or ‘v *= 0.5’ as well)

Background: python has automatic type detection and casting and thus interprets ‘v=10’ as a declaration of an integer type variable. It then isnt capable of dividing 1 by 2.

I almost never use while loops in blender because you’d expect them to run over n frames but in reality it runs through the loop entirely in one frame. So when pulse mode is on and it’s running your while loop every frame, Blender will crash.

Nicholas_A is right.

Each script runs once per frame, meaning that while loop will only run in one frame. If your while loop takes 10 seconds to loop through, then the game will stall for 10 seconds… every frame (if you have the true pulse on).

Imagine all your code already is inside a giant while (true) loop, this should help you fix your problems.

Here’s a fix:


from bge import logic as G

cont = G.getCurrentController()
S = G.getCurrentScene().objects["Sphere"]
v = 10

def run():
     if v>= 1 and cont.sensors["Collision"].positive:
          S.setLinearVelocity([0,0,v],0)
          v /= 2

To run this, use an Always sensor, with the true pulse enabled, connected to a Python module controller with “<name_of_script>.run” in the text box.

PS: Remember to use code tags when copying and pasting code.

Your code should calculate a single step rather than all steps at the same frame:


import bge

controller = bge.logic.getCurrentController()
collisionSensor = controller.sensors["Collision"]

if collisionSensor.positive: # be aware this measures on owner rather then on sphere
    scene = bge.logic.getCurrentScene() # no need to get the scene earlier - it is not needed before the next line
    sphere = scene.objects["Sphere"]
    velocity = sphere.getLinearVelocity()
    if velocity.z &gt;= 1.0:
        velocity.z /= 2.0
        sphere.setLinearVelocity(velocity)

(untested, I hope you get the idea)

Basically you grab the current velocity and calculate the next one. Do that for every bouncing event (collision).

Hello every body…

I’d first like to deeply apologize for never reacting to your kind responses…It’s almost a year ago when i postet my question, well the problem back then was that I hadn’t managed to find out how to find my “own” Problem thread so that I gave up using this platform after a realy short time.
Luckily I found out a little time later how to solve the Problem by my own. (Back then I was programming a physics simulation to manualy simulate the influence of a collision for the angular and translational velocity of a spherical object)

I hope youre accepting this apology as I never intended to behave so ignorant…

Hello Antinosaur,

Thanks for your apology. I do not think that causes any headaches here :).
Glad you solved your issue by yourself.

The while loop will only cause FPS issues while the condition is not met…there are some conditions in which you may want to use one.