1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Shadow that gets smaller as you jump

Discussion in 'Programming' started by Pkirkby, May 14, 2019 at 1:50 AM.

Tags:
  1. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Hey everyone, I have some code for when my character jumps, and the shadow draws as a different size for each increment of my z-axis (when I jump.) I realize there's certainly an easier way to do this with less code, but I don't know where to start. Any help would be great.

    if (zsp = 1)
    {
    draw_sprite_ext(sprite_index,image_index,x,y,image_xscale/2,image_yscale/2,
    image_angle,c_black,0.5);
    }
    else if (zsp = 2)
    {
    draw_sprite_ext(sprite_index,image_index,x,y,image_xscale/1.9,image_yscale/1.9,
    image_angle,c_black,0.5);
    }
    else if (zsp = 3)
    {
    draw_sprite_ext(sprite_index,image_index,x,y,image_xscale/1.8,image_yscale/1.8,
    image_angle,c_black,0.5);
    }
    else if (zsp = 4)
    {
    draw_sprite_ext(sprite_index,image_index,x,y,image_xscale/1.7,image_yscale/1.7,
    image_angle,c_black,0.5);
    }
    else if (zsp = 5)

    etc etc.
     
  2. Timothy

    Timothy Member

    Joined:
    Aug 7, 2016
    Posts:
    59
    /(2 - (zsp -1) * 0.1)
     
  3. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Fast reply! Would you mind explaining it a bit? How would I go about putting that code in? Excuse my noobishness.
     
  4. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    I am guessing he means replace the whole block of code with something like this;

    Code:
    draw_sprite_ext(sprite_index,image_index,x,y,image_xscale/(2 - (zsp -1) * 0.1),image_yscale/(2 - (zsp -1) * 0.1),
    image_angle,c_black,0.5);
    No 'if' tests needed.

    Also, it is unlikely that the if statements will ever be triggered unless your zsp is exactly the value you want. As soon as your zsp contains a decimal place, your code will fail.

    Using the mathematical approach will give smoother scaling as well, rather than sudden changes in scale.
     
    Last edited: May 14, 2019 at 2:26 AM
    Japster and A Random Creator like this.
  5. A Random Creator

    A Random Creator Member

    Joined:
    Jun 22, 2016
    Posts:
    87
    Code:
    draw_sprite_ext(sprite_index,image_index,x,y,image_xscale/(2 - (zsp -1) * 0.1),image_yscale/(2 - (zsp -1) * 0.1),
    image_angle,c_black,0.5);
    Use this, same thing, just took out one of the double / so that it actually functions as intended
     
    The Sorcerer likes this.
  6. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    ^Ha! I didn't even notice that. Was a quick copy paste thing. Good pick up. (Fixed it up :))
     
    A Random Creator likes this.
  7. E.M.I

    E.M.I Member

    Joined:
    Apr 9, 2019
    Posts:
    11
    Hey there! Just a small question, can the height of the jump change? Because just making it smaller when the Z value rises will make it so the shadow constantly gets smaller if the level goes up. I would personally make different sized sprites, put them into a sheet and make it so playing the jump animation will change the value of a variable that will make it so the shadow plays the animation to get smaller and then bigger. Something like:

    (In the create event of the player object)

    Code:
    ///@description
    
    is_jumping = 0;
    (In the step event of the player object)

    Code:
    ///@description
    
    if (keyboard_check(ord("W)) //Set it to the key you want
    {
    is_jumping = 1;
    //Put your jumping code here
    is_jumping = 0;
    }
    (In step event of the shadow object)

    Code:
    ///@description
    
    // To use the is_jumping variable
    
    with obj_player; //replace obj_player by the name of your player object
    
    // To play the animation
    
    if (is_jumping == 1)
    {
       image_index = shadow_animation;
       image_speed = img_spd; //Set it to whatever you want
    }
    
    else
    {
       image_index = shadow_idle;
       image_speed = 0;
    }
    Wrote that up quickly so there may be some errors to iron out.
     
  8. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    Isn't that the point?

    I think I get what you are saying there. It may not be desirable for a game where you go up levels in a building. But any solution is dependant on what you want to achieve, in your particular scenario.
     
  9. E.M.I

    E.M.I Member

    Joined:
    Apr 9, 2019
    Posts:
    11
    Well, I think it's ridiculous to think that the level won't go up a bit at any point of the game. Even if it's just a small slope, it'd look weird if the shadow got smaller as you walked up the slope right? That's not how real shadows work. Though, if there isn't a fixed jump height, my code would have to be changed, but we don't know that.
     
  10. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Ah yes! That probably explains why the shadows don't draw on the way up during my jump. Thanks!
     
    The Sorcerer likes this.
  11. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    I think it is ridiculous that you wouldn't think to use your ground as the point of reference. :)
     
  12. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Hey guys, I see what you're saying, the reason I wanted it to change size in increments like that is so it'd register it on the way up and down.
     
  13. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Ya I hear what you're saying, I don't know how complex I will be making the depth of levels yet
     
  14. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    No worries. You can subtract y from y_previous for that one. The sign will give you the direction.
     
    Pkirkby likes this.
  15. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Yeah, I should be able to find the bottom of the z-axis references and use that. Not that I know how, but I'll look into it. Thanks
     
  16. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    That was aimed at the other dude. As I said in a previous post the ultimate solution is really reliant on the task at hand. Sounds like you are on top of it.
     
  17. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Oh ok, but how do I set it up in switches or some sort of simple array? I'd really like to make a shadow manager that controls shadows of all obecjts I choose
     
  18. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Im getting there,. My goal is to have the reference for the shadow the floor of Z axis, so I have some work to do in general. Thanks
     
    The Sorcerer likes this.
  19. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    I'll take a bit of a look for you and see if I can come up with something.

    Having a z axis, your game is intended to have a 3D (quasi-3D) feel to it? Sounds like you are not going down the traditional 2D side on route, right?
     
  20. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Yeah that's right, it's essentially an ARPG, but I want a jump mechanic, so I'm working on how the shadows draw, I'd like to see it grow and shrink as you fall or rise. So far I kind of have it with that code, but for some reason it's not drawing shadows on the way up. I realize it's probably because there may be decimal points.
     
  21. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    That worked, however, I need to reverse it for the way down. I'm going to fool around and see if I can figure it out. Because right now the shadow grows to max size while at the highest point of the jump, and stays that size.
     
  22. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Edit: That worked perfect actually, I was seeing things lol. Thanks alot!
     
    The Sorcerer likes this.
  23. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
  24. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    29
    Oh, there still is one little problem, even though right now the shadow only appears under the player, Id like to try angling it for a casted shadow at some point, and right now the shadow only seems to draw while zsp is above 0. So only draws when I jump.
     
  25. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    Easiest way is to add an if statement

    Code:
    if(zsp == 0)
       // draw normal sized shadow
    
     
  26. Timothy

    Timothy Member

    Joined:
    Aug 7, 2016
    Posts:
    59
    Probably a good idea to insure this is never zero
     
    The Sorcerer likes this.
  27. The Sorcerer

    The Sorcerer Member

    Joined:
    Jan 3, 2019
    Posts:
    247
    Important safety tip right there. This won't fix your problem, but will stop intermittent divide by zero crashes.
     

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