How to create whirlpool/vortex effect?

2019-02-28 03:16发布

Im trying to make a Vortex effect on a Circle Body that is a Sensor. I've been looking for this and all examples i look for are in C++ or Objective C and i dont seem to translate them well.

when my objects collition, it calls beginContact(..) and it sets a flag so that i can call bodyToUpdate.applyForce(...);

 public void beginContact(Contact contact) {
             setColliding(true);
 }

 //updating collition every frame
 public void act(){
     if (colliding) {
        ball.getBody().applyForce(....);

 }

how to calculate the amount of force to apply every frame to make it a vortex?

Edit:

so i now have the object going straight to the center of the vortex, but no "spin"

public void act() {
    if (colliding) {
        ball.getBody().setLinearVelocity(0, 0);

        ball.getBody().applyForce((portal.getBody().getPosition().x - ball.getBody().getPosition().x) * i,
                (portal.getBody().getPosition().y - ball.getBody().getPosition().y) * i,
                ball.getBody().getPosition().x, ball.getBody().getPosition().y, true);

        i++;
    } else
        i = 10;
}

2条回答
Summer. ? 凉城
2楼-- · 2019-02-28 03:58

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 )
查看更多
男人必须洒脱
3楼-- · 2019-02-28 04:12

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:

Radial and tangential components.

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

查看更多
登录 后发表回答