# Finding reflection points in a scene - Trigonometry Help

Hey everyone,

I am attempting to do some first order reflection finding within my environment, for use in a later room acoustic model. The method I am using is the image source method, in which the reflections are found by first mirroring the scene and then drawing a line from the mirrored sources to the original listener. The point at which this line crosses the mirror line is the reflection point. This diagram from Aalto university shows the concept nicely.

I have a script which *almost* does this, except the locations of the reflections are slightly off: see this image of the scene

The centre of the cubes should be the reflection point, however they are slightly off the eviroment boundary.

the code used to create this is:

```
foreach (Vector3 dir in simpleDirections) {
//find the point at which a ray travelling this dir will hit something in the room
// to use as our mirror line
Ray ray = new Ray (source.transform.position, dir);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
if (hit.collider.name != listener.name) {
// Create the image source and a ray traveling from this image source to the listener
Vector3 imageSource = hit.point + (dir * hit.distance);
Ray imageSourceRay = new Ray (imageSource, listener.transform.position - imageSource);
// Find the point of reflection, to do this we need to find how far along
// the imagesourceray to travel to reach the mirror line.
// some trig to find this distance - cotan(theta) * opposite
float dot = Vector3.Dot(imageSourceRay.direction,dir);
float theta = dot / (Mathf.Abs(imageSourceRay.direction.magnitude) * Mathf.Abs(dir.magnitude));
float len = Mathf.Abs(cotan (theta) * hit.distance);
Vector3 reflectionPoint = imageSource + (imageSourceRay.direction * len);
path currPath = new path (source.transform.position, listener.transform.position);
currPath.segments.Add (new Ray (source.transform.position, reflectionPoint - source.transform.position));
currPath.segments.Add (new Ray (reflectionPoint, listener.transform.position - reflectionPoint));
currPath.isValid = true;
reflections.Add (currPath);
}
}
}
```

I can make the reflections appear in the correct place by raytracing the image source ray, as well as using convex colliders or double sided meshes. However the end application will use fairly large meshes so I would like a solution not using raytracing!

Currently I think that my problem is in calculating the distance from the image source to the boundary in order to find the reflection point. My method for this is in the above code as:

distance = cotan(theta) * opposite

where theta is the angle between: the vector from source - mirror image source and the vector from mirror image source to listener.

the opposite is the distance from the boundary (mirror line) to the image source.

On the math side to find the reflection point : Assuming we are in a 2d space (z is same for all) , also the listener is in the same line with the IS (:image source) which's line is parallel to the $$anonymous$$ (:$$anonymous$$irror) and both are vertical to the ground (x axis) and lastly you know the distance from the IS to the $$anonymous$$ and you know the θ(:theta) which you mention above we have :

```
tan(θ) * d(IS,$$anonymous$$)=d(A,RP)
//A is the point on the $$anonymous$$Irror from where we calculate the distance from IS to $$anonymous$$irror
tan(θ)*d(IS,$$anonymous$$) = sqr( (x2-x3)^2+(y2-y3)^2)
but x2=x3 so x2-x3=0 because RP and A are on the same line($$anonymous$$irror) which is vertical to the ground)
tan(θ)*d(IS,$$anonymous$$) = y2-y3
y2 = y1 (where y1 is y coordinte of IS) so it is known :
y3 = y2-tan(θ)*d(IS,$$anonymous$$)
and you found it (the RP(x3,y3))=(x2,y3))
d(IS,$$anonymous$$) : Distance of $$anonymous$$irror to Image Source
(x axis is the ground, y axis is parallel to $$anonymous$$irror and vertical with x)
```

To conclude if you want to make it for 3d space it s gonna get a bit more complicated but the idea is the same also if you don't make all the above assumptions thats gonna be wrong. Note: from what you write above distance= cotan(θ) * opposite is wrong because if opposite is the distance of the mirror to the Image Source and distance is the distance form the image Source to the $$anonymous$$irror Image source then the opposite is the half of the distance (: opposite = distance/2) due to the fact that mirror acts as a symmetry axis.

Note2: it will never be 100% accurate as float numbers are rounded

Cheers hope that helps.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.