Sign in to follow this  

Border Detection

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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(); }

Share this post


Link to post
Share on other sites
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 transformation
MFinal = MModel * MView * MProj
for 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
...
fi
rof


Corrections welcome [smile].

Hope this helps,
Pat.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this