W
wimaxou
Guest
Hi!
I've just recently started out with GameMaker and was wondering about collisions...
There are many tutorials on typical x and y collisions, but I am looking to implement these with a virtual joystick, and I cannot seem to find any help online.
It would also be nice to have smooth collisions as I am planning on making a top-down game and would like to get the mechanics going before anything else.
If you think you can help, please do take a look at the code I have below for the virtual joystick, and possibly help me if you can
Many thanks!
To clarify: joystickX and Y are the bottom of the joystick whereas joystickControllerX and Y are the bit of the joystick which you actually move.
--CREATE EVENT--
--STEP EVENT--
I've just recently started out with GameMaker and was wondering about collisions...
There are many tutorials on typical x and y collisions, but I am looking to implement these with a virtual joystick, and I cannot seem to find any help online.
It would also be nice to have smooth collisions as I am planning on making a top-down game and would like to get the mechanics going before anything else.
If you think you can help, please do take a look at the code I have below for the virtual joystick, and possibly help me if you can
Many thanks!
To clarify: joystickX and Y are the bottom of the joystick whereas joystickControllerX and Y are the bit of the joystick which you actually move.
--CREATE EVENT--
Code:
joystickX = device_mouse_x(0); // joystick = mouse position
joystickY = device_mouse_y(0);
joystickControllerX = joystickX; // controller position = joystick outline position
joystickControllerY = joystickY;
joystickRadius = sprite_get_width(sJoystickOutline) / 2;
Code:
#region Variables
//-----------------------------------------------------------------------------------------
// Check if left button is clicked / screen is tapped. (global bc is used elsewhere)
global.leftClick = device_mouse_check_button(0, mb_left);
//Find position of mouse / finger
var fingerX = device_mouse_x(0);
var fingerY = device_mouse_y(0);
//Find distance from centre of joystickOutline to finger position
//and find which direction the finger is going
distToJoystickOutline = point_distance(fingerX, fingerY, joystickX, joystickY);
directionOfFinger = point_direction(fingerX, fingerY, joystickX, joystickY) + 180;
#endregion
//-----------------------------------------------------------------------------------------
//Set player direction and speed, and defines the maximum position of the joystickController
if (global.leftClick) {
//if finger position is within outline, set controller position to finger position
if (distToJoystickOutline < joystickRadius) {
joystickControllerX = fingerX;
joystickControllerY = fingerY;
//set player speed depending on position of finger to joystick centre
oPlayer.move = 1;
oPlayer.speed = (distToJoystickOutline * oPlayer.spd) / 75;
oPlayer.direction = directionOfFinger;
}
//if finger position is outside joystick outline, set joystick controller position to joystick outline.
if (distToJoystickOutline > joystickRadius) {
joystickControllerX = joystickX + lengthdir_x(joystickRadius, directionOfFinger);
joystickControllerY = joystickY + lengthdir_y(joystickRadius, directionOfFinger);
oPlayer.move = 1;
oPlayer.speed = oPlayer.spd;
oPlayer.direction = directionOfFinger;
}
}
//return joystick to position
else {
oPlayer.move = 0;
joystickX = mouse_x;
joystickY = mouse_y;
joystickControllerX = joystickX;
joystickControllerY = joystickY;
}
Last edited by a moderator: