如何创建漩涡/漩涡效果?
我试图在作为传感器的圆形体上制作涡流效果.我一直在寻找这个,我寻找的所有示例都是用 C++ 或 Objective C 编写的,我似乎翻译得不好.
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.
当我的对象碰撞时,它会调用 beginContact(..) 并设置一个标志,以便我可以调用 bodyToUpdate.applyForce(...);
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?
所以我现在让物体直接进入漩涡的中心,但没有旋转"
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;
}
推荐答案
你想实现一个切向力,其大小向涡流中心增加.
You want to implement a tangential force with a magnitude that increases towards the center of the vortex.
这是一些伪代码.
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:
要创建漩涡效果,当物体靠近中心时,应增加径向 (Fr) 和切向 (Ft) 力.
To create a whirlpool effect there should be increasing radial (Fr) and tangential (Ft) forces as the object moves closer to the center.
相关文章