Object Speed Limitation

hi…
… i’m throwing objects by dragging each one of them using a ‘tempJoint’
… i want to set a max. speed for the throwing speed;
so even if i drag and throw it so fast, the object wouldn’t be thrown faster than the limit which was set before…

any thoughts !?

Kamel [import]uid: 96659 topic_id: 19962 reply_id: 319962[/import]

You might use obj:getLinearVelocity() to check the speed and immediately bump it down if it is too high.

Peach :slight_smile: [import]uid: 52491 topic_id: 19962 reply_id: 78011[/import]

Hi Peach,
i get the linear velocity then i set with a new one, and it set correctly…

 stones[c].tempJoint:setTarget( event.x + deltaX, event.y + deltaY )  
 vx, vy = stones[c]:getLinearVelocity()  
 print("velocityX : "..vx)  
 print("velocityY : "..vy)  
 if vx \> 100 then  
 vx=100  
 elseif vx \< -100 then  
 vx=-100  
 end  
 if vy\>300 then  
 vy=300  
 elseif vy vy=-300  
 end   
 stones[c]:setLinearVelocity( vx, vy )  
  
 v1, v2 = stones[c]:getLinearVelocity()  
 print("new velocity X : "..v1)  
 print("new velocity Y : "..v2)  

variables v1 & v2 are used to make sure that the new linear velocity was set correctly
but the object doesn’t seems to be affected with the new velocity … i think velocity can’t be controlled while throwing !!
Will applyForce() get the answer … or what !?
i will try it now anyway

Kamel [import]uid: 96659 topic_id: 19962 reply_id: 78482[/import]

i couldn’t bump the velocity down using applyForce() !! [import]uid: 96659 topic_id: 19962 reply_id: 78491[/import]

Could you post more code? Based on what you have, line 2 through line 19 should be wrapped inside a Runtime:addEventListener(“enterFrame”, ? ) function. Otherwise from your code, its checking the velocity once and changing it. The acceleration rom the joint will easily override this a millisecond later. [import]uid: 54716 topic_id: 19962 reply_id: 78518[/import]

of course … and here’s the whole function

[code]
local cancel=10 – a tracing flag
local timerNew – timer to cancel the joint

prevPause=0 – a flag variable for pausing game
local notFall = 0 – a tracing variable for the stone if it hasn’t been thrown yet

local function onClick (event)

local phase = event.phase

function cancelEvent()
stones[c].tempJoint:removeSelf()
touched=false
cancel=cancel+1
print("cancel "…cancel)
notFall=1
end

if “began” == phase then
if(event.y>50) then
stones[c].bodyType = “dynamic”
stones[c].isBullet = true
touched = true
cancel=10
print(“began”)
notFall=0
deltaX = stones[c].x - event.x; – in order to drag the object from
deltaY = stones[c].y - event.y; – anywhere on the screen
stones[c].tempJoint = physics.newJoint( “touch”, stones[c], stones[c].x, stones[c].y )
Runtime:removeEventListener(“enterFrame”, moveStone) – as the stone is moving at some kind of path
timerf=timer.performWithDelay(50,cancelEvent,1) – to not holding the stone too much
timerNew=timer.performWithDelay(100,newRound,1) – generate a new stone

prevPause=0
end
elseif touched then
if(event.y > 10) then
stones[c].tempJoint:setTarget( event.x + deltaX, event.y + deltaY )
–///// calc. the velocity
vx, vy = stones[c]:getLinearVelocity()
print("velocityX : "…vx)
print("velocityY : "…vy)
if vx > 100 then
vx=100
elseif vx < -100 then
vx=-100
end
if vy>300 then
vy=300
elseif vy vy=-300
end

stones[c]:setLinearVelocity( vx, vy )

v1, v2 = stones[c]:getLinearVelocity()
print("new velocity X : "…v1)
print("new velocity Y : "…v2)

– ////////////////
print(“dragged”)
if notFall==0 and cancel==10 then
timer.cancel(timerf)
timer.cancel(timerNew)
timerf=timer.performWithDelay(50,cancelEvent,1)
timerNew=timer.performWithDelay(100,newRound,1)
notFall=1
end

end
if “ended” == phase then
if cancel==10 then
notFall=1
timer.cancel(timerf)
timer.cancel(timerNew)
timerNew=timer.performWithDelay(10,newRound,1)
cancelEvent()

end
end

end

end
sky:addEventListener(“touch”, onClick)
[/code] [import]uid: 96659 topic_id: 19962 reply_id: 78533[/import]

I would still say to take out 40 and to 54 and make them into a runtime listener. You function is only slow done the stone when you move your finger a certain way - only when event.y > 10 and your finger is not stationary. [import]uid: 54716 topic_id: 19962 reply_id: 78603[/import]

0ok i will try this separate listener

and there’s no stationary phase in the touch event !!
http://developer.anscamobile.com/reference/index/eventphase-0
… even when i tried it, it did nothing [import]uid: 96659 topic_id: 19962 reply_id: 78671[/import]

it’s not workin’ well … i can’t handle it with a separate listener
… could you show me how !?

thanks [import]uid: 96659 topic_id: 19962 reply_id: 78907[/import]

Sorry, I wasn’t clear. I meant to say that because touch events do not account for stationery events that there can be many situations in which your function would not limit the speed.

Here is a “sample” listener:
[lua]stone[c].limit = function(self, event)
local inst = self-- or stone[c]
local vx, vy = inst:getLinearVelocity()
local sqrt = math.sqrt
local curV = sqrt(vx * vx + vy * vy)
if curV >= 30 then --check for speed here
inst:setLinearVelocity(maxXVelocity, maxYVelocity)
end
end
Runtime:addEventListener(“enterFrame”, stone[c].limit)[/lua]

You would have this in you begin phase and remove it in your end/cancel phase. [import]uid: 54716 topic_id: 19962 reply_id: 79313[/import]

hi ir8primates
thx for your help…your listener made it works…, i can see now the deference in speed between using this listener and working without it, even the deference is kind of closer, as i’m poor at this physical rules…
so i’ll trace on setting the suitable max. X,Y velocity

thnx again [import]uid: 96659 topic_id: 19962 reply_id: 79406[/import]