How so I make a laser path prediction line like a “slingshot cowboy” game in cocos2d or box2d and select the target like "slingshot cowboy" game.
Let's consider the basic slingshot mechanism that seems to be implemented here. We know the user can drag the slingshot by defining a vector having its origin at the tip of the downward pointing triangle (like in my figure). The user can define thus vectors that point toward the bottom of the screen (restricted programmatically), and whose radii cannot be larger than a dMax threshold.
Whenever the user shoots, the input vector has a length between [0, dMax]. This interval must map into a range interval: [rNear, rFar].
But since the range in real life is usually not a linear function of the launch velocity, we're free to suppose that the transfer function between [0,dMax] and [rNear,_rFar_] is looking somewhat like this:
where rFar is the maximum range (the red circle's radius), rNear is the minimum range (the green circle's radius). This function just maps one interval to the other, in a non-linear fashion. I'm using now the
In the figure above, the red vector is the one that gives you the shooting direction: just normalize it (divide it through its magnitude) and negate it (add the minus sign to it). Then multiply it with the result from the rangeFunction(length) where the length is the initial length (before normalization) of this vector.
Now find the angle between the blue and red vector using a 2D cartesian frame and the atan2 function. Let this angle be u.
The point where the projectile will land should be _(r*cos(u - pi), r*sin(u - pi))_, where r = rangeFunction(length). That should be it, more or less. Check if this point is "inside a cow's aura" and then kill it, like cowboys are supposed to, right?