Slick Forums

Discuss the Slick 2D Library
It is currently Sat Feb 23, 2019 1:01 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Onscreen Analog Stick
PostPosted: Sun Dec 04, 2011 9:55 pm 
Offline

Joined: Tue Aug 16, 2011 2:17 am
Posts: 31
Location: Phoenix
Does anyone know how to make the onscreen analog sticks for Android? I need one to control my character but cant figure out how to make it. Thanks in advance!


They are commomly seen in emulators and also on the Iphone in the Nazi Zombie's app, cant think of any other examples.

I have an idea on how to make it but im using MarteEngine and i can't get the gui to draw freely without moving with the camera.. any ideas (psuedo code will work, or text) on how to make this?


Top
 Profile  
 
PostPosted: Wed Dec 07, 2011 5:05 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
I'm unfamiliar with the Marte engine, but using only slick code, I have been able to track the location of two simultaneous touches by having a Vector2f variable for each. This Vector2f is initialized when a mousePressed event is called, and set to that location. When mouseMoved is called, the Vector2f that matches the old position is moved to the new position. When mouseReleased is called, the Vector2f matching that location is set to null, indicating that there is no touch for this vector. From there you can have a circular area that counts as a control stick, and if a touch is within that area, you can compute it's distance from the center and use that as your input. Is this what you needed?

code from gearbie:
Code:
public class PlayState extends BasicGameState...{
        ...
        ...
   /**track fingers*/
   Vector2f touchA=null;
   Vector2f touchB=null;
        ...
        ...
   @Override
   public void enter(GameContainer container, StateBasedGame game)
         throws SlickException {
                ...
                ...
      touchA=null;
      touchB=null;
   }
        ...
        ...
   @Override
   public void mousePressed(int button, int x, int y) {
      if (touchA==null){
         touchA=new Vector2f(x,y);
      }else if (touchB==null){
         touchB=new Vector2f(x,y);
      }else{
                        //if you get a third touch
                        //assume something is wrong and reset touches
         touchA=null;
         touchB=null;
      }
   }

   @Override
   public void mouseReleased(int button, int x, int y) {
      if (touchA!=null&&touchA.distanceSquared(new Vector2f(x,y))<9){
         touchA=null;
      }else if (touchB!=null&&touchB.distanceSquared(new Vector2f(x,y))<9){
         touchB=null;
      }
   }
   
   @Override
   public void mouseDragged(int oldx, int oldy, int newx, int newy) {
                //if oldx,oldy is within three pixels of touchA,
                //assume touchA is the one moving (same for touchB)
      if (touchA!=null&&touchA.distanceSquared(new Vector2f(oldx,oldy))<9){
         touchA=new Vector2f(newx,newy);
      }else if (touchB!=null&&touchB.distanceSquared(new Vector2f(oldx,oldy))<9){
         touchB=new Vector2f(newx,newy);
      }
   }

_________________
"Artificial intelligence will never be a match for human stupidity" - "Jamos Kennedynos"


Top
 Profile  
 
PostPosted: Wed Dec 07, 2011 8:35 pm 
Offline

Joined: Tue Aug 16, 2011 2:17 am
Posts: 31
Location: Phoenix
Thanks for this, it helps a little.. but im just going to go with a simple 8 direction d-pad instead to save time. This code wont work with my MarteEngine project because MouseMoved doesnt return an old x and y
Mr. Kenkron wrote:
I'm unfamiliar with the Marte engine, but using only slick code, I have been able to track the location of two simultaneous touches by having a Vector2f variable for each. This Vector2f is initialized when a mousePressed event is called, and set to that location. When mouseMoved is called, the Vector2f that matches the old position is moved to the new position. When mouseReleased is called, the Vector2f matching that location is set to null, indicating that there is no touch for this vector. From there you can have a circular area that counts as a control stick, and if a touch is within that area, you can compute it's distance from the center and use that as your input. Is this what you needed?

code from gearbie:
Code:
public class PlayState extends BasicGameState...{
        ...
        ...
   /**track fingers*/
   Vector2f touchA=null;
   Vector2f touchB=null;
        ...
        ...
   @Override
   public void enter(GameContainer container, StateBasedGame game)
         throws SlickException {
                ...
                ...
      touchA=null;
      touchB=null;
   }
        ...
        ...
   @Override
   public void mousePressed(int button, int x, int y) {
      if (touchA==null){
         touchA=new Vector2f(x,y);
      }else if (touchB==null){
         touchB=new Vector2f(x,y);
      }else{
                        //if you get a third touch
                        //assume something is wrong and reset touches
         touchA=null;
         touchB=null;
      }
   }

   @Override
   public void mouseReleased(int button, int x, int y) {
      if (touchA!=null&&touchA.distanceSquared(new Vector2f(x,y))<9){
         touchA=null;
      }else if (touchB!=null&&touchB.distanceSquared(new Vector2f(x,y))<9){
         touchB=null;
      }
   }
   
   @Override
   public void mouseDragged(int oldx, int oldy, int newx, int newy) {
                //if oldx,oldy is within three pixels of touchA,
                //assume touchA is the one moving (same for touchB)
      if (touchA!=null&&touchA.distanceSquared(new Vector2f(oldx,oldy))<9){
         touchA=new Vector2f(newx,newy);
      }else if (touchB!=null&&touchB.distanceSquared(new Vector2f(oldx,oldy))<9){
         touchB=new Vector2f(newx,newy);
      }
   }


Top
 Profile  
 
PostPosted: Tue Dec 13, 2011 7:15 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
let me know how you implement it. I haven't been able to think of a good way of monitoring touches other then what I described above.

_________________
"Artificial intelligence will never be a match for human stupidity" - "Jamos Kennedynos"


Top
 Profile  
 
PostPosted: Wed Dec 14, 2011 1:44 am 
Offline

Joined: Tue Aug 16, 2011 2:17 am
Posts: 31
Location: Phoenix
I'm actually currently looking at higher game engines for Android, AndEngine specifically. It's very complicated compared to the simplicity of Slick2d or MarteEngine, but I'm sticking with it.


Top
 Profile  
 
PostPosted: Wed Dec 14, 2011 7:20 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
If I may, don't rule out libGDX. The ability to test on a desktop environment may come in hand.

_________________
"Artificial intelligence will never be a match for human stupidity" - "Jamos Kennedynos"


Top
 Profile  
 
PostPosted: Fri Dec 16, 2011 5:49 am 
Offline

Joined: Tue Aug 16, 2011 2:17 am
Posts: 31
Location: Phoenix
I kind of stopped with both of those and went back to Slick/Marte Engine because i found those others (AndEngine/LibGDX) too complicated to learn.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group