1
\$\begingroup\$

I am looking for some help on what would be the best approach to create a path from one point in a grid to the other side.

Imagine a grid of 5 x 5, where at (0,1) there is a light source pointing into the grid. In the centre of grid there are angled mirrors, angled at 45 degrees. After randomly placing the mirrors, the light source will turn on and the beam of light grows, deflects off the mirrors and reaches a point somewhere else in the grid perimeter.

e.g. start: (col 0, row 1) Light, (col 3, row 1) mirror, (col 3 row 3) mirror, (col 4, row 3) end point.

In Objective C, what would be the best way to create this beam?

The deflection mirrors will have 4 reflection possibilities, UP or DOWN, LEFT or RIGHT depending on type of mirror and beam direction.

\$\endgroup\$
  • \$\begingroup\$ Please note that pathfinding is when you set up logic so your enemies know how to move around obstacles. A direct beam of light should not use pathfinding, and as such, I have removed the tag. \$\endgroup\$ – Gnemlock Jan 24 '17 at 23:42
  • \$\begingroup\$ Can you clarify what you're asking? Do you want to know how to draw the beam or what data structure to use to represent the different orientations? \$\endgroup\$ – user1118321 Jan 25 '17 at 2:26
  • \$\begingroup\$ maybe I need to add more detail, - I was looking for others approach to this problem. The game process: Random placement of obstacles Select random start point around perimeter 'fire' light or ball into grid, let it bounce around the obstacles - being deflected based on obstacle (simple top left corner of gird square or top right corner of grid square) When it bounces outside of perimeter, understand the point \$\endgroup\$ – Steve Jan 26 '17 at 12:51
  • \$\begingroup\$ I have tried using physics body, and letting it go. This works to some extent, but I don't know where it will end up - problem. I could set a boundary and when it crosses boundary I would be able to do something, but I want to know before it starts its journey. If I get the points and calculate path, then draw with CG drawRect, I have a destination. But I don't want the line, I want a path for an object to follow. How would I get an object to follow a drawRect line? (as though it were a path) and use update to move it along the path ... so my question is how would others approach this? \$\endgroup\$ – Steve Jan 26 '17 at 13:00
  • \$\begingroup\$ Thanks Josh for your answer, its appreciated, but not quite what I am looking for \$\endgroup\$ – Steve Jan 26 '17 at 13:01
1
\$\begingroup\$

You can use Core Graphics for this. In your NSView's drawRect handler, for example:

- (void)drawRect:(NSRect)dirtyRect {
  CGContextRef context = [[NSGraphicsContext currentContext] CGContext];

  // Move to first point, draw a line to the second point.
  CGContextMoveToPoint(context, X0, Y0);
  CGContextAddLineToPoint(context, X1, Y1); 

  // ...repeat the above as needed to draw all the lines, then:
  CGContextStrokePath(context);
}

Functions like CGContextSetRGBStrokeColor or CGContextSetLineWidth can be used to control the color and thickness of the line.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.