# linearVelocity.length() changes after collision with walls

Hi, It’s my first topic.

I want to create a break-out game using babylon.js. But I faced some problems I cant set constant speed to ball. It loses speed after collide with walls. How can I avoid this problem.

I set velocity at line 54

@Cedric

Welcome @Cmexxx !!! you figured well that @Cedric will be the king for this question

1 Like

Hello Cmexxx!

I know I’m not Cedric, but hopefully I can provide some illumination that isn’t too badly off-course for you to find useful

When you set up your physics impostors, the values you provide essentially instruct the physics engine whether to treat an object like a rubber ball, a brick, or somewhere in between the two when it comes to collisions. Colliding with the walls will cause your ball to lose speed when the collision is either fully elastic, or if one party to the collision (I’m looking at you, zero-mass walls) doesn’t care about conserving momentum.

The friction and restitution properties will dictate velocity losses from collisions; try setting the friction to 0 and restitution to 0.9999 or 1 and see if that does the trick.

HTH

Thanks a lot @jelster !!!

1 Like

Thanks a lot for your answer, but I didn’t see any changes after setting friction to 0 for walls (I think 0 is default value isn’t it?). I read a lot of information from different topics about similar problems. It helped me to understand a lot of new details, but not to solve my problem

That’s almost there I think, but both dance partners need to be moving to the same tune - both bodies in the collision need to have the combination of settings for it to work as expected.

Make sure you set the restitution on the ball doing the colliding - your walls are magic and don’t recoil from collisions, after all.

Another thing to consider is angular velocity. After colliding at an angle, linear velocity can be exchanged for angular so check to see if your missing linear velocity is going there too. That’s not a physics engine thing, it’s just physics

HTH

Hi @Cmexxx

Each frame, with `scene.onBeforeRenderObservable`, I would force the linear velocity magnitude to the value I want for the gameplay. That way, you don’t have to deal with friction, collision,…
So, I would get the linear velocity, normalize it, then multiply with the speed I want. It’s important to keep the velocity direction computed by the physics engine. Also, you can zero the component on the Y axis so the ball stay on the ground. Physics engine can add a bounce for example that will move the ball in the air.

BTW, using a physics engine seems a bit overkill to me. Personnaly, I would do the ball motion myself and because I know where walls and bricks are, I would do the collision detection. But that’s my way of doing things. Feel free to continue with a physics engine, the end result is what matters

4 Likes

Thanks a lot @Cedric.
That’s my code snippet for community:
`let speedVec = new BABYLON.Vector3(2, 2, 2);`
`let reslutVec = BABYLON.Vector3.Zero();`
`scene.onBeforeRenderObservable.add(() => {`
`reslutVec = sphere.physicsImpostor.getLinearVelocity().normalize().multiply(speedVec);`
`sphere.physicsImpostor.setLinearVelocity(reslutVec);`
`});`

3 Likes