0

I 'm trying to stop an object when it collide with an other. I mean if the user try to slide it on an other i want my object to slide and not to go over as it does for the moment. I'm using Xna and rectangle collision function.

 public void Update(GameTime gametime)
        {
            Currentposition = Station.Position;
            Station.Update();
            coli = IsHit(ListObs[16].ShapeO, Station.Shape);
         }

public void Draw(SpriteBatch spritebatch)
{
            if (coli == true)
            {
                Station.Position = Currentposition;
                Station.Draw(spritebatch);
            }
            else
            {
                Station.Draw(spritebatch);
            }
}

its not working for the moment, i could try to use Farseer or bOx2DX but i think its too deep for me ( i don"t need gravity ).

 public bool IsHit(Rectangle r1, Rectangle r2)
        {
            if (((r1.X + r1.Width >= r2.X) && (r1.X <= r2.X + r2.Width)) && ((r1.Y + r1.Height >= r2.Y) && (r1.Y <= r2.Y + r2.Height)))
                return true;
            else
                return false;
        }

2 Answers 2

0

I don't know IsHit, but here is how I detect collision between rectangles:

if (source.BoundingRect.Intersects(target.BoundingRect))
{
    ...
}

Where BoundingRect are Xna Rectangle

2
  • But what exactly is not working for the moment? The collision detection or the position of your Station?
    – Schminitz
    Commented Oct 24, 2013 at 14:22
  • @Gabson I suggest you to use default methods instead of implementing on your own.
    – pinckerman
    Commented Oct 24, 2013 at 22:13
0

all you will normally have to know is:

 mesh.BoundingSphere

I could better explain it to you, by an example... For example:

We do have a cube...

If you know the distance to the wall , you can even create a bounding box:

float d / / middle distance <- > wall
Vector3 center = mesh.BoundingSphere.Center ;
var box = new BoundingBox (new Vector3 ( Center.X - d Center.Y - d Center.Z - d) , new Vector3 ( Center.X + d + d Center.Y , Center.Z + d) ) ;

Now you have the bounding box that includes your cube ( the bounding rich Vector3 2 : minimum and maximum) . Ideally , we can calculate them only once (possibly at the start of the program) .

Now that were the basics...


When you have improved you prgramming skills you should finally add rotations,trannformation and whatever you want...

But if your cube have to rotate , it is already complicated. Instead , the bounding box each time that you re berechnest when the cube rotates , it uses matrices. A problem that occurs when you see the bounding box always recalculate want is that the dice will no longer be the bounding box corresponds to , but at worst the corners of the cube are all right in the middle of the walls of the bounding box and you thus much " air " have in your bounding box , which is considered a collision , although there was no collision. For a bounding box is always oriented to the axes , while your cube is then just rotated. To circumvent this problem, one uses matrices to bring the cube back axes oriented in a room if you want to test on a collision ( by its rotation with a matrix inverse reverses for the collision test).

If you understood this and was able to use it, you will be able to let the objects move like wherever and how you want ;)


I hope you understood it what I've tried to explain ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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