I'll look into that and see if my understanding of the function is correct. Thanks again for all your help. Im' about 100x closer to get this working than I was before.
Rotating an image to face a point
Ok, I was finally able to take a look at this again and I was able to figure it out.
package com.turretgame.mainpackage;
import java.util.List;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.Log;
import com.example.gamedevchapter3.Input.TouchEvent;
import com.example.gamedevchapter3.Pixmap;
import com.example.gamedevchapter3.Graphics;
public class Turret {
Pixmap turretImage;
Matrix matrix;
final int SHIP_X_POS = 360;
final int SHIP_Y_POS = 1040;
final int SHIP_X_ROTATEPOS = 360 + 15;
final int SHIP_Y_ROTATEPOS = 1040 + 20;
final int TOUCH_X_TRANSLATION = 360;
final int TOUCH_Y_TRANSLATION = 1040;
float direction, tempDirection = 0;
int xpos, ypos = 100;
float eventSlope = 0;
int screenWidth, screenHeight;
float shipPosition[] = {5.0f, 7.0f};
float shipRotation[] = {0.0f, 0.0f, 0.0f, 1.0f};
float touchPosition[] = {0.0f, -5.0f, 0.0f};
float blendedRotation = 0.0f;
float startRotation = 0.0f;
float targetRotation = 180.0f;
float sliderTime = 0.0f;
float incrementTime = 0.0f;
float distanceBetweenModels[] = {0.0f, 0.0f, 0.0f};
TouchEvent event;
public Turret(Pixmap image, int x, int y, int worldWidth, int worldHeight) {
turretImage = image;
matrix = new Matrix();
xpos = 360;
ypos = 1040;
matrix.postTranslate(360, 1040);
}
public void update(List <TouchEvent> events, float deltaTime) {
for (int i = 0; i < events.size(); i++){
event = events.get(i);
//Log.d("Touch Event Coords", "X value: " + Float.toString(event.x) + "\t Y value: " + Float.toString(event.y));
matrix.reset();
matrix.postTranslate(SHIP_X_POS, SHIP_Y_POS);
////////////////////////////////////////////////////////////
//this line used to be event.y + SHIP_Y_POS
touchPosition[1] = SHIP_Y_POS - event.y;
touchPosition[0] = event.x - TOUCH_X_TRANSLATION;
/////////////////////////////////////////////////////////////
//============================= - The next seven lines of code seems to have round 1 taken care of.
incrementTime += 0.001;
//--------------------------------
distanceBetweenModels[1] = touchPosition[1] + shipPosition[1];
distanceBetweenModels[0] = touchPosition[0] - shipPosition[0];
//------------------------------------------------------------------------------------------------------
targetRotation = (float) ((float)Math.atan2(distanceBetweenModels[0], distanceBetweenModels[1])* 180 / Math.PI);
blendedRotation = (float) ((startRotation * (1.0 - sliderTime)) + (targetRotation * sliderTime));
matrix.postRotate(blendedRotation, SHIP_X_ROTATEPOS, SHIP_Y_ROTATEPOS);
if(sliderTime <= 1.0) // This will prevent the rotation from over-shooting
{
sliderTime += incrementTime;
}
}
}
public void draw(Graphics g) {
g.drawPixmap(turretImage, matrix);
//g.drawLine((int)SHIP_X_ROTATEPOS, (int)SHIP_Y_ROTATEPOS, (int)touchPosition[0] + TOUCH_X_TRANSLATION, (int)touchPosition[1], Color.WHITE);
g.drawPixel(250, 250, Color.WHITE);
}
}
The math I was using to translate the touchEvent was backwards. Once I got the coordinates on some graph paper I was able to see it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement