GTGE 的 Java 数学问题

发布于 2024-10-30 09:28:00 字数 2405 浏览 5 评论 0原文

我有一个 GTGE 游戏,但我在数学方面遇到了一些问题。我不明白为什么它没有按照我想要的方式移动。当你点击某个地方时,精灵就会移动到那里。当您右键单击时,它会计算角度并移动到那里,但它不会停止。我捕获鼠标的位置,每次更新屏幕时,我都会检查精灵是否已到达该位置,但当它到达时,它不会停止。这是我的代码:

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.background.ImageBackground;


public class SpriteTest extends Game {
    //Default Background
    Background  background;
    //Toolkit!
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    //Main Sprite
    Sprite pacman;
    Dimension stop;

    @Override
    public void initResources() {

        //Load the background
        background = new ImageBackground(getImage("nature-wallpapers.jpg"));
        //Load the Sprite
        pacman = new Sprite(getImage("pacman.png"));
        pacman.setActive(false);
        pacman.setBackground(background);


        playMusic("intro.mid");
        showCursor();



    }

    @Override
    public void render(Graphics2D g) {

        //Render background
        background.render(g);
        //Render sprites

        pacman.render(g);




    }

    @Override
    public void update(long elapsedTime) {  
        //Update the background
        background.update(elapsedTime);
        //Update sprites

        if(click()) {


            pacman.setActive(true);
            pacman.setLocation(getMouseX(), getMouseY());


        }



        if(rightClick()) {
            int spriteLocX = (int) pacman.getX();
            int spriteLocY = (int) pacman.getY();
            int mouseX = getMouseX();
            int mouseY = getMouseY();
            pacman.setMovement(1, -(Math.toDegrees(Math.atan2(mouseY - spriteLocY, mouseX - spriteLocX ))));
            stop = new Dimension(mouseX, mouseY);

        }

            if(stop != null) {
                if(pacman.getX() == stop.getWidth()) {
                    if(pacman.getY() == stop.getHeight()){
                        pacman.setMovement(0, 0);
                    }

                }
            }
            pacman.update(elapsedTime);






    }

    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new SpriteTest(), new Dimension(800,600), true);
        game.start();
    }

}

I have a GTGE game and I am having some problems with the math. I don't understand why it isn't moving the way I want it to. When you click somewhere the sprite moves right there. When you right-click it calculates the angle and moves there, but it doesn't stop. I capture the position of the mouse and every time it updates the screen i check to see if the sprite has reached the position, but when it does it doesn't stop. Here is my code:

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.background.ImageBackground;


public class SpriteTest extends Game {
    //Default Background
    Background  background;
    //Toolkit!
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    //Main Sprite
    Sprite pacman;
    Dimension stop;

    @Override
    public void initResources() {

        //Load the background
        background = new ImageBackground(getImage("nature-wallpapers.jpg"));
        //Load the Sprite
        pacman = new Sprite(getImage("pacman.png"));
        pacman.setActive(false);
        pacman.setBackground(background);


        playMusic("intro.mid");
        showCursor();



    }

    @Override
    public void render(Graphics2D g) {

        //Render background
        background.render(g);
        //Render sprites

        pacman.render(g);




    }

    @Override
    public void update(long elapsedTime) {  
        //Update the background
        background.update(elapsedTime);
        //Update sprites

        if(click()) {


            pacman.setActive(true);
            pacman.setLocation(getMouseX(), getMouseY());


        }



        if(rightClick()) {
            int spriteLocX = (int) pacman.getX();
            int spriteLocY = (int) pacman.getY();
            int mouseX = getMouseX();
            int mouseY = getMouseY();
            pacman.setMovement(1, -(Math.toDegrees(Math.atan2(mouseY - spriteLocY, mouseX - spriteLocX ))));
            stop = new Dimension(mouseX, mouseY);

        }

            if(stop != null) {
                if(pacman.getX() == stop.getWidth()) {
                    if(pacman.getY() == stop.getHeight()){
                        pacman.setMovement(0, 0);
                    }

                }
            }
            pacman.update(elapsedTime);






    }

    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new SpriteTest(), new Dimension(800,600), true);
        game.start();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2024-11-06 09:28:00

可能是他移动得太远,所以他从未完全处于“停止”位置。如果他每个时间步移动 10 个单位,您可以假设他在任何给定时间步结束时仅在 10 次尝试中才会处于停止位置。

我会计算当你点击时你必须走的距离。然后,您可以将自被告知移动以来发生的时间步数乘以每次刻度移动的距离。当这个乘法大于原点和目的地之间的毕达哥拉斯距离时,您可以将移动设置为 0,并将位置设置为目的地。 (显然,在渲染之前执行此操作)

It could be that he's moving too far so he's never exactly on the 'stop' position. If he moves let's say 10 units each time-step, you can assume he'll only ever be on the stop position at the end of any given time-step once in 10 attempts.

I would calculate the distance you have to go when you click. Then you can multiply the number of time-steps that have occured since you were told to move times the distance you move each tick. When this multiplcation is greater than the pythagorean distance between your original point and the destination, you can set your movement to 0 and your position to the destination. (Do this before you render, obviously)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文