下面的自定义路径对象 [英] Object following custom path

查看:199
本文介绍了下面的自定义路径对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的2D游戏开发项目。
这是某种形式的棋盘游戏。

I'm working on 2d game dev project. It's some kind of board game.

为背景我有棋盘游戏的道路,那不是真正的圆,但某种波浪线。
对象正在移动跨路径。

As background I have board game with path, that isn't real circle, but some kind of wavy line. Objects are moving across that path.

在触摸事件...我需要我的球员的对象获得该确切位置,但他们需要走这条道路。

On touch event...I need to my player's object get at that exact position, but they need to follow that path.

如何翻译(画)对象对一些自定义路径?

How to translate (draw) objects over some custom path?

推荐答案

我想出了这个简单的解决方案,我已经剪掉祈祷,注重性能,职高我需要保持它的简单。

I've came up with this simple solution, i've didnt payed attention to performance, 'couse i need to keep it simple.

我做了类,MapPath的是类的实例。
我已经拍了X和Y坐标从的TouchEvent并给他们作为一个参数,我的功能。

I've made class with Path and PathMeasure objects, mapPath is instance of that class. I've took x and y coordinates from touchEvent and gave them as a parameters to my function.

我用简单的函数两个向量,我的道路上像素之间计算距离和矢量从我得到的TouchEvent

I use simple function to calculate distance between two vectors, my pixels on the path, and vector I got from touchEvent.

迭代,直到我让他们之间的距离最小。

Iterate until i get minimum of distance between them.

当我得到x和我的道路,在那里我需要把我的对象的y坐标,我简单的,协调翻译对象。

When i get x and y coordinate of my path, where i need to place my object, I simple translate object to that coordinates.

它这么简单,但它有点脏:)希望有人能有一天用它,在需要的时刻。干杯;)

Its that simple, but its little dirty :) Hope someone can use it someday, in hour of need. Cheers ;)

//Find point on path closest to touch coordinates    

public float[] findMinDistanceVector(int x, int y) {

 float[] xy = new float[2];
 float[] ten = new float[2];

 float distanceVectorOld = Float.MAX_VALUE;
 float distanceVectorNew = 0;
 float[] minXY = new float[2];
 for (float distance = 0; distance < mapPath.pathMeasure.getLength();distance++) {

 mapPath.pathMeasure.getPosTan(distance, xy, ten);

 distanceVectorNew = dist(x, y, xy[0], xy[1]);

      if (distanceVectorNew < distanceVectorOld) {

          minXY[0] = xy[0];
          minXY[1] = xy[1];
          distanceVectorOld = distanceVectorNew;

      }
 }

return minXY;
}

public float dist(float x1, float y1, float x2, float y2) {

    float distX = x1 - x2;
    float distY = y1 - y2;

    // Pythagora's theorem
    return FloatMath.sqrt(distX * distX + distY * distY);
}

这篇关于下面的自定义路径对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆