Page 1 of 1

Determine if camera is between portals?

Posted: Thu Feb 16, 2017 9:23 pm
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

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

Posted: Thu Feb 16, 2017 9:53 pm
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".

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

Posted: Thu Feb 16, 2017 10:01 pm
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.

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

Posted: Thu Feb 16, 2017 10:11 pm
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.

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

Posted: Thu Feb 16, 2017 10:12 pm
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;
}

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

Posted: Thu Feb 16, 2017 10:17 pm
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);
}

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

Posted: Thu Feb 16, 2017 10:41 pm
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?

Re: Determine if camera is between portals?

Posted: Thu Feb 16, 2017 10:46 pm
by paulggriffiths
For those that are interested here a YouTube video of my portals implementation:

https://youtu.be/TtJ-1bI10po

Re: Determine if camera is between portals?

Posted: Fri Feb 17, 2017 5:38 am
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

Re: Determine if camera is between portals?

Posted: Fri Feb 17, 2017 3:05 pm
by paulggriffiths
edit