Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm adding movable crates to my game and I need some help with my movement code. The way I've set up my movement code the crate's X and Y are moved according to a vector2 unless it hits a wall. Here's the movement code:

if (frameCount % (delay / 2) == 0)
        {
            for (int i = 0; i < Math.Abs(cSpeed.X); i++)
            {
                if (!Level.PlayerHit(new Rectangle(crateBounds.X + (Math.Sign(cSpeed.X) * 32), crateBounds.Y, crateBounds.Width, crateBounds.Height)))
                {
                    if (!Level.CollideTiles(crateBounds.X + (Math.Sign(cSpeed.X) * 32), crateBounds.Y, crateBounds.Width, crateBounds.Height))
                    {
                        if (cSpeed.X != 0)
                        {
                            crateBounds.X += Math.Sign(cSpeed.X);
                        }
                        else
                        {
                            Equalize(2);
                        }
                    }
                    else
                    {
                        cSpeed.X = 0f;
                    }
                }
                else
                {
                    if (!Level.CollideTiles(crateBounds.X - (Math.Sign(cSpeed.X) * 32), crateBounds.Y, crateBounds.Width, crateBounds.Height))
                    {
                        if (cSpeed.X != 0)
                        {
                            crateBounds.X -= Math.Sign(cSpeed.X);
                        }
                        else
                        {
                            Equalize(2);
                        }
                    }
                    else
                    {
                        cSpeed.X = 0f;
                    }
                }
            }

            for (int i = 0; i < Math.Abs(cSpeed.Y); i++)
            {
                if (!Level.PlayerHit(new Rectangle(crateBounds.X, crateBounds.Y + Math.Sign(cSpeed.Y), crateBounds.Width, crateBounds.Height)))
                {
                    if (!Level.CollideTiles(crateBounds.X, crateBounds.Y + Math.Sign(cSpeed.Y), crateBounds.Width, crateBounds.Height))
                    {
                        crateBounds.Y += Math.Sign(cSpeed.Y);
                    }
                    else
                    {
                        cSpeed.Y = 0f;
                    }
                }
                else
                {
                    if (!Level.CollideTiles(crateBounds.X, crateBounds.Y - Math.Sign(cSpeed.Y), crateBounds.Width, crateBounds.Height))
                    {
                        crateBounds.Y -= Math.Sign(cSpeed.Y);
                    }
                    else
                    {
                        cSpeed.Y = 0f;
                    }
                }
            }
        }

The frameCount and delay variables just slow down the movement somewhat. Anyway, I've added a tool to my game that acts as a gravity well (drawing objects into it's center; the closer they get to the center the faster they go) and what I'm trying to do is make it so that the crate will bounce off the player (and vice versa) when they collide. Thus far, my code only keeps the crate and player from getting stuck inside each other (the player can still get stuck under the crate, but I'll fix that later.) So what I'd like to know is how I can best make the crate bounce off the player.

The other movement problem I'm having is related to another tool which allows the player to pick up crates and move around with them. The problem is that the crate's movement while being carried isn't tied to the movement script (it moves the crate itself, instead of adding to speed), which makes the crate go through walls and such. I know what I have to do: make the crate's speed match the player's speed while the player is holding it, but I need the crate to snap to a certain position (just in front of the player) when the player grabs it and it needs to switch to another position (just in front of the player on the other side) when they player faces the other direction while holding it. What would be the best way to make it switch places while keeping all the movement tied to the speed vector?

share|improve this question
    
what Equalize does? –  FxIII Jun 24 '11 at 7:10
    
Equalize adds/subtracts from speed until it equals 0. –  IronGiraffe Jun 24 '11 at 9:39
add comment

1 Answer

You are doing too much things togheter. You see, five indentations level are too much. Now you should add even a sixth... I think your aproach is wrong and it can lead you only in an unmantainable code.

Well, enought criticism: lets work.

  1. you should work in each frame (do less things but do them).
  2. model things with a common base (both player and crates are solid objects with a position and an extent)
  3. model dynamics for the common base, Do not code it into this or that object but do it in another function/class (class is better).
  4. try to apply your position variation at once in a frame (not in a for loop): you will miss a collision only if you are travel very fast.
  5. vector come from Latin vector "one who carries or conveys, carrier" so NEWPOS = OLDPOS + VECTOR ( a definition for vector is V = B - A so you can use V to go from A to B)

If you do so, you can add a status to your crate so it can be FREE or PICKED, you change this status by the methods drop() and pickup(player): when you drop the crate's dynamic become the free status dynamic; when player picks up the crate, the crate will ask the player to give it the new dynamic it have to obey.

At each frame you:

  • update each common-base-objects current dynamic step that will change the common-base-object position accordly;
  • do the collision detection for each type - looking to the current status if needed and varing the dynamics parameters on collisions
  • update your scene according to the updated objects status.

Doing so you decouple things and factor common behaviours without explore each possibility in a single deep tree of nested ifs and fors.

Comments are appreciated

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.