Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You can use the atan2 function to get the rotation for your sprite. Just plug in the x and y offsets of your joystick (from the center of the joystick) and you'll get the angle in radians. The rotation will start at the 3 o'clock position, so if your sprite is normally pointing upwards (12 o'clock), you'll have to subtract 90 degree (or π/2 radians).

share|improve this answer
    
SO something like this??? Math.atan2(x - joystickCenter, y - joystickCenter) - 90; –  pengume Mar 7 '11 at 9:11
    
Almost. Since the result from Math.atan2 is in radians, you'll have to convert it to degrees first. Something like this: Math.atan2(y - joystickCenter.y, x - joystickCenter.x) * (180/Math.PI) - 90;. Also note that y is usually the first parameter for the atan2 function. Maybe check the documentation or code-help. –  bummzack Mar 7 '11 at 9:28
    
yea i tried to edit that but was too late. Thanks for your help. I feel like I am understanding this more. –  pengume Mar 7 '11 at 9:34
    
Awesome, would've never figured this out on my own haha! –  Chen Asraf Jul 5 '12 at 11:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.