OK, this is actually easier than the “cap to a maximum speed after launch” scenario which was the second option I mentioned above.
What you should do is just check the values that will be applied as force before you actually apply it, and factor them down if necessary.
The values you input to the force API are, of course, calculated by the difference between the touch start and end, as here: event.xStart - event.x
So, let’s say that the maximum X -or- Y force is 2… that’s an arbitrary number just for this example. Whatever maximum number works for your scenario (and it could be more, could be less, depending on your body densities, gravity, and other factors), neither X nor Y should be higher than that value in any particular launch.
Now, imagine that based on a particular “launch” (let’s imagine a pool cue hitting a ball), you actually get values like X=1 and Y=57… in this case, the user has pulled back the “cue” WAY down (difference of 57) but almost directly vertical (thus, only a difference of 1 for the X value). You obviously don’t want 57 being input to the force API or the ball will blow off the screen at hyper-speed!
To solve this, just factor down using simple math and if-then checks:
local xVal = event.xStart - event.x
local yVal = event.yStart - event.y
if ( math.abs(yVal) \> 2 ) then --refactor when Y \> 2
local factor = 2 / math.abs(yVal) --2 being the "cap": so, 2/57 = 0.035
--multiply BOTH terms by factor so they are relational
xVal = xVal \* factor --multiply X by the factor: 1 \* 0.035 = 0.035
yVal = yVal \* factor --multiply Y as well: 57 \* 0.035 = 2 (the cap value!)
--so, the final inputs to the force API will be 0.035,2. Notice that these
--are the same proportional ratio as 1,57... but now 57 is refactored down
--to 2 (the cap) and the X value is likewise refactored.
elseif ( math.abs(xVal) \> 2 ) then --refactor when X is \> 2
local factor = 2 / math.abs(xVal)
xVal = xVal \* factor
yVal = yVal \* factor
end
--notice that if both values are less than 2, no refactoring is performed!
player:applyLinearImpulse( xVal, yVal )
I think that’s it. Let me know if you see any problems in the math (I’m in the middle of several things at the moment and didn’t study this thing over with full attention to detail
).
Brent
[import]uid: 9747 topic_id: 31294 reply_id: 125142[/import]