0

I am developing a game which is based on optical items like mirrors and lenses. I have a beam. If beam will hit a optical item it should reflect depending on the physics rules. I know the beam's directions. So i need to detect the collision with optical items so i cant calculate the reflection. What would be the best way to implement optical items in java to detect collision with beam and optical item. Boundary box model will not fit my situation. I am thinking to use polygons. It will easy to detect collision point(x,y) but optical items can be rotate so the calculating the angles and reflection seems will hard to do.

What do you recommend?

henderunal
  • 749
  • 2
  • 9
  • 14

2 Answers2

1

Calculate the angle between the two lines of the mirror and beam. Then you just need to draw your reflected beam at the same angle on the other half of the mirror.

Community
  • 1
  • 1
Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
0

Usually when developing a UI, you have a data model and a view that renders the data model.

Your problem is a little different. You would have a class that represents a mirror and another class that represents a lens. Instances of the classes would hold state information like the position of the object and the direction the object is facing. The class methods would be responsible for implementing the physics rules of the mirrors and lenses.

Your view drawing logic will have to do more than just render the data model. You're going to have to loop through all of the instances of your objects to detect an impact, and then apply the physics rules to the beam to see where to go next.

While the beam source, lenses, and mirrors are all objects in your data model, the beam itself has to be calculated every time you want to draw the beam in the view.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111