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.

My problem is this, when iceBoy hits any of the newGrounds in the ground.groundAmmount list, it will return the specific position of this newGround so that i can set iceBoy's position based on the newGround that he touched. Here is the code that i have for this:

foreach (Ground newGround in ground.groundAmmount)
{
      if (HitGround())
      {
           iceBoy.position.Y = newGround.position.Y;
      }
}

but like Byte56 this will only loop through all of the newGrounds in the list and then iceBoy.position.Y will only equal the last newGround added to the list. how could i create another method that will have a return value of the position of the exact newGround that iceBoy is touching?

share|improve this question
add comment

closed as not a real question by bummzack, Byte56, Trevor Powell, Josh Petrie, Anko Apr 10 '13 at 10:58

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

The main problem with this code is that you don't know where it hit ground.

foreach (Ground newGround in ground.groundAmmount)
{
      if (HitGround())
      {
           iceBoy.position.Y = newGround.position.Y;
      }
}

So if iceBoy has hit any ground, you'll loop through and change his y position to each newGround block. That's why you're only seeing this: "only worked for the last sprite drawn in the list".

I don't know how your HitGround() function works, but you'll want to modify it in one of the following ways:

  1. Make it take an argument to test against a specific tile.
  2. Make it return the tile that iceBoy hit.

In the first case:

foreach (Ground newGround in ground.groundAmmount)
{
      if (HitGround(newGround))
      {
           iceBoy.position.Y = newGround.position.Y;
      }
}

For the second case:

 Tile newGround = HitGround();
 if (newGround != null)
 {
     iceBoy.position.Y = newGround.position.Y;
 }

Additionally, depending on your drawing origins, you'll want to modify your offsets as Roy T. suggests.

share|improve this answer
add comment

Are you having problems detecting when two sprites touch? Detecting if two rectangle colide is relatively easy, especially if the rectangles don't rotate (eg they are axis-aligned). A good start would be to read this article and to look up the separating axis theorem (which is a fast way to determine if two non-rotated rectangles intersect).

Another problem you might have is that your code, as it is now, makes two sprites overlap. To place one sprite on top of another sprite you should not write this:

iceBoy.position.Y = newGround.position.Y;

But this:

iceBoy.position.Y = newGround.position.Y + iceBoy.dimensions.Y;

share|improve this answer
    
SAT can detect collisions between arbitrary convex polygons. Using SAT for AABB collision detection is overkill, you can have that way easier. –  bummzack Mar 1 '13 at 22:35
    
I think the problem is more localized than this (maybe too localized). See my answer. –  Byte56 Mar 1 '13 at 23:07
add comment

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