# Bend a line in XNA

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

## Recommended Posts

I am developing a rainbow simulation in XNA where I want to simulate the glass prism effect where the incident light hits the surface of the glass prism. The light is bended in several different directions where in every direction a new color is formed. See this Wikipedia article for more information:

http://en.wikipedia.org/wiki/Dispersion_(optics)

I have managed to create a line, which represents the light, and a triangle, which represents the glass prism, in 2D in XNA. I also managed to implement the intersection handler between the line and the triangle, making it possible to alter the line when it is intersecting the triangle.

http://www.student.itn.liu.se/~johto970/game_project/prism_simplified.png

My question is how you bend the line when it is inside the triangle? See the image on the link above.

In the summary on I how have programmed so far in XNA,
I use a point list and a line list indices to create the line and then I draw the line using DrawUserIndexedPrimitives<VertexPositionColor> in XNA.
The line is moved at a certain direction by using the Translation Transform.

Should I create a new line when it is inside the prism/triangle with a different angle? Or can I transform the incident light/line so that when a section of the incident light is inside the prism/triangle it will be rotated with a angle so it becomes bended?

##### Share on other sites
I would model the problem using matrix optics.

Each optical element, such as a prism, free space, etc., can be modeled by a matrix. The transfer matrix then operates on the input vector producing the output vector on the other side of the interface.

You would have to make the refractive index of the material (the prism) dependent on wavelength to achieve the outcome that you are after. Wavelength is usually denoted by Lambda. For each input, I(lambda), you would have an output O(lambda). The superposition of the outputs would yield the desired output, having taken into account the wavelength dependence of the refractive index in the material, n(lambda). [n denotes refractive index].

Edit:in case that was overkill, just use Snell's law with a refractive index n(lambda) -- dependent on lambda -- look this up for glass, say something like this. Or even just make your own simple n(lambda) function; any monotonically decreasing function will do.

It will be a linear system so superposition applies; just calculate the behavior of each wavelength component and the net response is just the sum of the individual frequency components when taken individually.

[Edited by - signal_ on October 1, 2010 2:04:00 AM]

##### Share on other sites
Nice! I would really have a lot of use of the Wikipedia article that you posted for the project, never knew that article existed =) Really big thanks! =D

Still I am wondering if you should create a new line that is refracted in XNA when the incident line is entering the triangle? Because I am not sure if you can rotate a small section of the incident line using the rotation transformation because the transformation will affect the whole line. See the link to the image below for a clearer explanation:

http://www.student.itn.liu.se/~johto970/game_project/prism_simplified2.png

As you can see in the figure, it might not be the exact representation of what is happening in the reality. But I want to implement it that way for keeping it simple as much as possible and then I can build it up to a more realistic representation.

##### Share on other sites
Quote:
 Original post by john13toStill I am wondering if you should create a new line that is refracted in XNA when the incident line is entering the triangle? Because I am not sure if you can rotate a small section of the incident line using the rotation transformation because the transformation will affect the whole line. See the link to the image below for a clearer explanation:http://www.student.itn.liu.se/~johto970/game_project/prism_simplified2.png

So there are so basic issues here:
1. The simulation & its complexity
2. Rendering the results of the simulation.

It seems like you are now asking about #2. I would break the rays up into sections depending on where they are in the simulation of your optical system. Your optical system, by the way, is broad-band polychromatic low coherence light source (like sunlight) as the input. This light then travels thru 1. freespace, 2. glass prism, 3. free space. At each abrupt change in refractive index, the light will bend. So it seems you will need three rays: one in the first section of free space, one for the prism, and one for the last section of free space. So yeah, I think you'll need 3 xna lines. The first for the input should not change and just be a continuous input. Then you'll need 2 xna lines, one for the prism, and one for the freespace output, for each frequency of the input light. Hopefully this makes sense.

Graphically, you could represent the first polychromatic input as a beam of white light. Then for the sections of the prism and the free space at the end of the system you'd have to calculate the behavior of each frequency component of the light (it's color) and then draw the ray in the prism and at the freespace at the output, both bent at the appropriate angles. You would then repeat this for all the frequency components of the polychromatic input.

And for #1, the simulation, you would just use Snell's law for each frequency component, since the prism has a refractive index dependent on the wavelength of the input, and you know the angle of input incidence, since you can set this to whatever you want. As far as complexity goes, the sky's the limit. You could abandon geometric optics and use a wave optics representation or go with electromagnetic waves and Fresnel equations.

The point is as long as you're using geometric optics understand the simplifications you are making, and what you need to calculate. From here you can also think about rendering tricks that you may employ to get the exact type of effect that you are after.

##### Share on other sites
Yes, it is the #2 issue that I am dealing with right now. And yeah it makes very much sense to create extra XNA lines. =) It would make it too complicated of keeping track of every section of a single XNA line that needs to be refracted with a angle when the sections are inside the triangle. Thank you =)

However, since I want to simulate incident light/line with a limited length that is traveling through a glass prism/triangle, I am wondering how you adjust the position of one end point of the line while keeping the other end points position fixed in order to contract/expand the line? See the figure below to see what I mean

http://www.student.itn.liu.se/~johto970/game_project/shorter_the_line.png

Because if I use the Matrix.CreateScale with a numerical input on the scale factor, it will affect the both end points position. I only want to change ONE of the end points position over time by using the Update function where you insert gameTime as inparameter. I tried to assign new vertex positions of one of the end points with "new VertexPositionColor" during the Update function but nothing happened.

So, how do you change one of the lines end points position over time?

EDIT: I found out on how to do it, it is by using:

pointList.SetValue(new VertexPositionColor(newLineStartPos, Color.White), 0)

where pointList is an array of type VertexPositionColor, newLineStartPos is the updated position of one of the end point and the numerical value 0 is which position in the array the newLineStartPos should be inserted in.

[Edited by - john13to on October 3, 2010 3:19:40 AM]

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631788
• Total Posts
3002356
×