On Sep 12, 2012, at 1:59 PM, iQue <max.nielse...@gmail.com> wrote:

> 
> thanks for taking your time to help me. 
> 
> It ALMOST works now, the stick acts lke imgCenterY and imgCenterX is a bit 
> further down then they really are. I know I'm using the right positions cus 
> if I draw a circle at that position it gets drawn in the exact middle of my 
> joystickBG. 
> 
> this is how I add the stick: canvas.drawBitmap(joystick.get_joystick(), 
> touchingPoint.x - 13, touchingPoint.y - 13,null);
> this is how I add the joystickBG: 
> canvas.drawBitmap(joystick.get_joystickBg(), initx - 50, inity - 50, null);
> 
> and this is the changed code:
> 
>             int imgCenterX = initx + joystick.get_joystickBg().getWidth()/2 - 
> 50;
>             int imgCenterY = inity + joystick.get_joystickBg().getHeight()/2 
> - 50;
>             Point imgCenter = new Point(imgCenterX, imgCenterY);
>             int dx = touchingPoint.x - imgCenter.x;
>             int dy = touchingPoint.y - imgCenter.y;
> 
>             float distance = FloatMath.sqrt(dx * dx + dy * dy);
>             Log.d(TAG, "destance HERE: " + imgCenterX + "   :   " + 
> touchingPoint.x);
> 
> 
>             // Out of bounds check.
>             final float radius = 25.0f;
>             if (distance > radius) { 
>                 touchingPoint.x = imgCenter.x + (int)(dx * radius / distance);
>                 touchingPoint.y = imgCenter.y + (int)(dy * radius / 
> distance); 
>             }   
> 

You could try following (if I understood your coordinate system correctly);

Point imgPos = new Point(initx - 50, inity - 50);
Point imgCenter = new Point(joystick.get__joystickBg().getWidth() / 2, 
joystick.get_joystickBg().getHeight() / 2);
int dx = touchingPoint.x - imgPos.x - imgCenter.x;
int dy = touchingPoint.y - imgPos.y - imgCenter.y;

float distance = FloatMath.sqrt(dx * dx + dy * dy);
Log.d(TAG, "destance HERE: " + imgCenterX + "   :   " + touchingPoint.x);

// Out of bounds check.
final float radius = 25.0f;
if (distance > radius) { 
        touchingPoint.x = imgPos.x + imgCenter.x + (int)(dx * radius / 
distance);
        touchingPoint.y = imgPos.y + imgCenter.y + (int)(dy * radius / 
distance); 
}   

I can't try this out right now but hope that this provides proper distance 
calculations only.

--
H

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to