简体   繁体   English

以周期性圆形路径围绕点移动对象

[英]Moving an object in a periodical circular path around a point

I'm working on a game for a game jam right now, and the problem is related to the flight path of a certain game enemy.我现在正在做一个game jam的游戏,问题与某个游戏敌人的飞行路径有关。 I'm trying to have several of them fly in formation, and the idea was to have them fly in a wide radius circle around the center of the screen so they essentially box in the player.我试图让它们中的几个在编队中飞行,这个想法是让它们在屏幕中心周围的大半径圆内飞行,这样它们基本上就在播放器中。 To do this, I tried to use the following formula...为此,我尝试使用以下公式...

    public Pair<Double> move(Pair<Double> currPos, Pair<Double> playerPos) {
        Pair<Double> newPos = new Pair<>(currPos.x, currPos.y);

        // The radius used as the distance from the center.
        double r = (Framework.CANVAS_WIDTH / 2) - Player.SHIP_SIZE;
        // X,Y coords for center of the screen.
        double cX = (Framework.CANVAS_WIDTH / 2);
        double cY = (Framework.CANVAS_HEIGHT / 2);
        // Trigonometric equation for transforming the object in a circle.
        newPos.x = cX + (r * Math.cos(Framework.getHypotenuse(currPos, new Pair<Double>(cX, cY)) + (Math.PI / 90)));
        newPos.y = cY + (r * Math.sin(Framework.getHypotenuse(currPos, new Pair<Double>(cX, cY)) + (Math.PI / 90)));

        return newPos;
    }

I can't figure out why the equation doesn't seem to work.我不明白为什么这个方程似乎不起作用。 When I test the movement pattern, there seem to be two enemies on screen rotating around the center, even though I only spawned one.当我测试移动模式时,屏幕上似乎有两个敌人围绕中心旋转,尽管我只生成了一个。 However, they're blinking really fast, which makes it seem like maybe the ship is jumping back and forth really fast.但是,它们的闪烁速度非常快,这使得船似乎来回跳跃的速度非常快。 This is supported by the fact that when I took a screenshot, there was only one ship.这得到了以下事实的支持:当我截取屏幕截图时,只有一艘船。 Is there something wrong with my trigonometry that would cause this, or does the problem lie elsewhere?我的三角函数有什么问题会导致这种情况,还是问题出在其他地方?

The following pseudocode gives the standard way to make an object move in a circular path:以下伪代码给出了使对象沿圆形路径移动的标准方法:

double r  = (...);  // Radius of circle
double cX = (...);  // x-coordinate of center of rotation
double cY = (...);  // y-coordinate of center of rotation

double omega = (...);  // Angular velocity, like 1
double t = (...);  // Time step, like 0.00, 0.01, 0.02, 0.03, etc.

newPos.x = cX + r * Math.cos(t * omega);
newPos.y = cY + r * Math.sin(t * omega);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM