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:
rangeFunction(l) = (rFar - rNear) * sqr(l / dMax) + rNear
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
sqr (x^2) function for below unit positive values.
This function should give the user that plausible natural impression we're looking for in such games.. without a lot of mathematics/physics to support it.
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?