Author Topic: Object boundaries  (Read 3623 times)

Albareth

  • Guest
Object boundaries
« on: January 23, 2004, 01:11:18 am »
Helge, how would I write some sort of code that would stop a ship from moving beyond the edges of my display?  its set in 640*1024, if that helps...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11749
    • View Profile
    • http://www.jpct.net
Object boundaries
« Reply #1 on: January 23, 2004, 03:34:21 pm »
To determine if the object has left the screen boundaries, you can use Interact2D.projectCenter3D2D(); This will give you screen coordinates of the object's center. While it doesn't take the object's dimension into account, it should be sufficient in your case, if you roughly know an object's dimension on screen.
Or use Object3D.wasVisible(), which returns false if the object wasn't on screen in the last frame. This will work too, but it requires the object to be rendered offscreen at least once before it can be discarded.

Albareth

  • Guest
Object boundaries
« Reply #2 on: January 23, 2004, 07:47:47 pm »
What code would I write to do a check?  I want the ship to stop moving if it hits 20 pixels away from the edge of the display.  I'm slightly confused about how I would check a SimpleVector if the vertical or horizontal position (depending on which side the player is trying to pass) is different in every case, but the opposite position is constant, either 20 units away from the left or right side, and 20 pixels away from the top and bottom.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11749
    • View Profile
    • http://www.jpct.net
Object boundaries
« Reply #3 on: January 23, 2004, 09:17:35 pm »
I'm not sure if i'm understanding you correctly in this case. If i do and the "center" of the ship is placed in the actual center of the object, then maybe something like this will help (not tested...):

Code: [Select]
public boolean shouldIStop(SimpleVector projCenter) {

int xPos=(int) projCenter.x;
int yPos=(int) projCenter.y;

int xr=xPos+10;
int yt=yPos-10;
int xl=xPos-10;
int yb=yPos+10;

if (xl>LEFT_BORDER && xr<RIGHT_BORDER && yt>TOP_BORDER && yb<BOTTOM_BORDER) {
return false;
}

return true;

}


Or were you talking about something different?

Albareth

  • Guest
Object boundaries
« Reply #4 on: January 23, 2004, 10:45:50 pm »
Nope, I wasn't talking about something different...  You hit it on the nose... Thanks Helge!!!

Albareth

  • Guest
Object boundaries
« Reply #5 on: January 23, 2004, 11:09:48 pm »
Ok...  Now another bit...  How would I get the object to move in any direction other than the one that it hit a border?  for example, if it hit the left border, it can move up, down, left and right, but not left?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11749
    • View Profile
    • http://www.jpct.net
Object boundaries
« Reply #6 on: January 24, 2004, 12:42:22 pm »
Split the method above into 4 seperate ones. Each one checking for one border only and adjust your movement according to the results.