Object boundaries

Started by Albareth, January 23, 2004, 01:11:18 AM

Previous topic - Next topic

Albareth

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...

EgonOlsen

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

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.

EgonOlsen

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...):

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

Nope, I wasn't talking about something different...  You hit it on the nose... Thanks Helge!!!

Albareth

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?

EgonOlsen

Split the method above into 4 seperate ones. Each one checking for one border only and adjust your movement according to the results.