Hey everyone I have this game that i am making in android and I have a touch screen joystick that moves the player around based on the joysticks position. I cant figure out how to also get the player to rotate at the same angle of the joystick. so when the joystick is to the left the players bitmap is rotated to the left as well. Maybe someone here has some sample code I could look at here is the joysticks class that I am using. `public class GameControls implements OnTouchListener {
public float initx = DroidzActivity.screenWidth - 45; //255; // 320 og 425
public float inity = DroidzActivity.screenHeight - 45;//425; // 480 og 267
public Point _touchingPoint = new Point( DroidzActivity.screenWidth - 45, DroidzActivity.screenHeight - 45);
public Point _pointerPosition = new Point(DroidzActivity.screenWidth - 100, DroidzActivity.screenHeight - 100); // ogx 220 ogy 150
private Boolean _dragging = false;
private boolean attackMode = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
update(event);
return true;
}
private MotionEvent lastEvent;
public boolean ControlDragged;
private static double angle;
public void update(MotionEvent event) {
if (event == null && lastEvent == null) {
return;
} else if (event == null && lastEvent != null) {
event = lastEvent;
} else {
lastEvent = event;
}
// drag drop
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if ((int) event.getX() > 0 && (int) event.getX() < 50
&& (int) event.getY() > DroidzActivity.screenHeight - 160
&& (int) event.getY() < DroidzActivity.screenHeight - 0) {
setAttackMode(true);
} else {
_dragging = true;
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if(isAttackMode()){
setAttackMode(false);
}
_dragging = false;
}
if (_dragging) {
ControlDragged = true;
// get the pos
_touchingPoint.x = (int) event.getX();
_touchingPoint.y = (int) event.getY();
// Log.d("GameControls", "x = " + _touchingPoint.x + " y = "
//+ _touchingPoint.y);
// bound to a box
if (_touchingPoint.x < DroidzActivity.screenWidth - 75) { // og 400
_touchingPoint.x = DroidzActivity.screenWidth - 75;
}
if (_touchingPoint.x > DroidzActivity.screenWidth - 15) {// og 450
_touchingPoint.x = DroidzActivity.screenWidth - 15;
}
if (_touchingPoint.y < DroidzActivity.screenHeight - 75) {// og 240
_touchingPoint.y = DroidzActivity.screenHeight - 75;
}
if (_touchingPoint.y > DroidzActivity.screenHeight - 15) {// og 290
_touchingPoint.y = DroidzActivity.screenHeight - 15;
}
// get the angle
setAngle(Math.atan2(_touchingPoint.y - inity,
_touchingPoint.x - initx) / (Math.PI / 180));
// Move the ninja in proportion to how far
// the joystick is dragged from its center
_pointerPosition.y += Math.sin(getAngle() * (Math.PI / 180))
* (_touchingPoint.x / 70); // og 180 70
_pointerPosition.x += Math.cos(getAngle() * (Math.PI / 180))
* (_touchingPoint.x / 70);
// make the pointer go thru
if (_pointerPosition.x > DroidzActivity.screenWidth) {
_pointerPosition.x = 0;
}
if (_pointerPosition.x < 0) {
_pointerPosition.x = DroidzActivity.screenWidth;
}
if (_pointerPosition.y > DroidzActivity.screenHeight) {
_pointerPosition.y = 0;
}
if (_pointerPosition.y < 0) {
_pointerPosition.y = DroidzActivity.screenHeight;
}
} else if (!_dragging) {
ControlDragged = false;
// Snap back to center when the joystick is released
_touchingPoint.x = (int) initx;
_touchingPoint.y = (int) inity;
// shaft.alpha = 0;
}
}
public void setAttackMode(boolean attackMode) {
this.attackMode = attackMode;
}
public boolean isAttackMode() {
return attackMode;
}
public void setAngle(double angle) {
this.angle = angle;
}
public static double getAngle() {
return angle;
}
}` I should also note that the player has animations based on when he is moving or attacking.
EDIT:
I got the angle and am rotating the sprite around in the correct angle however it rotates on the wrong spot. My sprite is one giant bitmap that gets cut into four pieces and only one shown at a time to animate walking. here is the code I am using to rotate him right now. ` public void draw(Canvas canvas,int pointerX, int pointerY) {
Matrix m;
if (setRotation){
// canvas.save();
m = new Matrix();
m.reset();
// spriteWidth and spriteHeight are for just the current frame showed
//m.setTranslate(spriteWidth / 2, spriteHeight / 2);
//get and set rotation for ninja based off of joystick
m.preRotate((float) GameControls.getRotation());
//create the rotated bitmap
flipedSprite = Bitmap.createBitmap(bitmap , 0, 0,bitmap.getWidth(),bitmap.getHeight() , m, true);
//set new bitmap to rotated ninja
setBitmap(flipedSprite);
setRotation = false;
// canvas.restore();
Log.d("Ninja View", "angle of rotation= " +(float) GameControls.getRotation());
}
`
And then the draw method
// create the destination rectangle for the ninjas current animation frame
// pointerX and pointerY are from the joystick moving the ninja around
destRect = new Rect(pointerX, pointerY, pointerX + spriteWidth, pointerY + spriteHeight);
canvas.drawBitmap(bitmap, getSourceRect(), destRect, null);