Advertisement

Border Detection

Started by December 15, 2004 05:46 AM
8 comments, last by BangorRipper 19 years, 11 months ago
What's the best way to implement the following in a 3d game to detect when the models hit the edge of the screen? Can i do something similar to the following which is for 2d? cheers /** Left border at which to shift direction */ private static final int LEFT_BORDER = 10; // if we have reached the left hand side of the screen and // are moving left then request a logic update if ((dx < 0) && (x < LEFT_BORDER)) { game.updateLogic(); }
Actually there is no simple way of doing that. The problem is that 3D space is different from the 2d space it is projected to. That means you would have to transform each model model vertex manually and check if the transformed vertex (that is in screen space then) crosses a screen border:

; Final 3d -> 2D transformation matrix, MModel denotes the local transformationMFinal = MModel * MView * MProjfor each vertex v in model    ; transform vertex    vT = v * MFinal        ; vT.x and vT.y are in screen space now    if vT.x < LeftBorder then         ...    firof


Corrections welcome [smile].

Hope this helps,
Pat.
Advertisement
I just realised what you are trying to do [smile]. It is a bad idea trying to save CPU time by only updating on-screen entities. Checking whether entities are culled and performing update operations on unculled objects only is a much better idea.
So what you should look for is frustum culling.

Good luck,
Pat.
Sorry could you explain in a bit more detail plz, i am a newbie unfortunately :(. I am making a space invaders game so all i want to do is see if the leftmost/rightmost alien has hit a certian point which will be roughly the edge of the screen within my view so that i can then move them all forwards and then change their direction
thanks
Ah, I see [smile].
In that case you just don't deal with that kind of stuff (frustums, transformations a.s.o.). You can move your objects using a translation matrix and modify the x and y values. Then you just check these coords for a certain threshold as you would do in 2D. The value of that threshold depends on your view matrix (camera) setup.

Although to calculate the threshold you will need to calculate your frustrum. this thread contains the maths to do so, but if your objects aren't really 2D you'd be better off using an orthographic projection (google gluOrtho).

Enigma
Advertisement
Enigma i've pmd you, hope you don't mind
Yeah, I got the PM, but I'll post the reply in this thread incase anyone ever searches for this topic.

First off, is your scene actually 3D, or is it really 2D? If it's 2D then replace your gluPerspective call with a call to gluOrtho2D. they your left boundary, right boundary etc. will be the values you pass to gluOrtho2D.

If your scene really is 3d then you need to find the leftmost, rightmost, topmost and bottommost points on the objects.

The post I linked to contains the maths for calculating the x and y coordinates at the edge of a screen for a given depth, i.e.:
leftBorder = -tan(fovy) * (width / height) * object.leftmostPoint.z;if (object.leftmostPoint.x < leftBorder){	// object hit left border}

Where fovy, width and height can be obtained from your gluPerspective call.

Enigma
I have 3d models but I want to play the game from a fixed perspective
solved it now, really simple, just tested as to whether the alien's x had hit a particular x, which in my case is -30!!!

This topic is closed to new replies.

Advertisement