How to create controls
How do I make a joystick control?
Game Attributes Needed
- BaseX - The X position of the base actor. The base actor should have a Change Attribute behavior to modify this to its own X position.
- BaseY - The Y position of the base actor. The base actor should have a Change Attribute behavior to modify this to its own Y position.
- BaseRadius - The Radius of the base actor. The base actor should have a Change Attribute behavior to modify this to half of its width(if a perfect square), or half of the smaller of its width or height. min(self.Size.Width, self.Size.Height)/2
- Angle - The angle the control actor is relative to the base. You can use this in movement behaviors for the actor you are controlling.
- DistanceFromCenter - The distance from the center the control actor is from the base actor. You can use this in movement behaviors for the actor you are controlling.
First you want to detect if you are touching the thumb button. When you have the actor is touched (pressed), set an attribute saying that it is held. In the case above, it sets the SnapTo attribute to false. Otherwise, when you are not pressing the button, SnapTo is true.
If the SnapTo attribute is true, the button returns to its base position. In the image above, the base position is defined by game attributes that are given values by a base actor. (game.BaseX and game.BaseY) Otherwise, if SnapTo is false it needs to follow your mouse position.
Here you will need some math. And you will need to keep this button within the boundaries of the base radius, which is also defined by the base in the form of a game attribute, game.BaseRadius
Position.X
Constrain the value of this attribute to the following equation.
game.BaseX + min(game.BaseRadius , magnitude(game.Mouse.Position.X - game.BaseX , game.Mouse.Position.Y - game.BaseY))*cos(vectorToAngle(game.Mouse.Position.X - game.BaseX , game.Mouse.Position.Y - game.BaseY))
Position.Y
Constrain the value of this attribute to the following equation.
game.BaseY + min(game.BaseRadius , magnitude(game.Mouse.Position.X - game.BaseX , game.Mouse.Position.Y - game.BaseY))*sin(vectorToAngle(game.Mouse.Position.X - game.BaseX , game.Mouse.Position.Y - game.BaseY))
What to do now
Now that you have your controls working, to be able to use this you just need to use the data of this actor for use in other actors. i.e. the angle and the distance from the center.
You can get the ANGLE from the center of the thumbstick base using: vectorToAngle(self.Position.X - game.BaseX , self.Position.Y - game.BaseY)
You can get the DISTANCE from the center of the thumbstick base using: magnitude(self.Position.X - game.BaseX ,self.Position.Y - game.BaseY))
With the distance you can even control speed of something by dividing the distance by the baseRadius, which will give you a real number between 0 and 1.
Dual Thumbstick Controls
(Details to come later. Download the project above to see how it is done.)

