#### Archived

This topic is now archived and is closed to further replies.

# How do I detect if an object has reached the screen boundary in a 3D game?

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

## Recommended Posts

I am implementing a starfield into my 3D space shooter. The scene is viewed from a slightly downward angle and the stars move towards the player. What I want to do is, when the stars reach the lower boundary of the screen, they will move back to the upper boundary. However, I can''t simply compare the position on the y-axis (the y-axis goes into and out of the screen because of the view angle) because the stars lower down will visibly just disappear. I need to detect using actual screen coordinates. Any idea how to do this? I get the feeling it is something to do with multiplying or dividing some view matrices. I hope I''ve explained this clearly.

##### Share on other sites
[projection matrix] * [model matrix] * [point position] = [screen pos]

Or if you''re using OpenGL, gluProject(), D3D has something similar in D3DX I''m sure.

##### Share on other sites
D3DXVec3Project/D3DXVec3Unproject, one of those two will do what you want to.

##### Share on other sites
Thanks. I'm using OpenGL so will use gluProject(). Is this efficient when using this for e.g. 100 stars in the starfield?

[edited by - Billy Lee on March 24, 2004 4:22:35 AM]

##### Share on other sites
I''ve done some work on it. The game starts up i.e. goes to the title screen, but when I start the game, it crashes. I''ve narrowed it down to the bit in the for loop which compares the screen position of the star to a certain value:

if (*screen_pos[1] <= 10.0f)

Can anyone tell what is wrong? I realise that I''m comparing a double to a float but it doesn''t seem to complain. I tried different ways to do it too and they compile but have the same result.

Also, I get a warning saying warning C4700: local variable ''screen_pos'' used without having been initialized . How do you initialise this because I thought I did it by doing double* screen_pos[3] = {0,0,0}; ?

// Check if star has reached bottom of screenvoid Starfield::checkOutOfBounds(){ 	// Get modelview matrix	double modelview[16];	glGetDoublev (GL_MODELVIEW_MATRIX, modelview);	// Get projection matrix	double projection[16];	glGetDoublev (GL_PROJECTION_MATRIX, projection);	// Get viewport	int viewport[4];	glGetIntegerv (GL_VIEWPORT, viewport);	double* screen_pos[3] = {0,0,0};	// Use to store screen coordinates of a star	// For each star	for (int loop = 0; loop < num_particles; loop++)	{		gluProject (particles[loop].x, particles[loop].y, particles[loop].z, 					modelview, projection, viewport,					screen_pos[0], screen_pos[1], screen_pos[3]);		if (*screen_pos[1] <= 10.0f)		// If star has reached lower boundary		{			// Reset y position to top of screen			particles[loop].y = screen_top;		}	}} // End of method checkOutOfBounds

##### Share on other sites
change:

double* screen_pos[3] = {0,0,0};

to:

double screen_pos[3] = {0,0,0};

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 17
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631730
• Total Posts
3001917
×