XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Help! Stuck on per pixel collision sample

Last post 20/03/2009 22:04 by JinJohn. 4 replies.
  • 20/03/2009 17:27

    Help! Stuck on per pixel collision sample

    I'm using a copy of the per pixel collision sample. My code is exactly the same as it is in the sample. The only difference is person is now potion and block is now box. I used those items because they sound similar and if I got lost while adding to them, I could tell them apart in my code.

    Anyway, I eventually plan on changing out my box with ground texture and potion with player character. The problem I'm having is in this method

    // Check collision with person
                    if (IntersectPixels(potionRectangle, potionTextureData,
                                        boxRectangle, boxTextureData))
                    {
                        potionHit = true;
                        potionPosition.X = boxRectangle.Left - potionTexture.Width / 1.5f;
                    }
                    else
                    {
                        potionHit = false;
                    }

    This gets me spot on pixel collision with the left side of box, and keeps the potion out of the box, just like i would want with the character on the ground. My issue is I can't seem to get it to do the same for the Right, Top, and Bottom of the box. If I add those and try again, the potion teleports to left side of the box, I assume because it's the first line in the code. But adding the whole if else if line again with a different location on the box doesn't correct the issue. What am I missing? It's something simple I feel such as a missing keyword.

    Anyone know? I'm not good at explaining code(English not my first language), so if you need it to be clearer, just ask and I'll try to say it in a different way.
  • 20/03/2009 19:44 In reply to

    Re: Help! Stuck on per pixel collision sample

    What are you trying to "do" with the potion, from what I can see from your code  is at follows:

    Player intersects with potion

    Bool Potion = True -  I assume you will use this in order to do something with it later such as "if true" then "potion.color.Red" or something

    Potion is being pushed to the left hand side, as if the player "pushed" it there


    If you added the same statements but for the differant directions it would do the first if statement first, which is what you have stated.


    Are you trying to simulate 4 way directional collision detection? So the player cannot walk through objects, no matter what direction he walks into them?
  • 20/03/2009 20:53 In reply to

    Re: Help! Stuck on per pixel collision sample

    Yes, a bit like that. However, I'm just trying to set up some collision detection and response code so I can make ground and platforms. So in my example, the box resembles the ground and the potion resembles the player. I want the ground/platform to push the player out from any direction it is collided with, but only pushed out the side collided with.
  • 20/03/2009 21:47 In reply to

    Re: Help! Stuck on per pixel collision sample

    Nick Gravelyn, an MVP from Microsoft has provided a detailed tutorial on sprite to sprite collision here:

    http://nickgravelyn.com/archive/#tileengine

    Episode 7 is what you are looking for


    It seems to me as well you are making an RPG, watch all his tutorials if you intend on making one :)

  • 20/03/2009 22:04 In reply to

    Re: Help! Stuck on per pixel collision sample

    Thanks, I've got the source code for that engine and have watched about half of the vids before. I'm actually making a platformer, like a mix of mario and metal slug. What's the difference between how a top-down engine works from a side-view? I will go and watch vid 7 again though. :-D
Page 1 of 1 (5 items) Previous Next