Ask coding questions

← Back to all posts
Collision Responses (Physics)
tussiez (1668)

I'm working on physics for SortaCraft once again. My original version of "physics" wasn't very realistic, so I'm trying to improve on it.

I can tell if the player is inside something or not with Raycast.fromCamera(), but movement doesn't feel very real (e.g sliding along blocks inside of just preventing movement)

What should I do to make "realistic" physics?

As I said above, I can check for a collision, and update the player position (ignore controls, these are independent)

Here's a good example:

19wintersp (1142)

If you wanted to implement sliding past things, rather than moving the player object in terms of manually changing player position, apply a force in the required direction.

tussiez (1668)

@19wintersp Lol
Anyhow, how exactly would I implement this? Should I just slowly increase velocity? This isn't hard, I already do this when jumping. OH - move the player in two directions at once? I can't word sentences today, sorry :/

tussiez (1668)

@tussiez Possibly reduce the velocity in one direction and add it to another

19wintersp (1142)

@tussiez You can edit the velocity manually, though does your physics library have built-in methods for adding forces?

19wintersp (1142)

@tussiez CannonJS has one, if you're using that:

applyForce(force , worldPoint)
Defined in src/objects/Body.js:637

Apply force to a world point. This could for example be a point on the Body surface. Applying force this way will add to Body.force and Body.torque.


  • force (Vec3)
    The amount of force to add.

  • worldPoint (Vec3)
    A world point to apply the force on.

tussiez (1668)

1. No physics engine (this is why I'm asking)
2. I tried using CANNON, however creating custom ConvexPolyhedrons is much too expensive and makes the game infinitely slow (unfortunately)

19wintersp (1142)

@tussiez Okay then, implementing forces in your physics engine. The main thing is calculating velocity and rotation from force, friction, resistance, mass, and other fun stuff. I'm afraid I'm probably not the one to ask here. Good luck though!