# Physics Upon Collision move objects back

Trying to do a bumper game

I have a platform and 2 spheres on it. I want upon collision both spheres to push back (like billard)

Is there a native function to do that?

Also some “problems” (probably natural things but I want to disable them):

Is it possible for the spheres to not rotate from collision (rotate only on Y axis based on user’s mouse cursor position not because of collision)

After some time the spheres start bouncing on the platform is it possible to disable the Y force (although I do want whenever a sphere go off the platform to fall down)

Thanks!!!

I tried “programmatically” but it’s not very natural xD (I saw on net something like addInPulse or angular/linear velocity is this the correct approach? )

``````            // Calculate the direction from car1 to car2
const direction = item.position.subtract(car.position).normalize();
// Define the transition distance (4 units in this example)
const transitionDistance = 2;
// Calculate the target position for both cars (move back by transitionDistance)
const targetPosition1 = car.position.subtract(
direction.scale(
multiply2 *
transitionDistance *
)
);
direction.scale(
transitionDistance *
multiply1 *
)
);

// Create animation for car1 to smoothly transition to the target position
const animation1 = new Animation(
"moveBack1",
"position",
40,
Animation.ANIMATIONTYPE_VECTOR3,
Animation.ANIMATIONLOOPMODE_CONSTANT
);

// Define keyframes for the animation of car1
const keys1 = [];
keys1.push({
frame: 0,
value: car.position.clone(), // Start position
});
keys1.push({
frame: 60, // Duration of animation (in frames, adjust as needed)
value: targetPosition1, // Target position
});

// Set keys to the animation of car1
animation1.setKeys(keys1);

// Apply animation to car1
car.animations.push(animation1);
scene.beginAnimation(car, 0, 60, false, 3); // Start the animation for car1

// Create animation for car2 to smoothly transition to the target position
const animation2 = new Animation(
"moveBack2",
"position",
40,
Animation.ANIMATIONTYPE_VECTOR3,
Animation.ANIMATIONLOOPMODE_CONSTANT
);

// Define keyframes for the animation of car2
const keys2 = [];
keys2.push({
frame: 0,
value: item.position.clone(), // Start position
});
keys2.push({
frame: 60, // Duration of animation (in frames, adjust as needed)
value: targetPosition2, // Target position
});

// Set keys to the animation of car2
animation2.setKeys(keys2);

// Apply animation to car2
item.animations.push(animation2);
scene.beginAnimation(item, 0, 60, false, 3); // Start the animation for car2
``````

cc @Cedric

Something like this ? Babylon.js Playground
also check examples on these pages : Babylon.js docs

Yes something like that but a bit more bouncing back (a bit against of Third Law of Force Physics )

I use Nextjs and I haven’t been able yet to import Havok plugin (I’ve read
RaananW’s solution for Vite but I could find something similar for Nextjs - trying to load the plugin from a location that doesn’t exist)

So in my project I’m using cannon.js (since I wasn’t able to fix the error with nextjs + havok plugin) and I moving the spheres using addInPlace.

But I see fromm https://playground.babylonjs.com/#MKR6NF#1 that using addInPlace doesn’t work for Havok Physics? I will ask for a bit of help integrating Havok plugin in Nextjs and try your approach with applyForce

addinplace translate a vector3. it’s independent of physics engine.

https://doc.babylonjs.com/typedoc/classes/BABYLON.Vector3

I see.

Although i can’t figure out why in your example (#MKR6NF#)1 while I added the moveToTarget function with addInPlace it doesn’t move my sphere(first sphere) while in my example it does https://playground.babylonjs.com/#5YVAH0#2

I am new in game developing (1 month xD) So I want to make a game where player steering his constant moving sphere with mouse cursor and upon physics collision between 2 spheres I want a burst bounce back.

I have used addInPlace for the movement of spheres (and old Physics API), is that ok or it’s better to use setLinearVelocity (with change of sphere’s direction by mouse cursor’s position)

@Cedric I migrate my code to havok/ Physics v2 API and I used Force instead of addInPlace in order to move the spheres/“cars”.

I want to recreate this game (bumper-io)

I have set limit on frame rates (the higher the fps the higher the velocity, but I want constant velocity on different devices/fps) and also i have applied world’s havok speed limits because I don’t want the “cars” to accelerate to very high velocities.

Prolems :

1. I have applied Impulse in Collision callback function but It doesn’t see (when I change the value of the Vector3 for higher magnitude of the applied Impulse, the spheres/cars doesn’t bounce farther)

Probably because I have a velocity limiter? Is it possible to get that prefered constant velocity when spheres is moving freely and increase the velocity upon collision based on the applied Impulses/forces? Or I have to have checks/flags and occasionally increase the max allowed velocity if the spheres has been on a collision

2)When a sphere goes outside the ground/platform there is a delay before it start falling (reduce position.y) Increasing gravity doesn’t do much xD (UPDATE: Actually setting gravity to -100 improved this)

1. I don’t want the sphere to rotate on y axis from the collisions or movement. I want always to face the direction of the mouse cursor. This is possible only with creating a secondary Mesh non-PhysicBody and apply the graphics on that?

2. Also occasionally the spheres might travel on Y axis but I don’t want the spheres to jump only negative y (falling off the platform → losing) is this something possible with the Physics Engine (apply very high gravity?)

Thanks again!

I would compute a speed limiter that way:

``````desired speed = current speed + acceleration * (1- current speed/max speed)
``````

basically, the acceleration tends to 0 when current speed approached maximum speed.
at speed 0, acceleration is maximum. at speed = max speed, acceleration is 0.

You’ll need to adapt that for braking/collision but the principle stays the same: modulate value with `current speed/max speed`

1 Like

Thanks Cedric, Also another question if you could help

I’ve seen that on 35 fps (locked) the speed of my spheres is nice/good
If I drop the fps the spheres’ speed is descrease if I increase the fps (120) the speed is increased a lot, I believe physics /speed is related on frames and not time. What is a good way to lock the game speed despite the frame rates? I should use the engine deltaTime probably, there is a way to apply it on Physics/forces or I should apply it on desired speed equation?

`desired speed = current speed + acceleration * (1- current speed/max speed)`

I’ll write a blog post about framerate independant interpolation. It should be released this week.

5 Likes

Thank you I’ll wait for this