Monday 15 February 2010

awt - Dectecting side collisions using rectangles in Java -


This is my first week of Java, and I am making a game in which rectangles are related to each other's bounce. Rx and RE are coordinates of rectangular ones, and VLRX and Valerie are their X and Y velocity. I am trying to make it so that the rectangles are contrary to each other (vice versa) but continue with the same X velocity if they collide above or below, and vice versa for the left and right sides . However, I do not know on which side rectangles can be bumped. Can I get some help? The relevant code
  // Check the confrontation between the Namesis and the public public boolean check syllable other () {rectangle r1 = rect1.getBoundsNemesis (); Rectangle r2 = rect2.getBoundsCop (); If (r1.intersects (r2)) {collision = true; Rect1.velRY = -state1.velRY; Rect1.velRX = -state1.velRX; Rect2.velRY = -ct2.velRY; Rect2.velRX = -ct2.velRX; } And collision = false; Return collision; }    

What do you want to do, compelling rectangles for each of your edges Since the two games are rectangular since the rectangular has four edges, you have 4 bitting rectangles for rectangles.

You have the police and 4 biting rectangles for 4 biting rectangles for nemesis, that means you have 16 intersects for a double tests Have to do in the loop.

When one of the intersects exams is true, you can determine which side of your police rectangle and on which side of your slave raid there was a confrontation .

There is some code to make it clear how to make rectangle byte if you want, you can expand them more than 3 pixels.

  Public listing & lt; Bounding detection & gt; Make Bounding Retacles (Rectangle R) {List & lt; Bounding detection & gt; List = New Arrestist & lt; Bounding detection & gt; (); Int brWidth = 3; // Create a rectangle rectangle rectangle = new rectangle (Rx, R.E., BRWWithth, R.Hight); List.add (new boundingrectengal (left, "left")); // Top Rectangle Rectangle Top Rectangle = New Rectangle (Rx, RE, R. Width, BRWD); List.add (new boundingrectengel (top, "top")); // Correct rectangular oblong right = new rectangle (R. X + R. Width - Broadthth, R.I., Broadthth, R. Hight); List.add (new binding reference (true, "right")); // rectangle rectangle below rectangle = new rectangle (Rx, RI + R.Hyight - BRVDath, R. Width, BRWD); List.add (new boundingrectengel (bottom, "bottom")); Return list; } Public square boundingrectengel {Private rectangle rectangle; Private string position; Public bounding rectangle (rectangle rectangular, string position) {this.rectangle = rectangle; This.position = Status; } Public Rectangle getRectangle () {Return rectangle; } Public string getPosition () {return status; } Public Boolean cutting (rectangle r) {return rectangle.intersects (r); }}    

No comments:

Post a Comment