0
\$\begingroup\$

Would you know how to create a dash line that moves towards the direction of the target, and if it collides with a wall, goes to the opposite angle? I have seen atan2 but I am not sure what it does, and I am quite confused about the movement of the dashed line. A point can be found with cos() and sin() depending on a radius... should I change the radius to place each dash? Then how to move them with the same speed?

Example here :

enter image description here

Any advice would be much appreciated.

Thanks

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

You get the path the same way you'd move the object when you shoot it. Just have a tight loop that simulates the movement of the object and keep track of the position every so often. Now you have a list of positions, if you draw a dot at each position, you have a dotted line the represents the path of the object if it were to be shot from that angle.

\$\endgroup\$
9
  • \$\begingroup\$ Thanks, "simulates the movement"... would you have a small example? \$\endgroup\$
    – Paul
    Commented Mar 2, 2015 at 1:27
  • \$\begingroup\$ You simulate the movement. Take the position of the object, and in each iteration of the loop, add the velocity to the position based off of whatever your frame timestep is, check for collisions, etc. Essentially, each iteration of the loop is going to reproduce whatever your update loop does. \$\endgroup\$
    – House
    Commented Mar 2, 2015 at 3:14
  • 1
    \$\begingroup\$ In essence, for the example given, stop thinking about the line as a line (that's it's function but not what it is), start thinking about it as a procession of spheres, all launched from the bottom of the screen, which travel for a fixed distance with a fixed speed and collide / rebound from the sides. \$\endgroup\$
    – xan
    Commented Mar 2, 2015 at 9:37
  • \$\begingroup\$ Thanks Byte56, thanks @xan , I get the point now, moving spheres like objects in the game, instead of drawing a dash line. \$\endgroup\$
    – Paul
    Commented Mar 2, 2015 at 9:49
  • 1
    \$\begingroup\$ You have all the parts. Just solve for distance: distance = veclocity*time. In this case, time is your timestep 0.02. Velocity you have set to some constant I imagine. Now that you have distance, you just add that distance to your position each iteration: position += velocity*timestep; \$\endgroup\$
    – House
    Commented Mar 2, 2015 at 19:35
2
\$\begingroup\$

Byte56's answer is very good, especially for the example image given where simulating the movement of each "ball" in the line will work well. I'll give you an alternative idea however which might work better, or might be easier to implement if you are trying to work with a dashed line (with or without animation), something like -- -- -- --

  1. Calculate the angle at which your dashed line is aimed, and the distance (T) it should extend from the start (call it point S).
  2. Check for an intersection with the wall(s) you have present. There are lots of ways to do this, for example see this question.
  3. If there is no intersection, simply draw your line with whatever tools you use in your engine.
  4. If there is an intersection (call it point I):
    1. Draw the first section of your line between the start point and the intersection point SI, as in 3.
    2. Calculate the angle for the 2nd line segment by reflecting it in the surface you have intersected (see for example this question.
    3. Calculate the remaining line distance (T - SI)
    4. Draw the remaining line segment from point I with the appropriate angle.
  5. Repeat 2 - 4 if more intersections are possible.

As for animation in this case, that heavily depends on how you are drawing the line. If you are using a "dashed" texture you may be able to achieve this by:

  • Tiling / Repeating the texture along the length of the line and then "animating" / adjusting the texture offsets each frame such that you achieve the illusion of the dashes moving along the line.

Otherwise, if using vectors etc.

  • By similarly drawing the individual dashes based on some "offset" from the beginning of the line, and then moving this offset over time.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .