Jump to content

Javascript virtual joystick for touchscreen devices


LudovicCluber
 Share

Recommended Posts

Hi there!

Just wanted to show a touchscreen version of my first game here : http://vortalcombat.roostrjs.com/mobile/

 

I made a pretty simple Javascript virtual joystick for this. the joystick seems to work well on android, apple and Windows devices. Also works with a mouse.

 

You can change the size of the joystick in the options to fit your screen size.

 

I would be pleased to know if it works fine on your mobile devices.

 

Please let me know if there are bugs. Or if you have interesting ideas to improve the controls.

 

 

 

 

FeatureGraphic.png

 

 

Link to comment
Share on other sites

  • 8 months later...
function Joystick(){ //This class simulates an analog joystick for touchscreen devices    this.position=new vect(.0,.0); //position of the whole joystick    this.stickPosition=new vect(.0,.0); //position of the stick    this.delta=new vect(.0,.0); //vector from joystick to finger    //stick contour    this.size=new vect(imgJoystick.spriteSize.X*this.stickSize[OPTION.stickSize],imgJoystick.spriteSize.Y*this.stickSize[OPTION.stickSize]);    this.halfSize=new vect(this.size.X>>1,this.size.Y>>1);    this.squareRad=this.halfSize.X*this.halfSize.X; //compared with square magnitude of delta in setPosition() to determine what to do    this.stickContour=new sprite(this.position,0,0,imgJoystick,this.size,this.halfSize,0);        //stick    this.sSize=new vect(imgJoystick2.spriteSize.X*this.stickSize[OPTION.stickSize],imgJoystick2.spriteSize.Y*this.stickSize[OPTION.stickSize]);    this.sHalfSize=new vect(this.sSize.X>>1,this.sSize.Y>>1);    this.stick=new sprite(this.stickPosition,0,0,imgJoystick2,this.sSize,this.sHalfSize,0);        this.life=0;        this.margin=new Margin(-this.halfSize.X,-this.halfSize.Y);}Joystick.prototype={    draw:function(){        if(SCREEN.touched){ //draw joystick if TOUCH_PRESSED            this.stickContour.draw();            this.stick.draw();        }    },    setPosition:function(fingerPosition){ //TOUCH_MOVED            this.computeDelta(fingerPosition); //compute the vector from finger to joystick                         var sMag = this.delta.squareMagnitude(); //square magnitude of delta            if(sMag >= this.squareRad){ //joystick moves if the finger is out of the joystick contour                this.stickPosition.initV(fingerPosition); //stick moves under the finger                                this.delta.normalize(); //delta is now a direction                this.delta.scale(this.halfSize.X,2); //delta is now the stick position relative to the whole joystick                                this.position.initV(fingerPosition); //joystick contour moves under the finger                this.position.subtract(this.delta); //joystick contour is moved in order to keep stick direction                this.clamp();// the joystick contour can't go out of the screen                        }else //finger is inside the joystick contour. We move the stick only                this.stickPosition.initV(fingerPosition); //stick is moved under the finger                            this.computeDirection(); //gives the stick direction to the space ship    },    computeDelta:function(position){ //compute the vector from finger to joystick         this.delta.initV(position);        this.delta.subtract(this.position);    },    computeDirection:function(){ //sends the joystick direction to the space ship        SHIP.direction.initV(this.delta);        SHIP.direction.scale(1/this.halfSize.X,2); //normalize D    },    clamp:function(){ //joystick contour can't go out of screen        if(this.position.Y<this.margin.top)this.position.Y=this.margin.top;        if(this.position.X>this.margin.right)this.position.X=this.margin.right;        if(this.position.Y>this.margin.bottom)this.position.Y=this.margin.bottom;        if(this.position.X<this.margin.left)this.position.X=this.margin.left;    },    release:function(){ //TOUCH_RELEASED        this.stickPosition.initV(this.position);        SHIP.direction.init(.0,.0);        },    init:function(fingerPosition){ //TOUCH_PRESSED        this.position.initV(fingerPposition);        this.stickPosition.initV(fingerPosition);        this.clamp();        this.computeDelta(fingerPosition);        this.computeDirection();    }};

Here is the joystick class. Code is commented and simplified in order to keep the interesting things only.

Could be improved but it's working fine as it is.

Now you need to figure out how to detect touch events with javascript. I won't give the code here since you will find it easily on Google.

 

If something is not clear enough just ask me.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...