SOLVED adding rotation from key presses to draw_sprite_ext rotation

In a top down scenario, you can use:

facing = point_direction(x, y, mouse_x, mouse_y);
draw_sprite_ext(sprite, subimage, x, y, x_scale, y_scale, "FACING", colour, alpha);

to help prevent rotating mask collisions when using the mouse to aim but how do you do the same thing if you use the keyboard to rotate your object? ie:

if (left_key) {
image_angle += 2;
}

if (right_key) {
image_angle -= 2;
}

How would you incorporate that into the draw_sprite_exe function?

I've searched loads of topics on here, most annoyingly, the ones where people have stated they have found a solution they haven't posted what it is...

thanks
 

Simon Gust

Member
You can write horizontal and vertical states like this
Code:
var hor_move = right_key - left_key;
var ver_move = down_key - up_key;
and then use these two inside the point_direction.
Code:
facing = point_direction(0, 0, hor_move, ver_move);
hor_move will equal to:
+1 when only right_key is pressed (1-0=1)
0 when neither (0-0=0) or both (1-1=0) are pressed
-1 when only left key is pressed (0-1=-1)

same with the vertical inputs.
The point_direction will see these in combination like a joystick you can only point in 45° directions.
 
Tried that already, and like you said, only points in 45° directions which is not the same as, or that I desire, like using the mouse to aim in a top down shooter. Is there any other way to to use the keys A and D to rotate a sprite and still use draw_sprite_ext to aid in the collisions?

Code wise, i've stripped it back to bare bones:

Step Event:

GML:
// Turret Angle
turretAngle = (point_direction(x, y, mouse_x, mouse_y));

// Player input
var _moveForward    = keyboard_check(ord("W"));
var _moveBackward    = keyboard_check(ord("S"));
var _rotRight        = keyboard_check(ord("D"));
var _rotLeft        = keyboard_check(ord("A"));
    
var _motion    = _moveForward - _moveBackward;
var _angle    = _rotRight - _rotLeft;

// Tank angle
facing = point_direction(0, 0, _angle, _motion)


// Collisions
funcPlayerCollisions();

// Animate Tracks
if (_moveForward) {
    image_speed = 1;
} else if (_moveBackward) {
    image_speed= -1;
} else {
    image_speed = 0;
}
Draw Event

Code:
draw_sprite_ext(sprPlayer, 0, x, y, 1, 1, facing, c_white, 1);
draw_sprite_ext(sprPlayerGun, 0, x, y, 1, 1, turretAngle, c_white, 1);
If I remove the facing variable and respective draw_sprite_ext then replace it with:

if (left_key) {
image_angle += 2;
}

if (right_key) {
image_angle -= 2;
}

It works as expected but obviously clips the collision mask.

Any other ideas?

Thanks
 
Last edited:

sylvain_l

Member
I must be dumb, because I cant get your question... why don't you just use your image_angle in your draw_sprite_ext function ?
 

Simon Gust

Member
I must be dumb, because I cant get your question... why don't you just use your image_angle in your draw_sprite_ext function ?
I think image_angle does rotate the collision mask perhaps.
And now that I get it too, why not just change facing instead of image_angle in your step?
 
It seems to me like you're overcomplicating this?
Create Event
Code:
facing = 0;
Step Evet
Code:
// Player input
var _moveForward    = keyboard_check(ord("W"));
var _moveBackward    = keyboard_check(ord("S"));
var _rotRight        = keyboard_check(ord("D"));
var _rotLeft        = keyboard_check(ord("A"));

if (_rotRight) {
   facing -= 3;
}
if (_rotLeft) {
   facing += 3;
}
Draw Event
Code:
draw_sprite_ext(sprPlayer, 0, x, y, 1, 1, facing, c_white, 1);
You don't need to use point_direction or anything, literally just add or subtract the facing variable and it will rotate tank-like.
 

sylvain_l

Member
I think image_angle does rotate the collision mask perhaps.
And now that I get it too, why not just change facing instead of image_angle in your step?
if I remember right default mask is rectangle, not rectangle with rotation so the mask shouldn't rotate
 
I must be dumb, because I cant get your question... why don't you just use your image_angle in your draw_sprite_ext function ?
Because image_angle rotates the collision mask and then your objects get stuck in walls in rotation...

However, RefresherTowel, you were right, It annoys me I didn't figure that out on my own...

Thanks everyone.
 
Top