Slick Forums

Discuss the Slick 2D Library
It is currently Tue Jun 18, 2013 6:50 am

All times are UTC




Post new topic Reply to topic  [ 30 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sat Jan 30, 2010 3:15 pm 
Offline

Joined: Mon Jan 11, 2010 10:37 pm
Posts: 68
Code:
Vector2f point = new Vector2f(0, 0);
Rectangle rect = new Rectangle(0, 0, 10, 10);
System.out.println(rect.contains(point.x, point.y));

Output: false


Besides that, another point [9, 0] is not contained either!

This causes problems for example in tile maps.
You have point near to the division line of two tiles and no tile contains it.

I have tried same example with Java2D and it behaves better I think.
Code:
Point2D jpoint = new Point2D.Float(0, 0);
Rectangle2D jrect = new Rectangle2D.Float(0, 0, 10, 10);
System.out.println(jrect.contains(jpoint));

Output: true

Java 2D can handle not only [9, 0] but even floats like 9.3[/code]

This is causing more troubles when testing a rectangle on intersection with line that goes along (near to) rectangle's border.

_________________
Jattra


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 10:33 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
It's the point 9,0 really "contained", I mean it's an edge ;)

Not sure. Probably wrong.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 10:40 am 
Offline

Joined: Mon Jan 11, 2010 10:37 pm
Posts: 68
There could be a philosophical discussion about it.
However, from technical point of view, it is contained. Or do You want to create games in an evnironment, where none of your map tiles contains a point definitely laying on Your map?

_________________
Jattra


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 3:39 pm 
Offline

Joined: Wed Jan 27, 2010 4:23 pm
Posts: 10
In my opinion, given whole number coordinates in pixels. If you were to fill a rectangle, every pixel location that is drawn should be considered to be contained in the rectangle.

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 5:14 pm 
Offline
Game Developer
User avatar

Joined: Tue Nov 21, 2006 4:46 am
Posts: 620
Location: Iceland
Slick tries to adhere to Java2D in a way (if I remember Kev's original goals correct).

Here's the definition from the 1.4 (Slick's currently supported JDK v) Java2D API:

(Notice how they explicitly define this in the Javadoc)

Quote:
Definition of insideness: A point is considered to lie inside a Shape if and only if:

* it lies completely inside theShape boundary or
* it lies exactly on the Shape boundary and the space immediately adjacent to the point in the increasing X direction is entirely inside the boundary or
* it lies exactly on a horizontal boundary segment and the space immediately adjacent to the point in the increasing Y direction is inside the boundary.

The contains and intersects methods consider the interior of a Shape to be the area it encloses as if it were filled. This means that these methods consider unclosed shapes to be implicitly closed for the purpose of determining if a shape contains or intersects a rectangle or if a shape contains a point.

http://java.sun.com/j2se/1.4.2/docs/api ... 0double%29

So, according to this, point (0,0) should be contained within a rectangle (0,0,10,10), but point (9,9) not!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 5:46 pm 
Offline

Joined: Mon Jan 11, 2010 10:37 pm
Posts: 68
I don't agree with You appel in one thing. The point (9,9) should be contained as well (my opinion). The key to this is to realize, what the boundary is. Don't You think that each rectangle has one pixel border all around. Don't You think that between two neighboring rectangles is two pixels wide area of nobody, do You? OK, You are suggesting to make one of them belonging to its rectangle. But what about the other one?

So, as I understand it, there are no border pixels, there are just virtual borders. The boundary spoken in the Javadoc is meant to be the pixel with index equal to the rectangle size.

Imagine three appended rectangles of size 10. Let's get it just in X direction to keep it clean.
The bounds are x=0, x=10, x=20. So the 0 belongs to the first one, 10 belongs to the second and make a guess which rectangle should contain the 20. In this scenario, 9 is definitely part of the first one.
That is my view of things.

If Slicks is going along with Java2D, try the example code from my first post. Thank You guys for will to discuss these things.

_________________
Jattra


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 6:34 pm 
Offline
Game Developer
User avatar

Joined: Tue Nov 21, 2006 4:46 am
Posts: 620
Location: Iceland
Point(9,9) cannot belong both to Rectangle(0,0,10,10) and Rectangle(9,9,10,10).

Imagine a grid consisting of rectangles, where no rectangle overlaps another. You have to detect on which rectangle your mouse was over. Can Point(9,9) (your mouse) be contained in TWO rectangles?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 6:46 pm 
Offline

Joined: Wed Jan 27, 2010 4:23 pm
Posts: 10
appel,

If you were to draw these two rectangles:
first, draw Rectangle(0,0,10,10)
next, draw Rectangle(9,9,10,10)

The second rect would draw over the lower-right corner of the first rect.

In other words, the point (9,9) is contained in both rectangles. Your two rectangles are not adjacent, they are overlapping.

This does not seem to be a bug. As I understand it, it is functioning as designed, but I think it bears looking into. To me, in a graphics lib, it makes sense to treat point containment the same as rendering. If the pixel would be drawn by a filled rectangle, the point is contained within the rectangle.

Joe


Last edited by tireswing on Tue Feb 02, 2010 6:48 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 6:47 pm 
Offline

Joined: Mon Jan 11, 2010 10:37 pm
Posts: 68
appel wrote:
Point(9,9) cannot belong both to Rectangle(0,0,10,10) and Rectangle(9,9,10,10).

Imagine a grid consisting of rectangles, where no rectangle overlaps another. You have to detect on which rectangle your mouse was over. Can Point(9,9) (your mouse) be contained in TWO rectangles?


No You can't. Neither I can. Neither anybody... oh
Hey, one thing Your two rectangles ARE over-leaping. Count with me. First rectangle starts with zero and is ten points wide OK?
0 1 2 3 4 5 6 7 8 9

And the second starts with nine, so
9 10 11 12 13 14 15 16 17 18

Yes point 9 really belongs to both because they are over-leaping.

_________________
Jattra


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 7:01 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Careful with the attitude.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 7:07 pm 
Offline

Joined: Mon Jan 11, 2010 10:37 pm
Posts: 68
If You've got me bad, take my apologies. It was not intended. Just trying to make it clear.

One polite question. Have You tried already how Java2D is behaving in thees things? Appel has been referring to it.

_________________
Jattra


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 7:08 pm 
Offline
Game Developer
User avatar

Joined: Tue Nov 21, 2006 4:46 am
Posts: 620
Location: Iceland
In math, I believe, this is symbolized as:

(FILLED O1)--------(EMPTY O1)(FILLED O2)--------(EMPTY O2)

O1 and O2 do not overlap.


Again. The Java2D crowd at Sun bothered putting in text explaining this in a explicit manner. Those people are a lot smarter than you or me, and they must have valid reasons for doing this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 7:18 pm 
Offline

Joined: Mon Jan 11, 2010 10:37 pm
Posts: 68
You haven't tried appel, have You? Look It is not so hard. Just copy paste and whole thing is clear.

Code:
public static void main(String[] args) {
      Rectangle2D.Float rect = new Rectangle2D.Float(0, 0, 10, 10);
      Point2D.Float point = new Point2D.Float(0, 0);
      System.out.println(rect.contains(point));
      point = new Point2D.Float(9, 9);
      System.out.println(rect.contains(point));
}

_________________
Jattra


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 02, 2010 7:27 pm 
Offline
Game Developer
User avatar

Joined: Tue Nov 21, 2006 4:46 am
Posts: 620
Location: Iceland
Interesting!

I need some food to power my brain now :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 04, 2010 11:08 am 
Offline

Joined: Mon Jan 11, 2010 10:37 pm
Posts: 68
Hello there again.
I am awaiting the final trial. Will this issue be considered to be a bug or not?

We have discussed here and (I hope) finally agreed that Slick is not behaving in accordance with Java2D and in my opinion it is not behaving correctly.

Slick Rectangle(0, 0, 10, 10) draws pixels 0 to 9 but does not contain (calling Rectangle.contains(point) ) neither 0 nor 9.

Last note: I have tried that with Slick distributed as JAR package. I don't have checked latest sources out. Can somebody confirm this behaviour using latest slick? If not, I will do it tonight.

EDIT: Just tried it with latest slick from SVN. It behaves right as JAR distributed version.

_________________
Jattra


Last edited by jattra on Thu Feb 04, 2010 6:17 pm, edited 1 time in total.

Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 30 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group