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

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

Peach :)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
                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<-300 then
                        vy=-300
                end     
                stones[c]:setLinearVelocity( vx, vy )
                
                v1, v2 = stones[c]:getLinearVelocity()
                print("new velocity X : "..v1)
                print("new velocity Y : "..v2)

i couldn't bump the velocity down using applyForce() !!

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.

of course ... and here's the whole function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        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<-300 then
                           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)

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.

views:1624 update:2012/1/9 8:53:30
corona forums © 2003-2011