1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 [SOLVED] Having trouble implementing clamp (for image rotation)

Discussion in 'Programming' started by AtomicToilet, Feb 21, 2019.

  1. AtomicToilet

    AtomicToilet Member

    Joined:
    Jul 6, 2016
    Posts:
    70
    Holaarrgghhh!

    I'm working on implementing a flashlight that the player should only be able to rotate a set amount depending on the direction they're facing - should be easy, right? Well, I've got the flashlight working, and code that makes sure it's facing the right direction according to the player, but when it comes to actually rotating the light, I can't quite figure out how to clamp the rotation angle - I don't think I can use image_angle = clamp(image_angle,min, max) because the code looks like this -->

    flashlight object draw event (the most important part is //COMMENTED)
    Code:
    var bbb, i;
    bbb=20;
    surface_set_target(global.light)
    draw_clear_alpha(c_black,0.9)
    draw_set_blend_mode(bm_subtract)
    
    for (i=0; i<=bbb; i+=1)
    {
    draw_sprite_ext(spr_torch_a, 0, obj_player.x, obj_player.y, (1-(1/bbb*i)), (1-(1/bbb*i)), rotation, c_white, 1/bbb) //THIS IS THE ACTUAL FLASHLIGHT - I understand the 'rotation' variable is what I'm having trouble with, but as you'll see in the next code block below I think I have to have it linked with point_direction...
    }
    
    draw_set_alpha(1)
    draw_set_blend_mode(bm_normal)
    surface_reset_target()
    
    if surface_exists(global.light)
    {
    draw_surface(global.light,0,0)
    }
    else
    {
    global.light=surface_create(room_width,room_height)
    }
    I have a key press to set a torch_mouse_look variable to true or false, and in the flashlight step event I have:

    Code:
    if torch_mouse_look == true and obj_player.image_xscale >= 0
    {
    rotation = point_direction(obj_player.x, obj_player.y, mouse_x, mouse_y); //THIS IS THE BIT I'M HAVING TROUBLE WITH - how can I clamp this variable?
    }
    
    else if torch_mouse_look == true and obj_player.image_xscale < 0
    {
    rotation = point_direction(obj_player.x, obj_player.y, mouse_x, mouse_y) //SAME HERE
    }
    
    else if torch_mouse_look == false and obj_player.image_xscale >= 0
    {
    rotation = 0; //no problems with this
    }
    else if torch_mouse_look == false and obj_player.image_xscale < 0
    {
    rotation = 180; //or this!
    }
    As always, any suggestions are greatly appreciated!
     
  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,223
    Code:
    true_angle = center_angle + clamp( angle_difference( angle, center_angle ), -max_angle, max_angle);
    "center_angle" would be the middle of the arc within which you'll permit the flashlight to turn.
    "max_angle" is how much anti-clockwise and clockwise of "center_angle" the flashlight is able to turn.
    "angle" would be the input, point_direction(), or whatever.

    You're going to notice the flashlight suddenly jumping from max_angle extremes when the input "angle" goes across the 180 degree point from "center_angle". To stop that from happening you'd need to at least introduce a turning rate for your flashlight.
     
    AtomicToilet likes this.
  3. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    423
    angle_difference solves so many issues when working with angles.
     
  4. AtomicToilet

    AtomicToilet Member

    Joined:
    Jul 6, 2016
    Posts:
    70
    Thanks a bunch, flyingsaucerinvasion! I'll mark this as solved, as you've given me a really solid basis to play around with. Cheers!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice