Determine if camera is between portals?

Post Reply
paulggriffiths
Posts: 21
Joined: Tue Jan 31, 2017 11:21 am

Determine if camera is between portals?

Post by paulggriffiths »

Here's a screenshot of what I am doing:

Image

I need to know if the camera is between 2 portals regardless how far away the camera is or the orientation of the portals?

Heres the different possibilities:

ImageImage

I don't think it's going to be that simple though, any ideas?

Here's a screenshot of my graphics engine, spent 2 months on it so far, and can't give up now.

Image
Last edited by paulggriffiths on Thu Feb 16, 2017 10:44 pm, edited 1 time in total.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Any good at maths? A bit like collision detection.

Post by benelot »

Produce a convex hull out of the eight corners of both portals. In your case it basically means you create a box out of these eight corners of which two sides are the portals. Now you can use a convex hull algorithm which tells you if the point the camera is located in is within the box (or in general the convex hull). If it is within the hull, it is between the portals. There are plenty of convex hull algorithms out there, just google "check if point is in convex hull".
paulggriffiths
Posts: 21
Joined: Tue Jan 31, 2017 11:21 am

Re: Any good at maths? A bit like collision detection.

Post by paulggriffiths »

I think I've worked it out, some angles are positive and some are negative, got to work them out.
A bit simpler than convex hull.

If there're all positive or all negative then the camera is outside, though have to include the angles between the two portals too.
Last edited by paulggriffiths on Thu Feb 16, 2017 10:14 pm, edited 1 time in total.
paulggriffiths
Posts: 21
Joined: Tue Jan 31, 2017 11:21 am

Re: Any good at maths? A bit like collision detection.

Post by paulggriffiths »

benelot wrote:Produce a convex hull out of the eight corners of both portals. In your case it basically means you create a box out of these eight corners of which two sides are the portals. Now you can use a convex hull algorithm which tells you if the point the camera is located in is within the box (or in general the convex hull). If it is within the hull, it is between the portals. There are plenty of convex hull algorithms out there, just google "check if point is in convex hull".
No, the camera may be far away from the portals but still in-between them.
ktfh
Posts: 44
Joined: Thu Apr 14, 2016 3:44 pm

Re: Any good at maths? A bit like collision detection.

Post by ktfh »

use planes to represent the portals, a single signed distance calculation can determine if the camera is inside or outside
example

Code: Select all

float PlaneDist(vec4 plane, vec4 pt) {
    vec4 d = plane * pt;
    return d.x + d.y + d.z + d.w;
}
paulggriffiths
Posts: 21
Joined: Tue Jan 31, 2017 11:21 am

Re: Any good at maths? A bit like collision detection.

Post by paulggriffiths »

ktfh wrote:use planes to represent the portals, a single signed distance calculation can determine if the camera is inside or outside
Good idea as could calculate if it is rotated more than 180 first then do the planes equation.

Probably a bit simpler than my angles idea, thanks!

This is the function I use as the corners are vec3's

Code: Select all

float VRenderManager::getDistance(glm::vec3 p0, glm::vec3 p1, glm::vec3 p2, glm::vec3 p3)
{
	GLfloat a, b, c, d;
	a = (p2.y - p1.y) * (p3.z - p1.z) - (p2.z - p1.z) * (p3.y - p1.y);
	b = (p2.z - p1.z) * (p3.x - p1.x) - (p2.x - p1.x) * (p3.z - p1.z);
	c = (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
	d = -p2.x * a - p2.y * b - p2.z * c;

	return (a * p0.x + b * p0.y + c * p0.z + d) / sqrt(a * a + b * b + c * c);
}
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Any good at maths? A bit like collision detection.

Post by benelot »

I realized that you maybe have a different definition of inbetween. If two planes can be used, you can basically just use basic math to determine if your camera is in front or behind the planes:

The general prodecure would be this:
vec normalA;
vec AB = B - A;
double dot = dot(AB, normalA);
bool inFront = (dot > 0);

source:
https://www.opengl.org/discussion_board ... nd-a-plane

P.S. Can you please rename the thread (edit first post) to something meaningful like "Determine if camera is between portals" so that it is easier for others to identify what it is about?
paulggriffiths
Posts: 21
Joined: Tue Jan 31, 2017 11:21 am

Re: Determine if camera is between portals?

Post by paulggriffiths »

For those that are interested here a YouTube video of my portals implementation:

https://youtu.be/TtJ-1bI10po
paulggriffiths
Posts: 21
Joined: Tue Jan 31, 2017 11:21 am

Re: Determine if camera is between portals?

Post by paulggriffiths »

Can't use distance plane equation because what if the top portal is rotated by 90?

Cam A & B positions are both inside the portals but one side would produce a not inside result. Also if the cam is above the top portal the infinite plane equation would not know this.

So think ill have to use angles?

It has to be exact because this calculation decides if a portal is hidden by another portal and then clip the portal as in the first screenshot in this thread. Cam positions A & B not clip the portal, cam positions C & D clip the portal.

Image
paulggriffiths
Posts: 21
Joined: Tue Jan 31, 2017 11:21 am

Re: Determine if camera is between portals?

Post by paulggriffiths »

edit
Post Reply