Shadow that gets smaller as you jump

Discussion in 'Programming' started by Pkirkby, May 14, 2019.

Tags:
  1. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    90
    [​IMG]
    PkirkbyMember
    Hey everyone, I have this code implement from a tutorial I did. I have a z-axis in my game, and a jump command. When I jump, my shadow stays the same obviously.

    I am trying to work out an equation or simple solution to have it somehow multiply,divide, etc. by my zsp (my jump height on the z-axis) and increment by 0.1 depending on my height. I've almost successfully done it with some old code, but I can't figure it out. Hope this made sense to everyone.

    Here's my current code for my shadows:

    with(oParent)
    {
    //variables for shadows
    var sx = 25;
    var sy = 15;


    //Draw Shadow

    gpu_set_fog(true,c_black,0,1);
    draw_sprite_pos(sprite_index,image_index,
    x-(sprite_width/2)-sx,
    y-sy,
    x+(sprite_width/2)-sx,
    y-sy,
    x+(sprite_width/2),
    y,
    x-(sprite_width/2),
    y,
    0.5);


    //draw_sprite_ext(sprite_index,image_index,x-5,y-5,image_xscale,image_yscale,0,c_black,0.5);

    //This sets fog to disable,white is default.
    gpu_set_fog(false,c_white,0,0);

    }

    Here's the old way I did it, that almost worked:

    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);

    Thanks in advance everyone!
     
    Last edited: May 24, 2019
  2. Timothy

    Timothy Member

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

    Pkirkby Member

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

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    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 by a moderator: May 14, 2019
    Japster and A Random Creator like this.
  5. A Random Creator

    A Random Creator Member

    Joined:
    Jun 22, 2016
    Posts:
    88
    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
     
    Lonewolff likes this.
  6. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    ^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. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    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:
    90
    Ah yes! That probably explains why the shadows don't draw on the way up during my jump. Thanks!
     
    Lonewolff likes this.
  11. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    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:
    90
    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:
    90
    Ya I hear what you're saying, I don't know how complex I will be making the depth of levels yet
     
  14. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    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:
    90
    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. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    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:
    90
    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:
    90
    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
     
    Lonewolff likes this.
  19. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    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:
    90
    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:
    90
    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:
    90
    Edit: That worked perfect actually, I was seeing things lol. Thanks alot!
     
    Lonewolff likes this.
  23. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    Nice one, dude!
     
  24. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    90
    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. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    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:
    64
    Probably a good idea to insure this is never zero
     
    Lonewolff likes this.
  27. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,108
    Important safety tip right there. This won't fix your problem, but will stop intermittent divide by zero crashes.
     
  28. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    90
    Well, after further investigation, now that I'm back at it, I've realized that this code doesn't quite cut it. I'm trying to redo the equation, but I'm having issues. This seems to start off small, and grow bigger regardless of height.
     
  29. ZombieSquirrel

    ZombieSquirrel Member

    Joined:
    Oct 18, 2018
    Posts:
    71
    making something "pulsate" (grow larger or grow smaller, then return to normal)? Sine waves are your friend ;)
    if you do it in degrees for example, let's say the sprite size must pulsate.

    in the draw code for the sprite, make sure you have the x and y scales set as sine wave variables.
    Then, when the pulsating needs to happen, increment a value from 0 to 180.
    because a sine of 0 = 0, and if the degrees increment, it goes to 1 when it becomes 90°, then back to 0 when it becomes 180°. Which is perfect for scale arguments in draw_sprite_ext stuff.
    if you don't want the scale to start and end at 0, start and end at a different angle (like 45° untill 135° or something).
     
  30. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    90
    I understand where you're going with that, but I have no experience with sine waves. Would you be able to set me an example? My goal is to have a simple code that duplicates the objects sprite and displays as a shadow below them, and reuse this simply for every object. Is there a way with draw_sprite_ext to do this?
     
    Last edited: May 23, 2019
    ZombieSquirrel likes this.
  31. ZombieSquirrel

    ZombieSquirrel Member

    Joined:
    Oct 18, 2018
    Posts:
    71
    create event:
    xScale = 0;
    yScale = 1;
    Degree = 0;

    draw event: draw_sprite_ext(SPR_Shadow,.... xScale, yScale,...alpha(transparancy))

    step event:
    if (trigger for shadow scale, your choice)
    {
    Degree += 1
    xScale = dsin(Degree);
    yScale = dsin(Degree);
    if (Degree > 180)
    {
    Degree = 0
    }
    }

    this will go quite fast, the Degree +=1 will probably have to be much smaller than +1.

    This is also a dsine, not a sine. dsin means the angles used work in degrees, and sin means the angles used work in radians (0 to 2 pi).

    This code will also make the sprite go from 0 scale to full scale, you probably don't want that. Try starting at a different angle, like 45° and ending at 135° or something.

    Keep in mind you will have to draw the shadow a little big bigger than intended, as you can't make a sprite grow this way, the scale can only go to 1 and not higher (same with a sine).
    There might be a trick to enlarge the sprite further than a 1:1 scale however.


    EDIT:
    that's just how a shadow scale COULD work. If you want to duplicate a known sprite and use that for the shadow,
    just redraw the object sprite with another draw_sprite_ext. I think you can change the color to fully black there. The second to last argument in draw_sprite_ext is "col": colour. You'll have to mess around with it to get it right, so try inputting c_black first. There are a lot of color functions you could use for this, i'm sure there is a way.

    Don't forget the draw ORDER too.
    Shadow sprite comes first.
    Object sprite comes second.
    This way the shadow is always drawn underneath the object.
     
    Last edited: May 23, 2019
  32. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    90
     

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