首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java在2d中的移动

Java在2d中的移动
EN

Stack Overflow用户
提问于 2011-04-18 23:40:14
回答 1查看 1.3K关注 0票数 2

我以前在2d项目中使用过这样的东西来移动,它总是有效的。我现在正在使用它,它为我提供了一些输出的正确角度,以及错误的角度。我想只是我的trig中有一些错误,我弄错了。我已经检查了大约一百万次了。很可能只是自动纠正我脑海中的错误。以下是一些示例输出和错误的代码。

代码语言:javascript
运行
复制
this(x,y): (500.0, 250.0)
dest(x,y): (400.0, 300.0)
DeltX: 100.0
DeltY: -50.0
Angle: 0.46364760900080615 //about 26.56°, should be 206.56° (26.56+pi) i think
XVELOC: 0.08944272
YVELOC: 0.04472136

public void Update(long deltaTime){
        if(this.destination == null){
            return;
        }

        this.x += this.x_velocity * deltaTime;
        this.y += this.y_velocity * deltaTime;

        if(Math.abs(this.x-destination.x) < 1 && Math.abs(this.y-destination.y) < 1){
            destination.arrive(this);
        }
    }

    public void setPath(){
        if(this.destination == null){
            return;
        } 

        double delta_x = (double) (this.x-this.destination.x);
        double delta_y = (double) (this.y-this.destination.y);
        int sign_x = (int)Math.signum(delta_x);
        int sign_y = (int)Math.signum(delta_y);
        double radian_angle = Math.atan((delta_x/delta_y));
        if(sign_x > 0 && sign_y > 0)
            radian_angle += Math.PI;
        else if(sign_x > 0 && sign_y < 0)
            radian_angle += Math.PI/2;
        else if(sign_x < 0 && sign_y > 0)
            radian_angle += 3*Math.PI/2;

        System.out.println("DeltX: "+delta_x);
        System.out.println("DeltY: "+delta_y);
        System.out.println("Angle: "+radian_angle);

        this.x_velocity = this.max_velocity*(float)Math.cos(radian_angle);
        this.y_velocity = this.max_velocity*(float)Math.sin(radian_angle);

        System.out.println("XVELOC: "+x_velocity);
        System.out.println("YVELOC: "+y_velocity);
    }
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5705397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档