Jump to content
• Advertisement

# How to detect diamond shape collision detection , if object is inside the diamond

This topic is 665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

i have block with diamond on top , it is moving along the Y coordinate.

i have object which i want to know if it is inside this diamond shape.

it is now isometric game .

i saw this , but i have problem to implement it :

is there any easier way to do it ?

i calculated the diamond points A B C D but still i have the points In between :

                             C
/@\
B@ @D
\@/
A

This is my shape with the diamond on top

[sharedmedia=core:attachments:33305]

#### Share this post

##### Share on other sites
Advertisement

For the tutorial to work , you need a flat surface, so you must compute the tile position on the plane at the top of the block, and then afterwards check if the computed tile is included in the block.

If you need more help with the tutorial, it would be helpful if you tell what part you understood, and where you get stuck, as in, where  does the tutorial deviate from your expectations and why?

Obviously, there is always the option to take the plain xy coordinate computation route, since you already know where the 4 points are.

[attachment=33311:simple_diamond_check.png]

Using the points A to D, and the left picture, decide in which rectangle you are. Given the xy coordinates of the diamond corner points, this is simple.

The more tricky part is in which triangle part of the rectangle you are. There are two cases. For rectangles 1 and 4, you have a line from bottom left to top right, as shown in the upper right rectangle. For rectangles 2 and 3 you have the case as shown in the bottom right.

In both right rectangles, x is assumed to run horizontally from 0 to x_size. That makes (x / x_size) run from 0 to 1.

Similarly, y is assumed to run down from 0 to y_size, which makes (y / y_size) run from 0 to 1 as well.

If the condition written below the rectangle holds, your point is in the triangle with the black dot.

To see how this works, assume x=0 and y=0 in the upper-right rectangle, ie at the top-left corner, that makes (x/x_size) + (y/y_size) also 0, which is less than 1. Now imagine you slowly increase x and y, walking  towards the bottom-right corner. When you are halfway, (x/x_size) == 1/2 and (y/y_size) == 1/2, so together they are exactly 1. If you move further they grow more, and cross the "1" boundary, falsifying the condition.

The bottom-right rectangle does exactly the same, except it uses (1-x) instead of x, so the increase of x runs from right to left, instead of left to right.

#### Share this post

##### Share on other sites

Another way is to have a single offset image.

Pick the tile as if they were normal rectangles/squares, then do a look-up (check color of) offset image to see whether to adjust x/y coordinates.

This technique also works for other shapes, even if they are irregular.

#### Share this post

##### Share on other sites

Or, you could do frustum check against four infinite 2d lines, considering :

infinite lines are in constant winding getting defined (clockwise or counter-clockwise, see point pairs of your diamond).

In such case, compute distance of an arbitrary examined point from those four lines (regular infinite line), and if all distances have the same sign- your inside, otherwise outside. (it can be positive fours, or negative fours, depends on winding direction, main thing the winding is constant).

#### Share this post

##### Share on other sites

@Alberth
Thanks allot for taking the time to explain , your method is something i will try , i didn't understand how the vector math is working in the tutorial .
for example in my API ( cococs2d-x )
i dont have the ability to :

public Vector3 mul(Matrix4 matrix)

where in the example i see : touch.mul(invIsotransform);

@Lactose!
Thanks!

You mean pixel perfect collision detection?

@JohnnyCode
Thanks! but i didn't understand what you mean could you point me to some explanation ?
Edited by umen242

#### Share this post

##### Share on other sites
i dont have the ability to : public Vector3 mul(Matrix4 matrix) where in the example i see : touch.mul(invIsotransform);

You use 4x4 matrices to also allow translation (in the 4th column). People then basically standardize on that, so you have the same thing everywhere. The 3D coordinates then get extended to 4 rows, by appending a 1.0 in the 4th row.

Thanks! You mean pixel perfect collision detection?

Yes. What you do is take the 4 rectangles image, and draw the diamond in it. Colour the inside with one colour, and the outside with some other colour. Then given your coordinate, get the colour of the point in the image. If it is "one colour" you're inside, else you're outside the diamond.

If you have different sizes diamonds, you need to do some scaling to make the images and the diamond that you have of the same size.

Thanks! but i didn't understand what you mean could you point me to some explanation ?

A line through points (a1, b1) and (a2, b2) can be expressed as a equation (a2-a1)*x + (b1-b2)*y + c = 0, where c can be computed by using one of the points.

Now if you fill in points in this equation, you'll find that points on the line result in 0, points at one side of the line give a positive value, and points at the other side give a negative value. As such, this equation can tell you where a point is relative to the line.

Now if you take the 4 lines, and organize it such that the positive side is towards the diamond (swapping points reverses the sign), then if you fill in a single point in the equations of the 4 lines, and they get a positive value from all equations, you must be inside the diamond.

Edited by Alberth

#### Share this post

##### Share on other sites

Thanks!!

Sorry , in the last explanation i didn't understand what is: c?
what points ? can you please give simple example ?
Thanks again !

#### Share this post

##### Share on other sites
Question
im try to implement the method you show me
the problem is that in my case the 0,0 coordinates are from bottom left of the rectangle
the first formula (x/x_size) + (y/y_size) working great

but the second one with (1-x) doesn't work . how can i change it to work with my coordinates ?
also  now i just noticed that i need to reset on each part of the rectangles the x and y position to 0,0 from it world coordinates ..
this is way to much work ...

Thanks!
Edited by umen242

#### Share this post

##### Share on other sites

Sorry , in the last explanation i didn't understand what is: c?
c is a number that depends on the points (a1, b1), and (a2, b2).

I now also see I put the wrong computations with x and y, sorry about that.

The formula is thus (b1-b2)*x + (a2-a1)*y + c = 0. To compute c, take one of the points, eg (a1, a2), and fill that in for x and y:

(b1-b2)*x + (a2-a1)*y + c = 0      formula
(b1-b2)*a1 + (a2-a1)*a2 + c = 0    x=a1, y=a2
c = 0 - (b1-b2)*a1 - (a2-a1)*a2    bring c to one side

You can rewrite the right hand side of the final equation further if you like, but a computer will handle this too :)

but the second one with (1-x) doesn't work . how can i change it to work with my coordinates ? also now i just noticed that i need to reset on each part of the rectangles the x and y position to 0,0 from it world coordinates ..
Ah, another stupid mistakes from me. Parentheses are wrong. Should be 1 - (x / x_size) + (y / y_size) <= 1     The reason is x/x_size runs from 0 to 1, which makes 1-(x/x_size) run from 1 to 0.

As for using the lower-left corner, that's fine. You can mirror the right triangles horizontally, vertically or both, until you get the origin at the lower left corner instead of the upper left corner as I drew them. After doing that, compare the diagonal line with the line of the diamond, and pick the rectangle that matches w.r.t to diagonal line. It seems that rectangle 1 and 4 then use the mirrored lower right rectangle, while rectangle 2 and 3 use the mirrored upper right rectangle.

(The distinction here is whether the origin of the right rectangle has the diagonal line or not, that decides the formula. Which corner you name "origin" is not relevant, since everything runs from 0 to 1 after dividing by size.)

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5
• Advertisement

• 28
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631738
• Total Posts
3001950
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!