How to create whirlpool/vortex effect?

时间秒杀一切 提交于 2019-12-02 07:09:33

If by "spin" you mean that the falling object would move along a curve or a spiral, rather then changing the direction of movement immediately towards the black hole, there is an easy fix for that.

ball.getBody().setLinearVelocity(0, 0);

This completely stops the current movement of the body. I would start by removing that line. Also, for better realistic behaviour, you can follow the proper formula to compute attractive force, which goes something like this:

force = mass1 * mass2 * [some constant] / (distance ^ 2)

When you have the vector from your body towards the black hole (computed as black hole position - body position), the length of the vector is the distance, and after normalizing and multiplying by the force, you have the desired forceX and forceY force vector that needs to be applied to the body each update, as long as it stays in range of the hole.

However this formula will cause the force to grow to infinity as body moves closer to the hole, so you could try changing to linear conversion (closest = 1, farest = 0) if that causes any trouble.

force = mass1 * mass2 * [some constant] * ( (maxDistance - distance) / maxDistance )

You want to implement a tangential force with a magnitude that increases towards the center of the vortex.

Here's some pseudocode.

radialVector = objectPosition - vortexPosition;
tangentialVector = radialVector.perpendicularVector();

if (radialVector.length() < vortexRadius) {

    // Swirl faster when near the center of the vortex.
    // Max tangential force when distance from center is 0.
    // Min tangential force when distance from center is vortexRadius.
    forceMagnitude = map(radialVector.length(), vortexRadius, 0, minTangentialForce, maxTangentialForce);
    force = forceMagnitude * tangentialVector.normalize();
    object.applyForce(force);

}

Here's an image that shows the vector components:

To create a whirlpool effect there should be increasing radial (Fr) and tangential (Ft) forces as the object moves closer to the center.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!