• Godot Help
  • Simulated shadow in a 2D top-down perspective

I am attempting to emulate shadows in a 2D top-down perspective. I have an airplane that I am controlling that hovers at some height above the ground. In order to represent that, I want to emulate a shadow of it on the ground. My initial idea was the create a Sprite2D node that is offset from the airplane's main sprite in the y-direction.

However, I realized that since my shadow sprite is a child node of the airplane, when my airplane rotates, the shadow, instead of staying in place and rotating along with my main sprite, it revolves around the airplane's center as axis.


Is there any way to only have the sprite rotate along with my plane's sprite and not around it? Something that does not involve modifying the sprite's file.

    rohitchaoji One simple solution is to manually update the shadow position each frame and set its rotation back to 0.

    func _process(delta: float) -> void:
        $Shadow.global_position = global_position + Vector2(0, 400)
        $Shadow.global_rotation = 0

      Toxe

      Thank you! Although I'd like to point out (for the benefit of anyone else who might come across this question) that the $Shadow.global_rotation had to be set to self.global_rotation + (PI/2) for the shadow sprite to be able to rotate along with the plane's sprite. But this is exactly what I was trying to do, thanks!

      • Toxe replied to this.

        rohitchaoji I'd try to employ a RemoteTransform2D node. It would allow to push planes position to another node but ignore rotation. Then shadow can be added to that node and offset.

        I would have the shadow childed directly to the plane with no offset and then put this shader on it 😉

        shader_type canvas_item;
        render_mode skip_vertex_transform, unshaded;
        
        uniform vec2 offset = vec2(0.,128.);
        varying flat vec4 color;
        
        void vertex() {
        	VERTEX = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy + offset;
        	color = COLOR;
        }
        
        void fragment() {
        	float a = texture(TEXTURE, UV).a;
        	COLOR.rgb = color.rgb;
        	COLOR.a = color.a * a;
        }
        • xyz replied to this.

          rohitchaoji Haha you are right! I was in a hurry and copied the code from one of my projects, adjusted the name and vector... and forgot about the rotation. 😉

          award I would have the shadow childed directly to the plane with no offset and then put this shader on it 😉

          Witchcarft!