Rotate triangle so that its tip points in the direction of the point on the screen that we last touched
- by Sid
OpenGL ES - Android.
Hello all, I am unable to rotate the triangle accordingly in such a way that its tip always points to my finger.
What i did : Constructed a triangle in by GL.GL_TRIANGLES. Added touch events to it. I can rotate the triangle along my Z-axis successfully. Even made the vector class for it.
What i need : Each time when I touch the screen, I want to rotate the triangle to face
the touch point.
Need some help.
Here's what i implemented. I wonder that where i am going wrong?
My code :
public class Graphic2DTriangle {
private FloatBuffer vertexBuffer;
private ByteBuffer indexBuffer;
private float[] vertices = {
-1.0f,-1.0f, 0.0f,
2.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f
};
private byte[] indices = { 0, 1, 2 };
public Graphic2DTriangle() {
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder()); // Use native byte order
vertexBuffer = vbb.asFloatBuffer(); // Convert byte buffer to float
vertexBuffer.put(vertices); // Copy data into buffer
vertexBuffer.position(0); // Rewind
// Setup index-array buffer. Indices in byte.
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
My SurfaceView class where i've done some Touch Events.
public class BallThrowGLSurfaceView extends GLSurfaceView{
MySquareRender _renderObj;
View _viewObj;
float oldX,oldY,dX,dY;
final float TOUCH_SCALE_FACTOR = 0.6f;
Vector2 touchPos = new Vector2();
float angle=0;
public BallThrowGLSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
_renderObj = new MySquareRender(context);
this.setRenderer(_renderObj);
this.setRenderMode(RENDERMODE_WHEN_DIRTY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
touchPos.x = event.getX();
touchPos.y = event.getY();
Log.i("Co-ord", touchPos.x+"hh"+touchPos.y);
switch(event.getAction()){
case MotionEvent.ACTION_MOVE :
dX = touchPos.x - oldX;
dY = touchPos.y - oldY;
if(touchPos.y > getHeight()/2){
dX = dX*-1;
}
if(touchPos.x < getWidth()/2){
dY = dY*-1;
}
_renderObj.mAngle += (dX+dY) * TOUCH_SCALE_FACTOR;
requestRender();
Log.i("AngleCo-ord", _renderObj.mAngle +"hh");
}
oldX = touchPos.x;
oldY = touchPos.y;
Log.i("OldCo-ord", oldX+" hh "+oldY);
return true;
}
}
Last but not the least. My vector2 class.
public class Vector2 {
public static float TO_RADIANS = (1 / 180.0f) * (float) Math.PI;
public static float TO_DEGREES = (1 / (float) Math.PI) * 180;
public float x, y;
public Vector2() {
}
public Vector2(float x, float y) {
this.x = x;
this.y = y;
}
public Vector2(Vector2 other) {
this.x = other.x;
this.y = other.y;
}
public Vector2 cpy() {
return new Vector2(x, y);
}
public Vector2 set(float x, float y) {
this.x = x;
this.y = y;
return this;
}
public Vector2 set(Vector2 other) {
this.x = other.x;
this.y = other.y;
return this;
}
public Vector2 add(float x, float y) {
this.x += x;
this.y += y;
return this;
}
public Vector2 add(Vector2 other) {
this.x += other.x;
this.y += other.y;
return this;
}
public Vector2 sub(float x, float y) {
this.x -= x;
this.y -= y;
return this;
}
public Vector2 sub(Vector2 other) {
this.x -= other.x;
this.y -= other.y;
return this;
}
public Vector2 mul(float scalar) {
this.x *= scalar;
this.y *= scalar;
return this;
}
public float len() {
return FloatMath.sqrt(x * x + y * y);
}
public Vector2 nor() {
float len = len();
if (len != 0) {
this.x /= len;
this.y /= len;
}
return this;
}
public float angle() {
float angle = (float) Math.atan2(y, x) * TO_DEGREES;
if (angle < 0)
angle += 360;
return angle;
}
public Vector2 rotate(float angle) {
float rad = angle * TO_RADIANS;
float cos = FloatMath.cos(rad);
float sin = FloatMath.sin(rad);
float newX = this.x * cos - this.y * sin;
float newY = this.x * sin + this.y * cos;
this.x = newX;
this.y = newY;
return this;
}
public float dist(Vector2 other) {
float distX = this.x - other.x;
float distY = this.y - other.y;
return FloatMath.sqrt(distX * distX + distY * distY);
}
public float dist(float x, float y) {
float distX = this.x - x;
float distY = this.y - y;
return FloatMath.sqrt(distX * distX + distY * distY);
}
public float distSquared(Vector2 other) {
float distX = this.x - other.x;
float distY = this.y - other.y;
return distX * distX + distY * distY;
}
public float distSquared(float x, float y) {
float distX = this.x - x;
float distY = this.y - y;
return distX * distX + distY * distY;
}
}
PS : i am able to handle the touch events. I can rotate the triangle with the touch of my finger. But i want that ONE VERTEX of the triangle should point at my finger position respective of the position of my finger.