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 am trying to use a ray and bounding box to determine if a box is between the player and the mouse pointer in 2D space. When I try testing the code, the collision will return true when pointed at the box but it also returns true under other circumstances where it shouldn't.

For instance. If I have a player on the left and a box directly to the right, I can put the mouse pointer a few hundred pixels above the box or a few hundred below and it will still return true. Also, I can put my mouse pointer to the left of the player and in a certain area it will still return true. Does anyone have any idea what might cause this?

I have left out definitions for some of my members and properties just to make this code sample easier to read. The position property is just a Vector2 for where each object is located.

ray = new Ray(new Vector3(player.Position, 0), new Vector3(mouse.Position, 0);
box = new BoundingBox(new Vector3(box.Position, 0), new Vector3(
new Vector2(box.Position + box.Width, box.Position + box.Height), 0);

if (ray.Intersects(box) != null)
    collision = true;
else
    collision = false;
share|improve this question
    
When you debug, what values are stored in ray and box? –  Seth Battin May 30 '13 at 0:49
    
Here is an example of values I get. The ray is pointed in the opposite direction of the box but still returns a value, and a strange value of less than 2. I don't get it. ray origin: {X:350 Y:350 Z:0} ray destination: {X:82 Y:88 Z:0} box points: {X:500 Y:540 Z:0} {X:600 Y:540 Z:0} {X:600 Y:500 Z:0} {X:500 Y:500 Z:0} {X:500 Y:540 Z:0} {X:600 Y:540 Z:0} {X:600 Y:500 Z:0} {X:500 Y:500 Z:0} collision distance: 1.829268 –  getsauce May 30 '13 at 1:12
    
The order in which those points are inserted makes a lot of difference. Bounding box can get confused if it's various Min and Max coordinates are swapped. That came up for this post: gamedev.stackexchange.com/questions/54382/… –  Seth Battin Jun 4 '13 at 23:31

2 Answers 2

I'm not certain this is the cause, but I do notice an issue when I looked up Ray's documentation. Ray's first argument is a position (so okay there). Ray's second argument expects a Vector. Coding-wise, the two are interchangable, but concept-wise, they're very different. Basically, the second argument should just indicate which direction for that Ray to face from its starting point; not which position it should point towards.

I think mouse.position - player.position should do it.

share|improve this answer

The problem is that the bounding box has the same Z in Max and Min, so there is no volume.

Changing this:

 box = new BoundingBox(
    new Vector3(box.Position, 0), 
    new Vector3( new Vector2(box.Position + box.Width, box.Position + box.Height), 0);

To this:

 box = new BoundingBox(
    new Vector3(box.Position, 0), 
    new Vector3( new Vector2(box.Position + box.Width, box.Position + box.Height), 1);

Should work..

I thing your over complicating things a bit... Is easier using a rectangle for this without using rays and boundingboxes:

Reectangle bounds = new Rectangle((int) pos.X, (int) pos.Y, 0,0);
bounds.Inflate(size/2, size/2);

if (bounds.Contains(mouse.X, mouse.Y) {
  ....
}

Or using distance if it's suitable:

if (Vector2.Distance(playerPos, mousePos)

share|improve this answer

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.