Hi,
i have a problem with physics:
i have a wheel that can be rotated by drag and i apply angular impulse to it upon “ended” phase of touch
but now i cant figure out how to make it decelerate, it just goes on forever
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0,0)
physics.setDrawMode(“hybrid”)
– compute angle between two sets of points
local function angleBetweenPoints ( srcObjx, srcObjy, dstObjx, dstObjy )
– make sure we never return -1.#IND if we try to find angle between identical points
if ( srcObjx == dstObjx and srcObjy == srcObjy ) then return 0 end
– Original angleBetween
local xDist = dstObjx - srcObjx ; local yDist = dstObjy - srcObjy
local angleBetween = math.deg( math.atan( yDist / xDist ) )
if ( srcObjx < dstObjx ) then angleBetween = angleBetween + 90 else angleBetween = angleBetween - 90 end
– These tend to get flipped around, this is a quick fix
if ( angleBetween == 0 ) then angleBetween = -180
elseif ( angleBetween == -180 ) then angleBetween = 0
end
return angleBetween
end
– display the circle
local circle = display.newImageRect( “wheel.png”, 256, 256 )
circle.x = display.contentWidth / 2
circle.y = display.contentHeight / 2
physics.addBody(circle, {radius = 126})
– what to do when circle is touched
local function rotateObj(event)
local t = event.target
local phase = event.phase
if (phase == “began”) then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position of finger
t.x1 = event.x
t.y1 = event.y
elseif t.isFocus then
if (phase == “moved”) then
t.x2 = event.x
t.y2 = event.y
angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
print("angle1 = "…angle1)
rotationAmt = angle1 - angle2
–rotate it
t.rotation = t.rotation - rotationAmt
print ("t.rotation = "…t.rotation)
t.x1 = t.x2
t.y1 = t.y2
elseif (phase == “ended”) then
t:applyAngularImpulse( t.y1)
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
– Stop further propagation of touch event
return true
end
circle:addEventListener(“touch”, rotateObj)[/lua]
plug and play, just declare what you want in newImageRect line
any help is appreciated [import]uid: 16142 topic_id: 19937 reply_id: 319937[/import]