Shadow that gets smaller as you jump

Pkirkby

Member

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:
L

Lonewolff

Guest
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:
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.
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
 
E

E.M.I

Guest
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.
 
L

Lonewolff

Guest
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.
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.
 
E

E.M.I

Guest
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.
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.
 

Pkirkby

Member
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.
Ah yes! That probably explains why the shadows don't draw on the way up during my jump. Thanks!
 
L

Lonewolff

Guest
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.
I think it is ridiculous that you wouldn't think to use your ground as the point of reference. :)
 

Pkirkby

Member
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.
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.
 
L

Lonewolff

Guest
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.
No worries. You can subtract y from y_previous for that one. The sign will give you the direction.
 
L

Lonewolff

Guest
Ya I hear what you're saying, I don't know how complex I will be making the depth of levels yet
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.
 

Pkirkby

Member
No worries. You can subtract y from y_previous for that one. The sign will give you the direction.
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
 

Pkirkby

Member
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.
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
 
L

Lonewolff

Guest
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?
 

Pkirkby

Member
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?
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.
 

Pkirkby

Member
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.
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.
 

Pkirkby

Member
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.
Edit: That worked perfect actually, I was seeing things lol. Thanks alot!
 

Pkirkby

Member
Nice one, dude!
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.
 
L

Lonewolff

Guest
Easiest way is to add an if statement

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

Pkirkby

Member
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.
 
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.
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).
 

Pkirkby

Member
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).
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:
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:
Top