简体   繁体   English

在OpenGL ES中正确转换位置

[英]Correct position translation in OpenGL ES

I'm attempting to move a simple OpenGL Triangle rendered in android to the touch location. 我正在尝试将在Android中渲染的简单OpenGL三角形移动到触摸位置。

I can only seem to get the triangle moving along in a grid sort of fashion. 我似乎只能使三角形以网格的方式移动。 This is because despite my best efforts I am increasing X and Y coordinates by the same amount. 这是因为尽管我尽了最大的努力,但我仍在将X和Y坐标增加相同的数量。 I've attempted to use a sort of normalised value to alter the rate of movement so that either the X or Y change depending on which (X or Y) is closer to the target location, however this was to no avail. 我试图使用一种归一化的值来更改移动速率,以使X或Y取决于哪个(X或Y)更接近目标位置而改变,但是这没有用。 I'm wondering what it is I am missing and wondered if anyone can help as I am out of ideas as to how to get this to work. 我想知道我想念的是什么,想知道是否有人可以帮忙,因为我对如何使它起作用没有任何想法。

PointF vector = new PointF();

vector.x = destination.getX() - origin.getX();
vector.y = destination.getY() - origin.getY();

float length = (float)Math.sqrt(vector.x * vector.x + vector.y * vector.y);

// Normalize
vector.x /= length;
vector.y /= length;

// when you move your rectangle:

PointF newPosition = new PointF();
newPosition.x = currentPosition.x + (millisecSinceLastAnimationTick / 1000f) * amountToMovePerSecond * vector.x;
newPosition.y = currentPosition.y + (millisecSinceLastAnimationTick / 1000f) * amountToMovePerSecond * vector.y;

glTranslate(newPosition.x, newPosition.y, 0);

currentPosition.set(newPosition);

This should get you started. 这应该使您入门。

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

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