How can I draw a shadow under my character?

I created a 2D tile-based jump and run game for Windows Phone and I want to draw a shadow under the character. How can I do that? The shadow must also be drawn on the tiles if the character jumps. The character and the tiles are rectangles.

How can I draw a shadow under my character?

The shadow should look similar to the shadows in this picture:

How can I draw a shadow under my character?

UPDATE: I tried to solve the problem like described in Gamefreak0's answer, but it's not working. I created a ShadowChecker object and I check in a foreach-loop if it collides with a platform. If it collides, the variable ShadowPosition gets changed. But it's not working correctly. I made a picture, and in this picture you see that the shadow isn't on a platform. Why is the shadow not getting drawn on the pink platform?

The character(black rectangle) is above the pink platform, so the shadow should be on the pink platform.

How can I draw a shadow under my character?

I update the shadow's position with this code:

 protected override void Update(GameTime gameTime)     {                  Rectangle CollisionRect = new Rectangle(-1000, -1000, 0, 0);         ShadowChecker = CharacterRect;          //FloorRect is the brown rectangle at the bottom. There is no other platform under the FloorRect.         while ((CollisionRect.Height == 0) && (ShadowChecker.Bottom <= (FloorRect.Y + FloorRect.Height / 2)))         {             ShadowChecker.Y += 1;             foreach (Rectangle r in PlatformRectangles)             {                 if (ShadowChecker.Intersects(r))                 {                     CollisionRect = r;                     break;                 }             }         }         if (CollisionRect.Height != 0)         {             // Draw the shadow on this position             ShadowPosition = new Vector2(CharacterRect.X, CollisionRect.Y - CollisionRect.Height / 2);         }          base.Update(gameTime);     } 


Try this approach:
Spawn a shadow object on the X and Y position of the player, then apply a very strong gravity force, and check for collisions (just like you do with the real player)
Then on the next frame update the coordinates of the shadow object with the new X and Y cooordinates of the player (I.E. place it on the same coordinates of the player) Then apply again gravity, etc.
It is important that you set a strong enough gravity, so that you're sure that your shadow does not float mid-air.

Draw a shadow sprite like you draw your character before the character (to make it appear behind him). Make sure its X position is updated according to the character, but the Y position stays on the tile. You have got a shadow.

It may not be the best answer but,

Make an object called ShadowChecker that constantly draws a rectangle/line below the player. Use collision detection to adjust the height of the rectangle/line until it's not colliding with any object besides the player AND there is a platform just below the collision box. Then check the Y position just below the ShadowChecker, if there is a platform there, that's where you draw the shadow, if there isn't, don't draw the shadow. (the ShadowChecker's collision box is probably still updating if this happens)

This probably isn't the best answer but it'll give you some results.

EDIT: Method 2: Expanding on Predicting Collision Detection

Since the first method didn't work you'll have to create a method that does something similar to Collision Prediction. Here's a good article on it. Your method just needs to look only at the player's Y coordinate and always be looking down. Once you've figured out when the collision happens you know where it happens so you can draw the shadow there. If this doesn't work I would suggest Riccardo Vailati's answer.

Alright, so an easyish change to your algorithm will make it both neater and probably work better:

  1. You use ShadowChecker as Character rect that you move downwards. Instead, expand it's height to either infinity or the bottom of the screen (FloorRect.Y).
  2. After expanding it, loop through the platforms:
        float currentHighestY = ShadowChecker.Y;
        foreach (Rectangle r in PlatformRectangles)
            if (ShadowChecker.Intersects(r) && r.Y < currentHighestY)
                currentHighestY = r.Y;
                CollisionRect = r;
  3. i.e. You're picking the "highest" intersecting platform and you're only looping through the platforms once.
  4. Next step after that is to limit the amount of platforms you check with, but I think that's probably out of the scope.

Another point that will help you more than anything ever has is: you should use debugger to figure these things out. While you're running your app in debug mode, put a break point at the start of the loop by pressing F9. After that, go step by step with F10 until you figure out what the problem is.

In this case, you might specifically want to put a break point on the

if (ShadowChecker.Intersects(r))

And see what the values of ShadowChecker and r are at the moment and why this returns true. This should take you closer to the root cause of your problem.

Category: xna Time: 2014-03-24 Views: 2

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.190 (s). 12 q(s)