AnalogOnScreenControl bound by radius...

  ... the case you feel the need for a new feature or want to submit one.

AnalogOnScreenControl bound by radius...

Postby AngrySpade » Wed Sep 29, 2010 3:19 am

Hello all,
I just got into this engine and I'm playing with it, trying to learn more about game development.

I realized that the Analog Stick allowed you to make a square around the base.
Thus being able to get a further distance from the center in the corners than in the middles.
So I set out to find a way to rectify that.

I liked my result so I figured I would share.

Here is a video of the modified version of the Analog Stick Example using the control.
And the code of the class.

Thanks for the engine!
-Stan

http://screencast.com/t/ZmViOTRkZG

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class RadiusBoundAnalogOnScreenControl extends AnalogOnScreenControl {
  2.  
  3.     private float maxDistance = 0.5f;
  4.     private float minDistance = 0.3f;
  5.  
  6.     public RadiusBoundAnalogOnScreenControl(int pX, int pY, Camera pCamera, TextureRegion pControlBaseTextureRegion, TextureRegion pControlKnobTextureRegion, float pTimeBetweenUpdates, IAnalogOnScreenControlListener pAnalogOnScreenControlListener) {
  7.         super(pX, pY, pCamera, pControlBaseTextureRegion, pControlKnobTextureRegion, pTimeBetweenUpdates, pAnalogOnScreenControlListener);
  8.     }
  9.  
  10.     public RadiusBoundAnalogOnScreenControl(int pX, int pY, Camera pCamera, TextureRegion pControlBaseTextureRegion, TextureRegion pControlKnobTextureRegion, float pTimeBetweenUpdates, long pOnControlClickMaximumMilliseconds, IAnalogOnScreenControlListener pAnalogOnScreenControlListener) {
  11.         super(pX, pY, pCamera, pControlBaseTextureRegion, pControlKnobTextureRegion, pTimeBetweenUpdates, pOnControlClickMaximumMilliseconds, pAnalogOnScreenControlListener);
  12.     }
  13.  
  14.     @Override
  15.     protected void onUpdateControlKnob(float pRelativeX, float pRelativeY) {    
  16.         float x = pRelativeX;
  17.         float y = pRelativeY;
  18.        
  19.         if ((pRelativeX != 0) && (pRelativeY != 0)) {
  20.             float hypotenuse = FloatMath.sqrt((pRelativeX * pRelativeX) + (pRelativeY * pRelativeY));
  21.        
  22.             if (hypotenuse > this.maxDistance) {
  23.                 float absoluteX = Math.abs(pRelativeX);
  24.                 float absoluteY = Math.abs(pRelativeY);
  25.                 float rad = (float) Math.atan(absoluteY / absoluteX);
  26.  
  27.                 x = (float) (Math.cos(rad) * this.maxDistance) * ((pRelativeX < 0) ? -1 : 1);
  28.                 y = (float) (Math.sin(rad) * this.maxDistance) * ((pRelativeY < 0) ? -1 : 1);
  29.             }
  30.             else if (hypotenuse < this.minDistance) {
  31.                 float absoluteX = Math.abs(pRelativeX);
  32.                 float absoluteY = Math.abs(pRelativeY);
  33.                 float rad = (float) Math.atan(absoluteY / absoluteX);
  34.                
  35.                 x = (float) (Math.cos(rad) * this.minDistance) * ((pRelativeX < 0) ? -1 : 1);
  36.                 y = (float) (Math.sin(rad) * this.minDistance) * ((pRelativeY < 0) ? -1 : 1);
  37.             }
  38.         }    
  39.        
  40.         super.onUpdateControlKnob(x, y);
  41.     }
  42.  
  43.     public float getMaxDistance() {
  44.         return maxDistance;
  45.     }
  46.  
  47.     public void setMaxDistance(float maxDistance) {
  48.         this.maxDistance = maxDistance;
  49.     }
  50.  
  51. }
  52.  
Parsed in 0.013 seconds, using GeSHi 1.0.8.4
AngrySpade
 
Posts: 1
Joined: Wed Sep 29, 2010 3:05 am

Re: AnalogOnScreenControl bound by radius...

Postby oldskool73 » Wed Dec 01, 2010 4:47 pm

very handy, thanks
oldskool73
 
Posts: 128
Joined: Tue Oct 12, 2010 4:04 pm

Re: AnalogOnScreenControl bound by radius...

Postby nebkat » Sat Jan 15, 2011 10:49 pm

This should be implemented into the source ;)
nebkat
 
Posts: 23
Joined: Thu Jan 06, 2011 5:46 pm

Re: AnalogOnScreenControl bound by radius...

Postby Headset » Mon Feb 07, 2011 10:16 pm

very nice, thank you
Headset
 
Posts: 3
Joined: Mon Feb 07, 2011 10:08 pm


Return to Features

Who is online

Users browsing this forum: Google [Bot] and 6 guests