How to create whirlpool/vortex effect?

谁都会走 提交于 2019-12-02 07:49:29

问题


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;
}

回答1:


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 )



回答2:


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.



来源:https://stackoverflow.com/questions/25940650/how-to-create-whirlpool-vortex-effect

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