[Resolved] How to set a maximum force applied to an object ?

i want to set a maximum linear impulse applied to my player, now i only can give
a linear impulse so any ideas help please!!! :smiley:

this is the code i have

[lua]if (event.phase == “ended”)then
player:applyLinearImpulse(event.xStart - event.x,event.yStart - event.y)
player:setLinearVelocity( event.xStart-event.x,event.yStart-event.y )
–display.getCurrentStage():setFocus(nil)
arrow.isVisible = false;
orb.isVisible = false;
end[/lua] [import]uid: 138440 topic_id: 31294 reply_id: 331294[/import]

I don’t quite understand… you want a limit to the amount of force that can be applied? Or you want the object to have a “maximum speed” after any amount of force is applied, even if the force is massively high?

Brent [import]uid: 9747 topic_id: 31294 reply_id: 125086[/import]

i applied a linear velocity to my player calculated with the start of the touch and the end of it, so it means; if the end of the touch is very far from the start, the velocity would be very high… so i want to prevent that the gamers can applied a very high velocity… like when mario bross jump… he has the ability to jump just with some force so on that way is limited. [import]uid: 138440 topic_id: 31294 reply_id: 125092[/import]

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 :slight_smile: ).

Brent
[import]uid: 9747 topic_id: 31294 reply_id: 125142[/import]

O.O you are awesome it works… i change some numbers only but the maths are perfectly!!!
Thanks a lot for spend your time helping me [import]uid: 138440 topic_id: 31294 reply_id: 125167[/import]

I don’t quite understand… you want a limit to the amount of force that can be applied? Or you want the object to have a “maximum speed” after any amount of force is applied, even if the force is massively high?

Brent [import]uid: 9747 topic_id: 31294 reply_id: 125086[/import]

i applied a linear velocity to my player calculated with the start of the touch and the end of it, so it means; if the end of the touch is very far from the start, the velocity would be very high… so i want to prevent that the gamers can applied a very high velocity… like when mario bross jump… he has the ability to jump just with some force so on that way is limited. [import]uid: 138440 topic_id: 31294 reply_id: 125092[/import]

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 :slight_smile: ).

Brent
[import]uid: 9747 topic_id: 31294 reply_id: 125142[/import]

O.O you are awesome it works… i change some numbers only but the maths are perfectly!!!
Thanks a lot for spend your time helping me [import]uid: 138440 topic_id: 31294 reply_id: 125167[/import]