issues balls sticking to rotating platform

Take a look at the docs - it’s all there, with plenty of examples. You want the animation section.

In short, transition.to will modify an object’s property value from it’s current value to some destination value over a specified time. This is called tweening.

If you look in the code exchange you’ll also see a pausable transitions library I wrote to provide, as it says, a pausable (and reversible) transition api because the built in Corona transitions cannot be stopped once started (at least, without being able to restart) although they can be cancelled. [import]uid: 8271 topic_id: 6677 reply_id: 26979[/import]

Ah I can see how this would work - Thank you.

I’ll go and toy with it and see how it goes. [import]uid: 42777 topic_id: 6677 reply_id: 26982[/import]

I never took the above route because I couldn’t figure out anything other than changing x,y, and rotation on enterFrame that would work for what I was doing, but in the hope of perhaps being given a better alternative I’ll put it out there. What I have works, but every once in awhile I get hit with funky physics because I am fighting the engine.

I want to have a platform that goes in a “perfect circle” (I am doing geometric calculations for position to make this happen now). It moves along this perfect circle based on input from the accelerometer. As it is moving, the platform constantly updates its rotation so that it is facing the center.

So if the platform is at the top or bottom, it will be like this: ____
If it is on the left or right, it will be like this: |
If it is between any of those, it will be some degree of this: / or \

Last but not least, being hit by or colliding with other objects (balls, people, etc.) cannot affect the platform, but should affect the other object normally.

Any ideas beyond just constantly updating x,y, and rotation and fighting the physics engine? [import]uid: 36054 topic_id: 6677 reply_id: 27008[/import]

Can somebody help me? I have also rotating image and there is ball on it. I can rotate image with moving finger. Image rotates but when ball starts moving on image, ball goes away with lag. It sticks to image. So can somebody help me, that ball moves smoothly on rotating image.
Here is my code:

[code]
local lauta = display.newImageRect (“lauta2.png”,200,15)
lauta.x = display.contentWidth/2
lauta.y = display.contentHeight/1.7
physics.addBody(lauta, “kinematic”, {friction = 0})
localGroup:insert(lauta)

local max = math.max
local min = math.min
local centerX = display.contentWidth/2

local function rotate(event)
local r = centerX - event.x
r = min(r, 270)
r = max(r, -270)
lauta.rotation = r/9
end

Runtime:addEventListener( “touch”, rotate) [import]uid: 18445 topic_id: 6677 reply_id: 29459[/import]

Post number 15 is what solved my problem. [import]uid: 32061 topic_id: 6677 reply_id: 29460[/import]

I cant get it working. When i launch it, corona simulator crashes. Help, please. [import]uid: 18445 topic_id: 6677 reply_id: 29462[/import]

Well, I’m completely against using transitions or transforms to control physics engine objects for reasons stated above and given by Ansca themselves since year dot.

What you can do is use the joint motor. Give this serious consideration.

If you need more control, create an appropriate joint somewhere on the object and connect it with either a static object (which you can move using transitions because it’s not fighting Box2D) or use the touch joint to force that point on the object to a specific location.

Let me know if you’d like a code sample of either. [import]uid: 8271 topic_id: 6677 reply_id: 100011[/import]

@horacebury I’ve been running into a similar issue and would live to see your sample code. [import]uid: 14218 topic_id: 6677 reply_id: 100014[/import]

Hope this helps…

main.lua:
[lua]-- https://developer.anscamobile.com/content/game-edition-physics-joints
https://developer.anscamobile.com/code/maths-library
– engage
physics = require(“physics”)
physics.start()
physics.setGravity(0,10)
physics.setDrawMode(“hybrid”)

– the anchor
local anchor = display.newCircle( 5,5,5 )

– the squares
local red, green, blue = display.newRect( 0, 0, 100, 100 ), display.newRect( 0, 0, 100, 100 ), display.newRect( 0, 0, 100, 100 )
red.x, red.y = 100, 300
green.x, green.y = 300, 300
blue.x, blue.y = 500, 300
red:setFillColor( 255, 0, 0 )
green:setFillColor( 0, 255, 0 )
blue:setFillColor( 0, 0, 255 )

– the physics bodies
physics.addBody( anchor, “static” )
physics.addBody( red, “dynamic” )
physics.addBody( green, “dynamic” )
physics.addBody( blue, “dynamic” )

– the physics joints - hold the wheels in place
local redwheel = physics.newJoint( “pivot”, anchor, red, red.x, red.y )
local greenwheel = physics.newJoint( “pivot”, anchor, green, green.x, green.y )
local bluewheel = physics.newJoint( “pivot”, anchor, blue, blue.x, blue.y )

– drive the red wheel with a motor (the easy one)
redwheel.isMotorEnabled = true
redwheel.maxMotorTorque = 100000
redwheel.motorSpeed = 100

– touch joints to move the green and blue squares with
local greentouch = physics.newJoint( “touch”, green, green.x, green.y+100 )
local bluetouch = physics.newJoint( “touch”, blue, blue.x, blue.y+100 )

– create a display group to be used as a rotation reference for the green square
local rotategroup = display.newGroup()
rotategroup.x, rotategroup.y = green.x, green.y

– display group powered rotation of the green square
function rotateGreen()
– rotate the display group
rotategroup.rotation = rotategroup.rotation + 15

– convert a fixed point in the group to a world coordinate location
local x, y = rotategroup:localToContent( 0, 100 )

– perform the touch joint rotation of the blue wheel
greentouch:setTarget( x, y )
end

– function to calculate rotation (used to rotate the blue square)
– taken and modified slightly from my math library: https://developer.anscamobile.com/code/maths-library
function rotatePoint( x, y, degrees )
local theta = math.rad( degrees )
local px = x * math.cos(theta) - y * math.sin(theta)
local py = x * math.sin(theta) + y * math.cos(theta)
return px, py
end

– calculation-powered rotation of the blue square
function rotateBlue()
– calculate the rotation from the current rotation
local x, y = rotatePoint( 0, 100, blue.rotation + 15 ) – rotates around 0,0

– perform the touch joint rotation of the blue square
bluetouch:setTarget( x + blue.x, y + blue.y )
end

– timers to call the rotation functions
timer.performWithDelay( 100, rotateGreen, 0 )
timer.performWithDelay( 100, rotateBlue, 0 )

– drops a ball into the world
– gets auto-removed if it drops off the bottom of the world
function dropBall( event )
local ball = display.newCircle( event.x, event.y, 30 )
ball:setFillColor( 0,0,0,0 )
ball:setStrokeColor( 255, 0, 0, 255 )
ball.strokeWidth = 5
physics.addBody( ball, “dynamic”, { radius=30} )
function ball:timer()
if (ball.y > display.contentHeight + 200) then
print(‘removed’)
timer.cancel( ball.t )
ball.t = nil
ball:removeSelf()
end
end
ball.t = timer.performWithDelay( 1000, ball, 0 )
return true
end

Runtime:addEventListener( “tap”, dropBall )

– lets you rotate the squares
function touch( event )
if (event.phase == “began”) then
event.target.touchjoint = physics.newJoint( “touch”, event.target, event.x, event.y )
event.target.touchjoint.dampingRatio = 0.2
event.target.touchjoint.frequency = 50
event.target.touchjoint.maxForce = 5000
display.getCurrentStage():setFocus( event.target )
elseif (event.phase == “moved”) then
print(event.phase, event.x, event.y )
event.target.touchjoint:setTarget( event.x, event.y )
else
event.target.touchjoint:removeSelf()
event.target.touchjoint = nil
display.getCurrentStage():setFocus( nil )
end
return true
end

– add touch listeners to allow the user to rotate the squares
red:addEventListener( “touch”, touch )
green:addEventListener( “touch”, touch )
blue:addEventListener( “touch”, touch )[/lua]
[import]uid: 8271 topic_id: 6677 reply_id: 100081[/import]

@horacebury thanks for posting your code example. Like others here, we made a ball drop from the top of the screen onto a rotating platform. The ball would hit the ground and bounce before it settled, but it would stick to the rotating platform in a very strange way.

I have now used your anchor point method and this works well with a stationary platform. I added a touch listener to rotate the platform by a few degrees, and again the balls bounce off it at the correct angle - no sticking.

However, as soon as I try and use the enterFrame runtime listener - this is when we get problems. As you suggest, it performs too many operations - too many for the physics to function correctly. You say about performing a transform rotation every 4 seconds, but what if someone wanted a faster spinning platform?

Has anyone else managed to find an appropriate solution - please share your code so we can all learn from this.

Thanks [import]uid: 140429 topic_id: 6677 reply_id: 99889[/import]

Damn, I was about to jump in to make some suggestions, but that damn horacebury character came in and made things right. How dare he be helpful and provide insight, how dare he come in here and be a community helper.

lol.

Ok seriously, i’m done. As you have found out, Box2d doesn’t play nice sometimes with things. Using the joint motor you will get more of what you may be after.

I’ve had issues like this and I had to take a different approach, which horacebury is already suggesting.

ng [import]uid: 61600 topic_id: 6677 reply_id: 100373[/import]