@@saxenart It makes the variable a local variable that gets destroyed after the function/event is over. In addition, you can't access the variable outside of the function/event. You technically never need to use them, but did it here anyways. If you have anymore questions, or need me to clarify something, I'd be happy to answer!
If anyone is experiencing the shadows jittering when using a camera object, it's because the x and y values for drawing the sprite are being set as decimal values (I think!). To fix this try using the 'round' function, like this: var viewX = round(camera_get_view_x(view_camera[0])); And the same goes for the viewY.
Yes this could cause a jitter, especially for low rez games that might have already set up their own camera functionality. Thanks for posting this and your solution :)
Thank you so much for posting this, today when I tested my room in full screen I realized I had some jittering in the shadows while moving my character around, I was the whole day trying to figure out what was it and this fixed it. Thanks!
It's hard to believe you're a new channel; your content is on-par with some of the best GM tutorials out there. Not a knock on them; on the contrary-you're just really good.
OMG, you are so awesome. I am trying to make a top-down 2d game similar to early pokemon games on game boy advanced. I am thinking of making dynamic day and night system which requires constant changing shadows and your tutorial definitely helps me on that. Thanks a lot.
This is a great video for sure! However, if there's anyone out there who doesn't want to hard code a system like this you can use Construct 2 for this kind of effect. To achieve it, Construct 2 comes with objects that do this sort of thing automatically. Instead of typing lines of code (which doesn't look like much here) you can just add the behavior to specific objects with just drag and drop features in C2, and adjust the settings accordingly. I hope this helped! Again, wonderful video! I've been trying to learn this myself and this is a rare tutorial to find. Most of the time I found something like this was just simply demos with no instruction. You're great!
You're most welcome! I love Game Maker Studio 2 and Construct 2 since I've used them both. They're great for their own reasons. :D I also agree that it adds a lot of depth to a game.
Superb tutorial, thank you! - I was trying to get my head around using surfaces for shadows, and this was really easy to follow - sorted! - thanks again!
If your sprite's canvas is larger than your sprite itself, it'll give you a much larger number than you'll want. In that case, ,use (sprite_get_width(mask_index) * image_xscale)/2 instead. It'll give you the collision mask of the sprite instead.
Found your channel by it being recommended after watching Heartbeast and Shaun Spalding. Amazing stuff you've got here. Thanks for the explanation! No more elliptical shadows for me! ;)
Thank you so much! For some reason, I haven't thought about "with" statement. I was trying to forse every object to draw in the shadow surface, after which obj_shadow draws the surface, but because the room in my game is giant and there's a lot of objects game was lagging. But using "with" it doesn't lag!
Could you make a tutorial about how to make an inventory system like the ones in Pokemon or Final Fantasy? It's really hard to find nice, well-explained videos on it. Keep up the great work on the videos!
Were you able to find any tutorials on that? I've been looking for one for a little while now. While i love Coamonauts videos on menus those are mostly for start menu types.
Awesome tutorial! I’m having a problem, though. I want the shadows to face away from an object as if it was casting light. However, I haven’t really been able to get my head around it. Any help?
I'm having this same issue, I managed to create a "static" ellipse shadow for my objects, although when I move it looks a bit jittery. I'm guessing this is because every time I move the shadow is being erased and redrawn in the same position, which makes it look jittery. I'm unsure if something can be done. *Edit* : Don't think you'll need it anymore but the solution to fix it is in the comments below, you have to use this variables instead: var ViewX = round(camera_get_view_x(view_camera[0])); var ViewY = round(camera_get_view_y(view_camera[0])); The reason is because the x and y values for drawing the sprite are being set as decimal values.
Thanks a bunch, this was a very helpful video. I managed to add a bit of code to allow the shadows to move realistically when the player jumps! Basically, I have two variables in my player object, one called 'z', which tracks the player's position above the ground, and one called 'yfloor' which tracks the point on the ground which the player is floating above when he jumps. So in the 8 arguments for the draw_sprite_pos function, I changed all mentions of 'y' to 'yfloor', and I subtracted 'z' from each argument. To make it more accurate, I subtracted '(z/2)' in the y position arguments instead of just 'z'. The main thing I had to change is that I had to add the 'z' and 'yfloor' variables to objects that didn't use them already.
Really liking your tutorials and your measured, clear explanations of the principles. Only think I'd really like is that when you typing out or going through lines of code, even on full screen the text is pretty small. I can just about read it if I get close enough to the screen (apologies, I'm short sighted), but would love it if you could figure out a way of zooming in a little so it's clearer from a regular distance.
Thank you! Yes in my latest videos I've taken up the habit of increasing the text size in the videos, but unfortunately a few of the older ones are like this.
I'm not sure if @friendlyCosmonaut you are following the comments at all but I have a few questions. 1. If I draw the shadow on a character,npc, player object etc. the shadow will change depending in which direction my toon is facing. So the shadow falls left if my character or NPC looks left or it falls right when I or the NPC looks left. Of course the simplest workaround is to use a different shadow ( for example the simple eclipse shadow under the character ) but I am wondering if this can be fixed with code ? 2. I would like for the shadows to cast shadows also on object "below" the shadow. If my character walks under a tree where the shadow falls I would him to also get the same color as the shadow, the same for the stone which is under tree and has a shadow on its own. Is that possible ? Please keep in mind that I do not use priority to determine the depth, I use a simple line of code in the step event in the parent object : depth = -bbox_bottom; . So I already tried to move the instance layer above the player but it doesn't really work. Do you maybe know or have had similar issues ? :
Thank you for showing me this! I do have one question though: If I want to make the height/length of the shadow relative to the height of the object like in real life (taller tree, taller shadow). Can I still use this same method or do I have to make the shadows manually as part of the sprite as I have always done before? Thought it looked kind of funky how the small bushes had the same size shadow as the buildings etc.
This is probably way too late but if you wanted to keep things simple you could probably just have multiple parents for different height groups (short, medium, tall, extra tall) and adjusted the variable accordingly, then set the objects' parents to whatever one you felt fit best. edit: Now that I think about it you could probably also just do some simple maths when setting the shadows length/height by doing something along the lines of, say, var sx = sprite_width * n where n is any random number you feel like putting there (experiment with it). Keep in mind I haven't tested this out so you may need to delve a little deeper into it to get it working, but you get the idea.
this works, though the shadow won't invert its scale when the reference object does (and understandably so!) Problem is, I can't figure out how to invert its scale since all of the "drawing" part happens in a single function, so its abstracted from me
Nice tutorial, done something similar in the lighting system I have. I am currently trying to work out how to change the shadow skew when the shadow hits a "vertical" surface, ie the shadow stretches across the "horizontal" surface and skews when it hits the "vertical" all dependant on "light" position. Currently thinking about using another surface to draw the altered shadow, but currently struggling to conceptualise how to detect when this should happen, any pointers or ideas welcome.
Ah, this probably won't be the easiest thing to do. I'd suggest taking a look at primitives or vertex shaders for this, as you'll want to control the "vertices" of how the shadow sprite gets drawn. Unfortunately I don't know of any tutorials that cover this sort of thing so I can't point you anywhere other than the documentation for primitives/shaders!
I've noticed that the draw_sprite_pos function defines the BOUNDING BOX of the sprite to draw. Because my sprites don't occupy the whole of the image size, my shadows ended up wider than they should have been. Modifying the points to have "bbox_left" rather than x-sprite_width/2, etc. fixed this issue.
Awesome video. I'm currently working through your farming tutorial as well. I've just finished the day night cycle. Everything is working fine. You started to go into how to add this to a day night cycle, but I'm wondering how would you add this to your day night cycle?
Thanks for the tutorial! Do you know if there is any way to get this method to work using vector images (swf files) as draw-pos does not support them? Draw-ext doesn't look nearly as good and it would be a bit tedious to create a shadow version (non swf) of each image with animation.
Thank you! I can't seem to find your other comment so I will reply here: Yes you could have your sprites centred. In the parent object, setup a variable called y_offset = sprite_yoffset. That way, all the children will set this variable in their create event. Then in the draw object, instead of draw_self(), use draw_sprite(sprite_index, image_index, x, y+y_offset);
Is there any difference from using lengthdir_x and lengthdir_y instead of (room_width/2)-mouse_x and (room_height/2)-mouse_y. I threw in lengthdir just because i'm used to using it and got about the same results but does it put more stress on the game?
Huh, no, I didn't actually think of it at the time (I sort of just threw that line of code in there as an example). Vector stuff is probably the easier way to go, so feel free to use it! I don't think there'd be much of a difference to performance one way or the other.
Hey friendlycosmonaut, amazing tutorial as usual, however how do i mix the shadows together with the depth system? I can't seem to get it to work, where do i put what etc.
I figuered it out, lol xD So you set your objects as your children in your "obj_parent_xy", right? But you are NOT allowed to have multiple "obj_parents_xx" with the same "obj_children". Thats why you have to create only one "obj_parent" with your children-objects and replace the "obj_parent_shadow" and "obj_parent_depth" in your code with your new single obj_parent. i hope you understand me.
You mean apply shadows to tilemap "objects" like a house made of tiles? Ooh, that'd be tricky. I wouldn't say it's not doable -- everything is if you throw enough code at it -- but I'm not sure it'd be worth the hassle. Perhaps it's just beyond me; if anyone else has ideas, I'd love to hear them!
Yeah I was thinking how cool it would be to have a tile layer and any tiles that were placed on that layer generated shadows in a similar way your system does. Instead of using actual objects under a parent. I'm doing all tile collisions and trying to reduce as many objects that I don't need.
This is cool, but there is still an issue. What if the shadow being casted needs to be drawn over the player or another object? For instance, if the player is standing under a tree, obviously the tree's shadow should be drawn over the player, but if the player is standing under a rock, then the player should not be affected because the rock's shadow is under them. Do I just have to draw those specific shadows on a separate layer?
Great tutorial! I've got it working for the most part. I have a z-axis in my game, and I'm trying to make the shadow shrink as I jump. I have a zsp that determines my height. Any idea how I would implement an equation that could somehow use my zsp and shadow size to shrink/grow as I am in the air? I know it's asking alot, so don't feel pressured to answer that lol.
i have one issue not about the surfaces itself but the way my game is set up i have a z axis and you can jump on stuff with a higher z and i have the shadow follow the players z_floor and since the surface for the shadows are drawn behind everything they arent seen when i jump on a platform because its behind the platform sprite. I hope i explained that right
Thank you for the amazing tutorial! i have one question tho. My sprites do not fill the entire width of the sprite (with this i mean, the guy i'm trying to draw the shadow of, has some empty space on each side to later animate sword animations etc). This makes the shadow of the person im drawing really wide, any ideas?
Hmm! As you probably know, this is happening because of our use of "sprite_width" (or sprite_width/2, anyway). Instead of using that, we could make up a variable called "shadow_width" and declare it in the create event of the parent. As it's default value, we'll make it shadow_width = sprite_width, and we can just leave it at that for objects and sprites that don't have this problem (the children will inherit this value). But for children that you want to tweak the shadow width, you can go to their create events and redefine the value as something else -- it will overwrite what it got from its parent. Note: if you have any objects that already have a create event, but you want them to inherit shadow_width, you'll have to put "event_inherited()" in their create event.
Alright, thanks a lot! Just one other question, in my game i move the camera around with my mouse, and i have a zoom function as well. When zooming out, the shadows won'' draw the way i intend to. Is there maybe a way to contact you so i could show you?
Very nice, not perfect shadows, but then who needs perfect when good enough does. :) Though I am curious as to what kind of programming is needed for absolutely pixel perfect shadows, such that if they hit another object they'll climb up it properly and stuff like that.
Fantastic tutorial, allowed me to do simple ground shadows to add that little bit of graphical flavor, and I really appreciate you teaching what each line of code's purpose is, very informative. I know the video's a bit old at this point, but was wondering if there was a way to reverse the image_xscale of the shadow when the object's sprite_index's image_xscale is flipped. I feel like the answer is simple but I can't seem to figure it out. Thanks again, cheers.
Actually this should be happening automatically with the function draw_self(). But you could use draw_sprite_ext(sprite_index, image_index, etc) and plug in all of the instance's variables to be sure.
Unfortunately it didn't do it itself, I would have thought it would do it automatically as well but for an odd reason it always draws the xscale = 1 version. However I did find a solution, which was to interchange the x1 and x2 with each other if the obj's image_xscale = -1, so the sprite's specific corners are flipped therefore flipping the sprite. I appreciate the reply though, thank you.
Oh, doh! I was mis-remembering this video. Of course, draw_sprite_pos was just stretching the co-ordinates to a scale of 1! You found the perfect solution, well done :)
Everything works except this one! I just realized same problem too. Static objects are fine but with animated objects(like a player or enemy) it always shows xscale = 1 on shadows. I'm a beginner, How exactly do i fix it? Also, i'm using draw_sprite_ext(); because i don't need to skew or anything.
Lovely video, if there anyway to get this to work in GM 1.4? the gpu_set_fog doesn't exist, but I looked in the manual and there is a "gm_FogColour" and "gm_PS_FogEnabled." Also, how would I make a shadow for an object using a layered-sprite fake 3D?
Could you please make a follow up tutorial on the last part before the video ended, with the shadows rotating throughout a day cycle?? This is really what I want but I can't figure it out...
i know it a bit late and you figured it out, but this is for other people who see your comment. in a create event of an object write alarm[0] = 700; (this should change depending on the day speed). then in the alarm[0] event write obj_shadow.cx ++; obj_shadow.cx ++;. that should work, but i didnt test it
Thanks for these! I have a question, why doesn't it work with a single object instead of two? I mean, at 13:50 you create a shadow object with the code from the parent object, then set them to the layer. Why doesn't it work if you only have the obj_parent with the code itself and assigned it to a layer?
This is a great question! The short story is: you don't "have" to create the second object... but doing so means the system does a lot less work then it has to. Longer story: if we put all the code in the parent object, that means every single child would be running every single line of code. That means we'd be having to check that the surface exists in every child, having to turn on the fog, and then drawing the surface multiple times. As we create more and more instances in the room (tens, perhaps over a hundred), you can see how costly this would all get! I know that it may seem a little excessive to have two abstract-seeming objects for this system, but having lots of object parents is very much the norm, and an essential part of object management/hierarchies/organisation. It helps to think of this in the context of a larger, established game. Let's pretend we have dozens of objects. NPCs, dogs, cats, trees, chairs, fires, stars, food, health, potions, plants... the list goes on and on. In such a case, it will make sense to have various parents for those objects (enemies, friendlies, furniture, light sources). So this "obj_parent" in the video would just be something at the top of the hierarchy, that links together everything with a shadow. Whereas, obj_shadows is not a "parent" object, it's an object that does work - it draws all the shadows. I hope that makes sense and puts things into perspective!
Another great tutorial, I am combining this with the depth system but once I set the children to the obj_shadow the obj_dpeth code stops working, I suppose that it overwrites it. Should I mix the two of them together in one object? The problem is that I may have an object with no shadow and depth system. Thanks
Oh and also, I'm having problems with drawing shadows on sprites that are drawn in a canvas a bit bigger than them, I mean that there's a bit of leftover pixels in the sprite wich are transparent. An example is my main character, it's drawn in a 32 * 32 square but it logically doesent occupy the whole thing. The problem comes when drawing the shadow (I removed the X offset to solve this bug), it draws a shadow 32 * 32 shadow with the shape of the sprite, it deforms the sprite to fit the shape of the canvas wich is pretty annoying. I suppose that the function "sprite width" counts the width of the sprite as the canvas width, the sqare where the sprite is drawn, but the function you use to draw the deformed shadow counts the four points only where it is drawn. How could I fix that, do I relly have to make all rthe "canvases" fit the size of my sprite? Thanks a lot for your dedication
Hey Carles, sorry for the late reply - yes once you make the objects children of one object and not the other they'll stop inheriting from it. You might have to make the depth object a child of the shadow or vice versa. Then you could have a toggle for whether to draw the object as part of the depth/shadow, like "depthObject" and "shadowObject". These would be set in the parent as true by default (all the children will inherit this), and you can manually go into objects you don't want to have as part of either system and mark them as false. Then, have a check in the draw code to exclude any object that has those variables marked as false from the drawing process. As for the width issue - you could put in a special case for those objects but yeah the simplest thing to do here is to remove the "whitespace" from the sprite, as annoying as that is! Hope that makes sense!
Not for the later section where we use draw_sprite_pos -- there's no way to set a "blend" for this, so we have to do it ourselves with the gpu_set_fog. You could also use a simple shader to accomplish the same thing.
Hi! This was great, do you have any tips on how to do indoors lightning? I'm trying to make it so that I can have the shadows point away from an invisible object in my room, the way a ceiling lamp would make things cast a shadow in the opposite direction from it. I've played around to try and fix it on my own, but I can't get it to work. I want the skew of every object to be different depending on where they are in relation to my light source, but instead every object has the same skew as my player object. Also, I'm not sure how to make the skew of my shadow change every time my player moves. PS: I'm a complete noob.
Thank you! Ah, to be honest, this probably requires a lot of fiddling and a good deal of math (vectors). It sounds like instead of a set skew, you'll want to be getting the direction and distance objects are from the light source, and then change the skew accordingly. So, you'll want to be using the functions point_direction() and point_distance(). Definitely have a look at those in the documentation (you can type them in to a code block and then middle click to bring up the appropriate page). You'll also probably need the lengthdir_x and lengthdir_y functions, as you'll want to be creating vectors the shadow should follow, extending from the object. Essentially, what you'll need to do is get the point_distance and point_direction of the light source to an object. Then, you'll want to translate the "point_direction" into horizontal and vertical components with the lengthdir_x/y functions. The length of these components should depend on the point_distance. Once that's all calculated, you can use this information to form where the shadow should be located. This is a little "advanced" if you've never met with these concepts before, but to be honest, it would make a great beginner exercise if you can get your head around all the functions and vector concepts.
Hi! Thanks for the helpful response, I messed around with it for a while and kind of got it to work, but I ended up scrapping it because I felt like It'd have to be far more complex and way beyond my skill level to get it to work the way it should. Here's a screenshot to explain the problem imgur.com/a/9Gfbm, it should have been obvious to me that this method wouldn't work when the shadow is in front of instead of behind the object, because you'd need the shadows to "extrude" from 3 points instead of just 2 (that's my best attempt to explain it in words, haha). Also, THANK YOU for the inventory system tutorial. I've had a literal massive headache trying to find a guide for that and yours is the only one that made any sense to my noob brain.
Very nice tutorial. I tried it out myself, however the draw_sprite_pos( ); just won't work for me. I've tried everything that's to my knowledge and I also tested if other draw_ methods would work (they did) but draw_sprite_pos won't generate a sprite for me. Do you have any clue why this could be happening?
Hmm, nothing comes to mind. If other functions are working but this one isn't, I can only guess that the co-ordinates that you're plugging in may not be right? You could be putting them in the wrong order. It should be top left (x,y), top right, bottom right, bottom left.
Thanks for the reply. I think I messed up the order of the coordinates or something. Didn't catch that when I was testing most likely since I've tried it multiple times with different sprites and on different projects. It's working fine now, thank you for helping out! Love your content btw, been watching your videos for that past few days and it's been really helpful and (more important) insightful!
I'm away from my computer for a couple days so unfortunately can't test this. But I believe the problem is arising because we can't have an object have two parents. We can however do the next best thing: make one of those parents a child of the other parent. So, make the parent object from the shadow tutorial (obj_parent) a child of the parent object from the depth tutorial (obj_parent_depth). Let me know if this works -- otherwise, there's a couple more things I can test once I'm at my desktop.
nice tutorial, however, i'm trying this on game-maker studio 1 and the function "gpu_set_fog" doesn't exists so i cant make my shadows black! Do you happen to know a workaround for this?
Yeah the function you'll want is d3d_set_fog. Here it is in the docs: docs.yoyogames.com/source/dadiospice/002_reference/drawing/drawing%203d/3d%20lighting%20and%20fog/d3d_set_fog.html
Hey Friendly Cosmonaut! I love your tutorials and I get through them very quickly, but I get stuck after this one. I really want to make a day/night cycle with the shadows based on the time of the day like said it in your video. I made a simple time system(not optimized at all but working) and a surface darkening when dusk comes and lightening when dawn comes, but I am struggling with the shadows. Could you please help me a little with what maths I have to use for the skew?
Hey, thanks! It depends on what you want the shadows to do exactly. Could you describe what you are imagining happen to the shadows as the day progresses?
I would like to have shadows following the path of the sun, with a dawn, were the shadows are long and going from the object to the east(room's right), at noon the shadows are inexistant (or not long at all and going to the north) and a dusk were the shadows are also long but going to the east. All of this dynamically and progressively, with a little shadow_height to have the impression of the sun being above the camera.
Ah okay. I might do a follow-up video and include something like this. For now, I've tossed together some images that should help you out: i.imgur.com/7pMGjrM.png The result of what it should look like is this: i.imgur.com/A3sWwyd.gif Obviously tweak variables as needed. Personally I liked the look of going south -> north, but you could just increase the skewX more to do it the other way around.
Thank you a lot for your help. I am moving towards the CBNA Smart Light engine who manages lights, shadows and time way better than everything I can do but I will improve my code with your example to finish it, maybe if I want to change later.
So my sprite area is bigger than the sprite it self. When going vertically the shadow changes size and I think it is because the animation does something, but I don't know what. I already set new variables to replace the sprite_width. Does anybody know what is the problem?
Well, my animation is first 4 pixels wide and it changes in walk animation to 6. And my sprite canvas (or whatever it is called) is 16 pixels. So should I change it so every time when it is at this certain animation and also every time when image_index is at the certain pictures then change the sprite_width. Or is there any easier way to do this?
I see the problem, I think -- when the player is on the side and the shadow's width appears greater than the player. I'm not entirely sure but I think it has to do with your sprite sizes, and drawing "empty space". Could you reduce the canvas size so that it's 6 pixels wide (so that the player picture "fits perfectly" inside it)?
The draw_set_colour wouldn't actually work in this case, as the draw_sprite_pos will ignore all those draw_set settings. It just draws the "raw" sprite. So we have to take a different tact - the gpu_set_fog changes how the GPU is renders the sprite.
So kind of like how I've done my mouse stuff, visible at 20:50, but instead of mouse_x and mouse_y, you'd grab obj_light.x and obj_light.y. Then you'd just flip the minuses to pluses so the shadows go away and not towards it.
Hi there. Fantastic video. I've been trying something similar (using a light object). With the code below, I can get it to lock onto whatever the nearest light object is, and it adjusts to character position and looks great: var light = instance_nearest(obj_parent.x,obj_parent.y,obj_light); var sx = light.x - lerp(light.x, obj_parent.x, 5); var sy = light.y - lerp(light.y, obj_parent.y, 5); However... my main issue is that while it works... the shadows are doing the opposite of what they would in real life. The closer the parent gets to the light source, the smaller the shadows get and the further away, the bigger(times infinity). I'd love to flip it so that it is closer to reality, I figure it must have something to do with the y values in the 'draw_sprite_pos' but I've been banging my head and can't for the life of me figure it out. If you have any ideas, that'd rock! Regardless, love the channel, keep it up!
Do you mean like this: var sx = room_width/2+instance_nearest(x, y, o_light); var sy = room_height/2+instance_nearest(x, y, o_light); still not working though lol Thanks for your help :)
Thanks Hamish. The answer is: fiddly maths, where we make the shadows LARGER when the distance between the objects gets SMALLER. At the moment I'm using the reciprocal (1/distance) to do this, but it needs some other magic factors. I'm just madly finishing up a game for the low rez game jam (finishes in a few days), but after that, I'll probably make a video on this. I was playing around with it and got it working with multiple light sources too. But I'm not too happy with my "fiddly math" yet (the shadows not growing at a "rate" I would like), so still have to tweak it.
Joshua Auvia Patricia Pagcaliwangan I had the same issue but I worked it out, the object I was referring to with the ‘with’ statement didn’t have all the other objects as children. I’m trying to use this in conjunction with the depth tutorials.
Aaaaaand now it doesn't run at all after I removed this code, even though I didn't even touch the depthgrid code and this is the only code I added or even opened. Coding is "awesome".
Yeah there are some pretty significant changes between the two videos... even the smallest change/mistake can mean the system doesn't work. I can't really guess the error without seeing your code -- I'd suggest having a look at the finished project's code (link is in the description) and comparing it with your own. Don't give up! Programming is fiddly work :)
I came for the shadows and stayed to learn about surfaces. Also, I love your style of explaining things. Thank you!
This really improved my game's visuals. Thanks a lot! I also made the day/night cycle affect the shadows, which looks awesome.
Awesome! Glad it helped :)
if you want to make the shadow scale to the sprite use this
var sx = sprite_width/2
var sy = sprite_height/2
instead of
var sx = 5
var sy = 10
@Colby Brian um... ok? but what does this have to do with anything?
@@user-zn8fm8kw5v It does have to do with the tutorial. He's just trying to be helpful for people who have that specific problem.
What does ‘var’ stand for?
@@saxenart It makes the variable a local variable that gets destroyed after the function/event is over. In addition, you can't access the variable outside of the function/event. You technically never need to use them, but did it here anyways.
If you have anymore questions, or need me to clarify something, I'd be happy to answer!
If anyone is experiencing the shadows jittering when using a camera object, it's because the x and y values for drawing the sprite are being set as decimal values (I think!). To fix this try using the 'round' function, like this: var viewX = round(camera_get_view_x(view_camera[0])); And the same goes for the viewY.
Yes this could cause a jitter, especially for low rez games that might have already set up their own camera functionality. Thanks for posting this and your solution :)
@@FriendlyCosmonaut Your videos are incredible and have helped me so much to learn about programming, thank you!
Thank you so much for posting this, today when I tested my room in full screen I realized I had some jittering in the shadows while moving my character around, I was the whole day trying to figure out what was it and this fixed it. Thanks!
It's hard to believe you're a new channel; your content is on-par with some of the best GM tutorials out there. Not a knock on them; on the contrary-you're just really good.
Haha, thank you! That's quite a compliment :)
OMG, you are so awesome. I am trying to make a top-down 2d game similar to early pokemon games on game boy advanced. I am thinking of making dynamic day and night system which requires constant changing shadows and your tutorial definitely helps me on that. Thanks a lot.
this looks amazing, I had never thought of doing something like this, ill keep this in mind when I am ready to add extra flair to my existing project.
me too i've been playing with adding *image_xscale to the sx var but nothing so far
sorry mate wrong person
This is a great video for sure! However, if there's anyone out there who doesn't want to hard code a system like this you can use Construct 2 for this kind of effect. To achieve it, Construct 2 comes with objects that do this sort of thing automatically. Instead of typing lines of code (which doesn't look like much here) you can just add the behavior to specific objects with just drag and drop features in C2, and adjust the settings accordingly.
I hope this helped! Again, wonderful video! I've been trying to learn this myself and this is a rare tutorial to find. Most of the time I found something like this was just simply demos with no instruction. You're great!
Thanks Branden! Construct 2 is a great engine for beginners too. Little features like this can add a lot of depth to a game.
You're most welcome! I love Game Maker Studio 2 and Construct 2 since I've used them both. They're great for their own reasons. :D I also agree that it adds a lot of depth to a game.
This tutorial is worth it's weight in gold! Thank you!
You are amazing! Thank you for this
The ultimate and the best shadow tutorial indeed. Thank you 3000.
Superb tutorial, thank you! - I was trying to get my head around using surfaces for shadows, and this was really easy to follow - sorted! - thanks again!
If your sprite's canvas is larger than your sprite itself, it'll give you a much larger number than you'll want. In that case, ,use (sprite_get_width(mask_index) * image_xscale)/2 instead. It'll give you the collision mask of the sprite instead.
Great tip!
This is such a good tutorial! You present excellently the topic and dive just deep enough to get things going!
Found your channel by it being recommended after watching Heartbeast and Shaun Spalding. Amazing stuff you've got here. Thanks for the explanation! No more elliptical shadows for me! ;)
Thank you so much! For some reason, I haven't thought about "with" statement. I was trying to forse every object to draw in the shadow surface, after which obj_shadow draws the surface, but because the room in my game is giant and there's a lot of objects game was lagging. But using "with" it doesn't lag!
You're the best! It is clearly all that I need! Thank you.
Another great tutorial. Thanks for making these. (your other GMS2 tutorials are very well done too!)
Could you make a tutorial about how to make an inventory system like the ones in Pokemon or Final Fantasy? It's really hard to find nice, well-explained videos on it. Keep up the great work on the videos!
Yes! This is definitely on the to-do list for future videos.
Were you able to find any tutorials on that? I've been looking for one for a little while now. While i love Coamonauts videos on menus those are mostly for start menu types.
Awesome tutorial! I’m having a problem, though.
I want the shadows to face away from an object as if it was casting light. However, I haven’t really been able to get my head around it. Any help?
The shadow is a little bit jittery for me is there any way to make it more solid?
I'm having this same issue, I managed to create a "static" ellipse shadow for my objects, although when I move it looks a bit jittery. I'm guessing this is because every time I move the shadow is being erased and redrawn in the same position, which makes it look jittery. I'm unsure if something can be done.
*Edit* : Don't think you'll need it anymore but the solution to fix it is in the comments below, you have to use this variables instead: var ViewX = round(camera_get_view_x(view_camera[0]));
var ViewY = round(camera_get_view_y(view_camera[0]));
The reason is because the x and y values for drawing the sprite are being set as decimal values.
I noticed the shadows don't transform depending on the xscale / yscale of the object. Is there a way to fix this?
me too i've been playing with adding *image_xscale to the sx var but nothing so far
I love your tutorials! Thank you so much.
This was invaluable!
Thanks a bunch, this was a very helpful video. I managed to add a bit of code to allow the shadows to move realistically when the player jumps! Basically, I have two variables in my player object, one called 'z', which tracks the player's position above the ground, and one called 'yfloor' which tracks the point on the ground which the player is floating above when he jumps. So in the 8 arguments for the draw_sprite_pos function, I changed all mentions of 'y' to 'yfloor', and I subtracted 'z' from each argument. To make it more accurate, I subtracted '(z/2)' in the y position arguments instead of just 'z'. The main thing I had to change is that I had to add the 'z' and 'yfloor' variables to objects that didn't use them already.
Ajudou muito! Estava procurando um tutorial assim há muito tempo.
This was very helpful and easy to understand thanks to you 5 out 5
Amazing! I'm using this in the game that I'm developing. Corpsenia - Parasite Slayer
What a fantastic tutorial. Thank you.
Beautiful description
Really liking your tutorials and your measured, clear explanations of the principles. Only think I'd really like is that when you typing out or going through lines of code, even on full screen the text is pretty small. I can just about read it if I get close enough to the screen (apologies, I'm short sighted), but would love it if you could figure out a way of zooming in a little so it's clearer from a regular distance.
Thank you! Yes in my latest videos I've taken up the habit of increasing the text size in the videos, but unfortunately a few of the older ones are like this.
I'm not sure if @friendlyCosmonaut you are following the comments at all but I have a few questions.
1. If I draw the shadow on a character,npc, player object etc. the shadow will change depending in which direction my toon is facing. So the shadow falls left if my character or NPC looks left or it falls right when I or the NPC looks left. Of course the simplest workaround is to use a different shadow ( for example the simple eclipse shadow under the character ) but I am wondering if this can be fixed with code ?
2. I would like for the shadows to cast shadows also on object "below" the shadow. If my character walks under a tree where the shadow falls I would him to also get the same color as the shadow, the same for the stone which is under tree and has a shadow on its own. Is that possible ? Please keep in mind that I do not use priority to determine the depth, I use a simple line of code in the step event in the parent object : depth = -bbox_bottom; . So I already tried to move the instance layer above the player but it doesn't really work.
Do you maybe know or have had similar issues ? :
Thank you for showing me this! I do have one question though: If I want to make the height/length of the shadow relative to the height of the object like in real life (taller tree, taller shadow). Can I still use this same method or do I have to make the shadows manually as part of the sprite as I have always done before?
Thought it looked kind of funky how the small bushes had the same size shadow as the buildings etc.
This is probably way too late but if you wanted to keep things simple you could probably just have multiple parents for different height groups (short, medium, tall, extra tall) and adjusted the variable accordingly, then set the objects' parents to whatever one you felt fit best.
edit: Now that I think about it you could probably also just do some simple maths when setting the shadows length/height by doing something along the lines of, say, var sx = sprite_width
* n where n is any random number you feel like putting there (experiment with it). Keep in mind I haven't tested this out so you may need to delve a little deeper into it to get it working, but you get the idea.
Those using GMS1.4 can substitute the function "gpu_set_fog" with "d3d_set_fog".
and the camera_get_view_x()? how?
this works, though the shadow won't invert its scale when the reference object does (and understandably so!)
Problem is, I can't figure out how to invert its scale since all of the "drawing" part happens in a single function, so its abstracted from me
dudas se podra para GMstudio sip sip utilizo surface ... excelente sus videos son geniales siga haciendo mas videos en game maker ....
This helped with my mario maker type game :thumbsup:
Nice tutorial, done something similar in the lighting system I have. I am currently trying to work out how to change the shadow skew when the shadow hits a "vertical" surface, ie the shadow stretches across the "horizontal" surface and skews when it hits the "vertical" all dependant on "light" position. Currently thinking about using another surface to draw the altered shadow, but currently struggling to conceptualise how to detect when this should happen, any pointers or ideas welcome.
Ah, this probably won't be the easiest thing to do. I'd suggest taking a look at primitives or vertex shaders for this, as you'll want to control the "vertices" of how the shadow sprite gets drawn. Unfortunately I don't know of any tutorials that cover this sort of thing so I can't point you anywhere other than the documentation for primitives/shaders!
Thanks for this.
I've noticed that the draw_sprite_pos function defines the BOUNDING BOX of the sprite to draw. Because my sprites don't occupy the whole of the image size, my shadows ended up wider than they should have been. Modifying the points to have "bbox_left" rather than x-sprite_width/2, etc. fixed this issue.
Nice resolution, thanks for sharing!
Awesome video. I'm currently working through your farming tutorial as well. I've just finished the day night cycle. Everything is working fine. You started to go into how to add this to a day night cycle, but I'm wondering how would you add this to your day night cycle?
Thanks for the tutorial! Do you know if there is any way to get this method to work using vector images (swf files) as draw-pos does not support them?
Draw-ext doesn't look nearly as good and it would be a bit tedious to create a shadow version (non swf) of each image with animation.
caramba, voce salvou minha vida
Brilliant, thanks! Could you please also do a tutorial on cameras and views. :D
fantastic videos btw!! you're sure to get lots of views and subscribers in the next few years
Thank you! I can't seem to find your other comment so I will reply here:
Yes you could have your sprites centred. In the parent object, setup a variable called y_offset = sprite_yoffset. That way, all the children will set this variable in their create event.
Then in the draw object, instead of draw_self(), use draw_sprite(sprite_index, image_index, x, y+y_offset);
thanks! i figured it out while tinkering which is why i deleted it ^_^
Ah no worries then. Thank you!! :)
Is there any difference from using lengthdir_x and lengthdir_y instead of (room_width/2)-mouse_x and (room_height/2)-mouse_y. I threw in lengthdir just because i'm used to using it and got about the same results but does it put more stress on the game?
Huh, no, I didn't actually think of it at the time (I sort of just threw that line of code in there as an example). Vector stuff is probably the easier way to go, so feel free to use it! I don't think there'd be much of a difference to performance one way or the other.
You're a wizard.
very helpful
Hey friendlycosmonaut, amazing tutorial as usual, however how do i mix the shadows together with the depth system? I can't seem to get it to work, where do i put what etc.
I figuered it out, lol xD
So you set your objects as your children in your "obj_parent_xy", right? But you are NOT allowed to have multiple "obj_parents_xx" with the same "obj_children". Thats why you have to create only one "obj_parent" with your children-objects and replace the "obj_parent_shadow" and "obj_parent_depth" in your code with your new single obj_parent.
i hope you understand me.
Your tutorials are amazing
Great tutorial! Keep it up!
This is awesome! Question: How would you apply this to tilemap layers instead of child objects? Is that doable?
You mean apply shadows to tilemap "objects" like a house made of tiles? Ooh, that'd be tricky. I wouldn't say it's not doable -- everything is if you throw enough code at it -- but I'm not sure it'd be worth the hassle. Perhaps it's just beyond me; if anyone else has ideas, I'd love to hear them!
Yeah I was thinking how cool it would be to have a tile layer and any tiles that were placed on that layer generated shadows in a similar way your system does. Instead of using actual objects under a parent. I'm doing all tile collisions and trying to reduce as many objects that I don't need.
This is cool, but there is still an issue. What if the shadow being casted needs to be drawn over the player or another object? For instance, if the player is standing under a tree, obviously the tree's shadow should be drawn over the player, but if the player is standing under a rock, then the player should not be affected because the rock's shadow is under them. Do I just have to draw those specific shadows on a separate layer?
Great tutorial! I've got it working for the most part. I have a z-axis in my game, and I'm trying to make the shadow shrink as I jump. I have a zsp that determines my height. Any idea how I would implement an equation that could somehow use my zsp and shadow size to shrink/grow as I am in the air? I know it's asking alot, so don't feel pressured to answer that lol.
i have one issue not about the surfaces itself but the way my game is set up i have a z axis and you can jump on stuff with a higher z and i have the shadow follow the players z_floor and since the surface for the shadows are drawn behind everything they arent seen when i jump on a platform because its behind the platform sprite. I hope i explained that right
Great video!
Thank you for the amazing tutorial! i have one question tho. My sprites do not fill the entire width of the sprite (with this i mean, the guy i'm trying to draw the shadow of, has some empty space on each side to later animate sword animations etc). This makes the shadow of the person im drawing really wide, any ideas?
Hmm! As you probably know, this is happening because of our use of "sprite_width" (or sprite_width/2, anyway). Instead of using that, we could make up a variable called "shadow_width" and declare it in the create event of the parent. As it's default value, we'll make it shadow_width = sprite_width, and we can just leave it at that for objects and sprites that don't have this problem (the children will inherit this value). But for children that you want to tweak the shadow width, you can go to their create events and redefine the value as something else -- it will overwrite what it got from its parent.
Note: if you have any objects that already have a create event, but you want them to inherit shadow_width, you'll have to put "event_inherited()" in their create event.
Alright, thanks a lot! Just one other question, in my game i move the camera around with my mouse, and i have a zoom function as well. When zooming out, the shadows won'' draw the way i intend to. Is there maybe a way to contact you so i could show you?
never mind, i fixed the problem!
Haha, well done :)!
Very nice, not perfect shadows, but then who needs perfect when good enough does. :)
Though I am curious as to what kind of programming is needed for absolutely pixel perfect shadows, such that if they hit another object they'll climb up it properly and stuff like that.
3D programming.
Fantastic tutorial, allowed me to do simple ground shadows to add that little bit of graphical flavor, and I really appreciate you teaching what each line of code's purpose is, very informative.
I know the video's a bit old at this point, but was wondering if there was a way to reverse the image_xscale of the shadow when the object's sprite_index's image_xscale is flipped. I feel like the answer is simple but I can't seem to figure it out. Thanks again, cheers.
Actually this should be happening automatically with the function draw_self(). But you could use draw_sprite_ext(sprite_index, image_index, etc) and plug in all of the instance's variables to be sure.
Unfortunately it didn't do it itself, I would have thought it would do it automatically as well but for an odd reason it always draws the xscale = 1 version. However I did find a solution, which was to interchange the x1 and x2 with each other if the obj's image_xscale = -1, so the sprite's specific corners are flipped therefore flipping the sprite. I appreciate the reply though, thank you.
Oh, doh! I was mis-remembering this video. Of course, draw_sprite_pos was just stretching the co-ordinates to a scale of 1! You found the perfect solution, well done :)
Everything works except this one! I just realized same problem too. Static objects are fine but with animated objects(like a player or enemy) it always shows xscale = 1 on shadows. I'm a beginner, How exactly do i fix it?
Also, i'm using draw_sprite_ext(); because i don't need to skew or anything.
Keep up the good work FC. :)
Lovely video, if there anyway to get this to work in GM 1.4? the gpu_set_fog doesn't exist, but I looked in the manual and there is a "gm_FogColour" and "gm_PS_FogEnabled." Also, how would I make a shadow for an object using a layered-sprite fake 3D?
Hi, is there an equivalent function to camera_get_view_x(view_camera[0]); in order to make this work on Game Maker Studio 1.4? Thank you.
Could you please make a follow up tutorial on the last part before the video ended, with the shadows rotating throughout a day cycle?? This is really what I want but I can't figure it out...
i know it a bit late and you figured it out, but this is for other people who see your comment. in a create event of an object write alarm[0] = 700; (this should change depending on the day speed). then in the alarm[0] event write obj_shadow.cx ++; obj_shadow.cx ++;. that should work, but i didnt test it
Fantastic.
top video!
Does this work with 2.3 ? I downloaded your game file but it just shows a black screen .. it did had to convert a few things but not sure
Thanks for these! I have a question, why doesn't it work with a single object instead of two? I mean, at 13:50 you create a shadow object with the code from the parent object, then set them to the layer. Why doesn't it work if you only have the obj_parent with the code itself and assigned it to a layer?
This is a great question! The short story is: you don't "have" to create the second object... but doing so means the system does a lot less work then it has to.
Longer story: if we put all the code in the parent object, that means every single child would be running every single line of code. That means we'd be having to check that the surface exists in every child, having to turn on the fog, and then drawing the surface multiple times. As we create more and more instances in the room (tens, perhaps over a hundred), you can see how costly this would all get!
I know that it may seem a little excessive to have two abstract-seeming objects for this system, but having lots of object parents is very much the norm, and an essential part of object management/hierarchies/organisation. It helps to think of this in the context of a larger, established game. Let's pretend we have dozens of objects. NPCs, dogs, cats, trees, chairs, fires, stars, food, health, potions, plants... the list goes on and on. In such a case, it will make sense to have various parents for those objects (enemies, friendlies, furniture, light sources). So this "obj_parent" in the video would just be something at the top of the hierarchy, that links together everything with a shadow. Whereas, obj_shadows is not a "parent" object, it's an object that does work - it draws all the shadows.
I hope that makes sense and puts things into perspective!
Awesome! Thank you =)
Another great tutorial, I am combining this with the depth system but once I set the children to the obj_shadow the obj_dpeth code stops working, I suppose that it overwrites it. Should I mix the two of them together in one object? The problem is that I may have an object with no shadow and depth system. Thanks
Oh and also, I'm having problems with drawing shadows on sprites that are drawn in a canvas a bit bigger than them, I mean that there's a bit of leftover pixels in the sprite wich are transparent. An example is my main character, it's drawn in a 32 * 32 square but it logically doesent occupy the whole thing. The problem comes when drawing the shadow (I removed the X offset to solve this bug), it draws a shadow 32 * 32 shadow with the shape of the sprite, it deforms the sprite to fit the shape of the canvas wich is pretty annoying. I suppose that the function "sprite width" counts the width of the sprite as the canvas width, the sqare where the sprite is drawn, but the function you use to draw the deformed shadow counts the four points only where it is drawn. How could I fix that, do I relly have to make all rthe "canvases" fit the size of my sprite? Thanks a lot for your dedication
Hey Carles, sorry for the late reply - yes once you make the objects children of one object and not the other they'll stop inheriting from it. You might have to make the depth object a child of the shadow or vice versa. Then you could have a toggle for whether to draw the object as part of the depth/shadow, like "depthObject" and "shadowObject". These would be set in the parent as true by default (all the children will inherit this), and you can manually go into objects you don't want to have as part of either system and mark them as false. Then, have a check in the draw code to exclude any object that has those variables marked as false from the drawing process.
As for the width issue - you could put in a special case for those objects but yeah the simplest thing to do here is to remove the "whitespace" from the sprite, as annoying as that is!
Hope that makes sense!
Awesome! Thank you, thank you, thank you!!! =D
You're very welcome!
Brilliant!
Hmm, does gpu_set_fog not work on HTML5 Targets? Great Video!
awesome tutorial!
but shouldn't image blend work for making the shadow black?
Not for the later section where we use draw_sprite_pos -- there's no way to set a "blend" for this, so we have to do it ourselves with the gpu_set_fog. You could also use a simple shader to accomplish the same thing.
Hi! This was great, do you have any tips on how to do indoors lightning? I'm trying to make it so that I can have the shadows point away from an invisible object in my room, the way a ceiling lamp would make things cast a shadow in the opposite direction from it. I've played around to try and fix it on my own, but I can't get it to work. I want the skew of every object to be different depending on where they are in relation to my light source, but instead every object has the same skew as my player object. Also, I'm not sure how to make the skew of my shadow change every time my player moves.
PS: I'm a complete noob.
Thank you! Ah, to be honest, this probably requires a lot of fiddling and a good deal of math (vectors). It sounds like instead of a set skew, you'll want to be getting the direction and distance objects are from the light source, and then change the skew accordingly. So, you'll want to be using the functions point_direction() and point_distance(). Definitely have a look at those in the documentation (you can type them in to a code block and then middle click to bring up the appropriate page). You'll also probably need the lengthdir_x and lengthdir_y functions, as you'll want to be creating vectors the shadow should follow, extending from the object.
Essentially, what you'll need to do is get the point_distance and point_direction of the light source to an object. Then, you'll want to translate the "point_direction" into horizontal and vertical components with the lengthdir_x/y functions. The length of these components should depend on the point_distance. Once that's all calculated, you can use this information to form where the shadow should be located.
This is a little "advanced" if you've never met with these concepts before, but to be honest, it would make a great beginner exercise if you can get your head around all the functions and vector concepts.
Hi! Thanks for the helpful response, I messed around with it for a while and kind of got it to work, but I ended up scrapping it because I felt like It'd have to be far more complex and way beyond my skill level to get it to work the way it should. Here's a screenshot to explain the problem imgur.com/a/9Gfbm, it should have been obvious to me that this method wouldn't work when the shadow is in front of instead of behind the object, because you'd need the shadows to "extrude" from 3 points instead of just 2 (that's my best attempt to explain it in words, haha).
Also, THANK YOU for the inventory system tutorial. I've had a literal massive headache trying to find a guide for that and yours is the only one that made any sense to my noob brain.
Very nice tutorial. I tried it out myself, however the draw_sprite_pos( ); just won't work for me. I've tried everything that's to my knowledge and I also tested if other draw_ methods would work (they did) but draw_sprite_pos won't generate a sprite for me. Do you have any clue why this could be happening?
Hmm, nothing comes to mind. If other functions are working but this one isn't, I can only guess that the co-ordinates that you're plugging in may not be right? You could be putting them in the wrong order. It should be top left (x,y), top right, bottom right, bottom left.
Thanks for the reply. I think I messed up the order of the coordinates or something. Didn't catch that when I was testing most likely since I've tried it multiple times with different sprites and on different projects. It's working fine now, thank you for helping out!
Love your content btw, been watching your videos for that past few days and it's been really helpful and (more important) insightful!
The shadows don't seem to play nice with the depth video that you did. I either have shadows or they don't change depths. Any idea what I could do?
I'm away from my computer for a couple days so unfortunately can't test this. But I believe the problem is arising because we can't have an object have two parents. We can however do the next best thing: make one of those parents a child of the other parent. So, make the parent object from the shadow tutorial (obj_parent) a child of the parent object from the depth tutorial (obj_parent_depth).
Let me know if this works -- otherwise, there's a couple more things I can test once I'm at my desktop.
It does work :D Thank you!
Yay! :)
nice tutorial, however, i'm trying this on game-maker studio 1 and the function "gpu_set_fog" doesn't exists so i cant make my shadows black! Do you happen to know a workaround for this?
Yeah the function you'll want is d3d_set_fog. Here it is in the docs: docs.yoyogames.com/source/dadiospice/002_reference/drawing/drawing%203d/3d%20lighting%20and%20fog/d3d_set_fog.html
thanks a lot! Your tutorials are super informative and well made for how new of a channel you are, keep up the great work!
No worries, thanks Harvey! :)
@Harvey Rupp Hi, did you got it working on game maker studio?, can you tell wich function did you use to replace camera_get_view_x? Thanks
Hey Friendly Cosmonaut! I love your tutorials and I get through them very quickly, but I get stuck after this one.
I really want to make a day/night cycle with the shadows based on the time of the day like said it in your video.
I made a simple time system(not optimized at all but working) and a surface darkening when dusk comes and lightening when dawn comes, but I am struggling with the shadows.
Could you please help me a little with what maths I have to use for the skew?
Hey, thanks! It depends on what you want the shadows to do exactly. Could you describe what you are imagining happen to the shadows as the day progresses?
I would like to have shadows following the path of the sun, with a dawn, were the shadows are long and going from the object to the east(room's right), at noon the shadows are inexistant (or not long at all and going to the north) and a dusk were the shadows are also long but going to the east.
All of this dynamically and progressively, with a little shadow_height to have the impression of the sun being above the camera.
Ah okay. I might do a follow-up video and include something like this. For now, I've tossed together some images that should help you out: i.imgur.com/7pMGjrM.png
The result of what it should look like is this: i.imgur.com/A3sWwyd.gif
Obviously tweak variables as needed. Personally I liked the look of going south -> north, but you could just increase the skewX more to do it the other way around.
Thank you a lot for your help. I am moving towards the CBNA Smart Light engine who manages lights, shadows and time way better than everything I can do but I will improve my code with your example to finish it, maybe if I want to change later.
No worries! Good luck.
what are they player things? theyre so cute? are they in a game youre making?
Haha no, they were just simple assets I threw together for these tutorials. But thank you!
to bad, they look really cool. it should be a game ^.^
So my sprite area is bigger than the sprite it self. When going vertically the shadow changes size and I think it is because the animation does something, but I don't know what. I already set new variables to replace the sprite_width. Does anybody know what is the problem?
Well, my animation is first 4 pixels wide and it changes in walk animation to 6. And my sprite canvas (or whatever it is called) is 16 pixels. So should I change it so every time when it is at this certain animation and also every time when image_index is at the certain pictures then change the sprite_width. Or is there any easier way to do this?
I'm not 100% sure I'm understanding you. Could I see a gif/video/picture of what's happening?
FriendlyCosmonaut Yes I will link it here later cause I can't be on my computer right now
I see the problem, I think -- when the player is on the side and the shadow's width appears greater than the player. I'm not entirely sure but I think it has to do with your sprite sizes, and drawing "empty space". Could you reduce the canvas size so that it's 6 pixels wide (so that the player picture "fits perfectly" inside it)?
What if the char jumps?
Great tutorial! Thank you! I'm wondering why do you use gpu_set_fog instead of draw_set_colour?:)
The draw_set_colour wouldn't actually work in this case, as the draw_sprite_pos will ignore all those draw_set settings. It just draws the "raw" sprite. So we have to take a different tact - the gpu_set_fog changes how the GPU is renders the sprite.
How could I make it so the shadows are relative to a light object? So the shadows move away from an object instead of the mouse? Great tutorials :)
So kind of like how I've done my mouse stuff, visible at 20:50, but instead of mouse_x and mouse_y, you'd grab obj_light.x and obj_light.y. Then you'd just flip the minuses to pluses so the shadows go away and not towards it.
Hi there. Fantastic video. I've been trying something similar (using a light object).
With the code below, I can get it to lock onto whatever the nearest light object is, and it adjusts to character position and looks great:
var light = instance_nearest(obj_parent.x,obj_parent.y,obj_light);
var sx = light.x - lerp(light.x, obj_parent.x, 5);
var sy = light.y - lerp(light.y, obj_parent.y, 5);
However... my main issue is that while it works... the shadows are doing the opposite of what they would in real life. The closer the parent gets to the light source, the smaller the shadows get and the further away, the bigger(times infinity). I'd love to flip it so that it is closer to reality, I figure it must have something to do with the y values in the 'draw_sprite_pos' but I've been banging my head and can't for the life of me figure it out. If you have any ideas, that'd rock! Regardless, love the channel, keep it up!
Do you mean like this:
var sx = room_width/2+instance_nearest(x, y, o_light);
var sy = room_height/2+instance_nearest(x, y, o_light);
still not working though lol
Thanks for your help :)
cool thanks, what is your obj_parent object reffering too?
Thanks Hamish. The answer is: fiddly maths, where we make the shadows LARGER when the distance between the objects gets SMALLER. At the moment I'm using the reciprocal (1/distance) to do this, but it needs some other magic factors. I'm just madly finishing up a game for the low rez game jam (finishes in a few days), but after that, I'll probably make a video on this. I was playing around with it and got it working with multiple light sources too. But I'm not too happy with my "fiddly math" yet (the shadows not growing at a "rate" I would like), so still have to tweak it.
Thanks :3
Really should've used shaders for that, it's not optimal
The finished project link doesn't work for me ;-;
Why si the NPC walking over the player character?
does this work with gamemaker studio 1.4
sos una geeniiiiaaa!! XD
i am getting an error "trying to draw non-existant sprite" when Im using the second option (all shadows will be drawn with a single object)
Joshua Auvia Patricia Pagcaliwangan I had the same issue but I worked it out, the object I was referring to with the ‘with’ statement didn’t have all the other objects as children. I’m trying to use this in conjunction with the depth tutorials.
For some reason this caused a fatal error in the code from the updated depthgrird tutorial.
Aaaaaand now it doesn't run at all after I removed this code, even though I didn't even touch the depthgrid code and this is the only code I added or even opened. Coding is "awesome".
Yeah there are some pretty significant changes between the two videos... even the smallest change/mistake can mean the system doesn't work. I can't really guess the error without seeing your code -- I'd suggest having a look at the finished project's code (link is in the description) and comparing it with your own.
Don't give up! Programming is fiddly work :)