1
\$\begingroup\$

I have a landscape(generated via Perlin noise) and a ball. I want the ball to move along the geodesic(implementation of basic physics: gravitation, friction). I thought to do raycast around the ball to the landscape, choose the lowest point and move the ball to this point, but it won't work in every case and it won't allow the ball to jump (with inertia). So, what is the best way/algorithm to implement such feature?

P.S. I don't want to use any libraries.

Thanks.

\$\endgroup\$
4
\$\begingroup\$

The first step in implementing a physics engine is to model the movement of each object in form of a 3d velocity vector. Each object has current velocities in x, y and z direction. Each logic tick of your game engine, the object is moved by its current velocity. Changing the velocity-vector of an object by applying an acceleration-vector is the primary method of moving an object in a pyhsics engine. Other forms of position changes only occur in extraordinary circumstances, because an instant position change violates Newton's laws of motion and thus introduces implausible situations into your physics simulation.

To introduce gravity, change the z-vector (or whichever axis is "up" in your game) by the equivalent of 9.81 meters per second / your update rate.

To make the ball bounce, check for collisions with the ground. When you have a collision, mirror the current movement vector at the plane on which the collision happened. For a flat plane, that would mean to just invert the z-vector. When you have angled planes, it gets a bit more complex.

Then multiply the vector by the "bounciness" of the object. The bounciness is somewhere between 1.0 for super-bouncy (bounces forever) or 0.0 for completely unbouncy (stops completely when touching a surface).

But seriously, you are reinventing the wheel here. There is no shame in using 3rd party libraries. There are lots of physics engines available which already implement this better than you could. Just use the tools the development community has to offer and use the time you safe to build your game.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.