Stop force

This might be alittle confusing but any help is appreciated!

Lets say i am using the simple pool example and i made a timer so 10 seconds after i hit the ball (applied force) it goes back to the original point. I made this already but when it goes back to the origin it still has to force that it hits the ball. I just want it to stop when it is sent to the origin…

So basically i am asking how can i stop a force that is being applied?
Thanks,
Brian
[import]uid: 24708 topic_id: 14934 reply_id: 314934[/import]

Try this

ball:setLinearVelocity(0,0)

Let me know if that works I think it will

Larry

DoubleSlashDesign.com [import]uid: 11860 topic_id: 14934 reply_id: 55154[/import]

No, it did not work

This is the code i am using to strike the ball

t:applyForce( ( event.x - t.x ), ( event.y - t.y ), t.x, t.y ) [import]uid: 24708 topic_id: 14934 reply_id: 55184[/import]

Why i have been thinking is that maybe that i could find out the velocity of the ball and set an opposite linear velocity to it.

ball:setLinearVelocity(0,0)

Would anyone know how to do this? [import]uid: 24708 topic_id: 14934 reply_id: 55217[/import]

I am not sure but you may try physics.stop() and then start it again. Try it. [import]uid: 46529 topic_id: 14934 reply_id: 55221[/import]

The physics.stop works, but i dont think the start is. It stops the velocity of the ball, but doesnt allow me to hit it again. Thanks for the input! [import]uid: 24708 topic_id: 14934 reply_id: 55222[/import]

It seems physics.start() could be used only at the very beginning of the main.lua.

http://developer.anscamobile.com/reference/index/physicsstart :

Note:
require( “physics” ) and physics.start() must be at the top of the main.lua file.

Ok, here is another idea :
http://developer.anscamobile.com/reference/index/physicsremovebody

First remove the body of the object and then add a new body again. Try it and tell me the result. [import]uid: 46529 topic_id: 14934 reply_id: 55260[/import]

This following code didnt work

physics.removeBody( cueball )  

I tried this code and it gave me the following errors

attempt to call field ‘removeBody’ (a nil value)

Am i using the code wrong? [import]uid: 24708 topic_id: 14934 reply_id: 55265[/import]

It seems there is not an object called “physics” at the scope of your removeBody code. You have to be requiring “physics” api, something like as ‘local physics = require(“physics”)’.

This is my smart guess, I can not tell what is wrong without seeing some code. [import]uid: 46529 topic_id: 14934 reply_id: 55269[/import]

This is the code to hit the ball from simple pool example, with a few changes.

---------------------------------------------Hit--------------------  
-------------------------------------------the ball--------------------  
  
local cueball = display.newImage( "levelimages/golfball.png" )  
 cueball.x = 57; cueball.y = 150  
 cueball.xScale = 1   
 cueball.yScale = 1,  
 physics.addBody( cueball, {radius = 6} )  
 cueball.linearDamping = .9  
 cueball.angularDamping = .8  
 cueball.isBullet = true -- force continuous collision detection, to stop really fast shots from passing through other balls  
 cueball.color = "white"  
  
 local target = display.newImage( "target.png" )  
 target.x = cueball.x; target.y = cueball.y; target.alpha = 0  
 target.xScale = 1  
 target.yScale = 1  
  
 -- Shoot the cue ball, using a visible force vector  
 local function cueShot( event )  
 local t = event.target  
  
 local phase = event.phase  
 if "began" == phase then  
 display.getCurrentStage():setFocus( t )  
 t.isFocus = true  
  
 -- Stop current cueball motion, if any  
 t:setLinearVelocity( 0, 0 )  
 t.angularVelocity = 0   
  
 target.x = t.x  
 target.y = t.y  
  
 startRotation = function()  
 target.rotation = target.rotation + 1  
  
 end  
  
 Runtime:addEventListener( "enterFrame", startRotation )  
  
 local showTarget = transition.to( target, { alpha=0.4, xScale=0.2, yScale=0.2, time=200 } )  
 myLine = nil  
  
 elseif t.isFocus then  
 if "moved" == phase then  
  
 if ( myLine ) then  
 myLine.parent:remove( myLine ) -- erase previous line, if any  
 end  
 myLine = display.newLine( t.x,t.y, event.x,event.y )  
 myLine:setColor( 255, 255, 255, 50 )  
 myLine.width = 8  
  
 elseif "ended" == phase or "cancelled" == phase then  
 display.getCurrentStage():setFocus( nil )  
 t.isFocus = false  
  
 local stopRotation = function()  
 Runtime:removeEventListener( "enterFrame", startRotation )  
 end  
  
 local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )  
  
 if ( myLine ) then  
 myLine.parent:remove( myLine )  
 end  
  
 -- Strike the ball!  
 t:applyForce( ( event.x - t.x ), ( event.y - t.y ), t.x, t.y )  
 audio.play(hitsound)  
  
 end  
 end  
  
 -- Stop further propagation of touch event  
 return true  
 end  

And this is the timer that resets the cueball

[code]
local function listener( event )
if(right == “true”) then

cueball.x = 57
cueball.y = 150

print( “listener called” )
right = “false”
end
end

timer.performWithDelay( 100, listener, -1 )

[/code] [import]uid: 24708 topic_id: 14934 reply_id: 55313[/import]

i got it guys thanks for the help! [import]uid: 24708 topic_id: 14934 reply_id: 55340[/import]