Physics Body issue with Anchor Points

Hello,

I’m working with anchor points on a project. Here is the sample code that represents the usage of anchor points. For some reason, when the transition begins, the earth’s physics body acts stationary.

1- The issue needs to be done with anchor points.

2- Also, I attached two png files that shows the transition pause/start stages. 

[lua]local physics = require ( “physics” )
physics.start()
physics.setDrawMode(“hybrid”)
physics.setGravity(0.0, 0.0)

display.setDefault( “isAnchorClamped”, false )


– Initialize the variables

local width = display.contentWidth
local height = display.contentHeight

local startTimer
local startTimer2

local timerControl = false
local timerControl2 = false

– Initialize the objects and physics -> Sun, earth

local sun = display.newImageRect(“sun.png”, 200, 200)
sun:translate(width / 2, height / 2)

local earth = display.newImageRect(“earth.png”, 50, 50)
earth:translate(sun.x, sun.y)

earth.anchorX = 5

physics.addBody(sun, “static”, { radius = 45 })
physics.addBody(earth, “static”, { radius = 23 })


– Initialize the functions

function moveRight()
     if (earth.anchorX > 3 and not timerControl) then
        earth.anchorX = earth.anchorX - 0.1

     else
        timerControl = true
        timerControl2 = true
     end
end

function moveLeft()
     if (earth.anchorX < 5 and timerControl2) then
        earth.anchorX = earth.anchorX + 0.1
     else
        timerControl = false
        timerControl2 = false
     end
end

function initializeTimer()
     startTimer = timer.performWithDelay(50, moveRight, -1)
     startTimer2 = timer.performWithDelay(50, moveLeft, -1)
end

transition.to( earth, { iterations = -1, onStart = initializeTimer } )[/lua]

Both the physics bodies are static. You are changing the anchor values of the display objects, not the physics bodies.

If you are trying to move the physics bodies and have the images move with them, you want to simply change the x and y values, not the anchorX and anchorY values, or the objects.

It kinda looks like you’re going about a very simple operation in the most difficult way.

#1 Why does the issue have to be done with anchor points?

Hi @spritekit,

It’s mentioned in the documentation:

“If you change the anchorX or anchorY property of a display object before adding a physics body, the body will be positioned properly relative to the adjusted anchor. However, you should not change the anchor point after adding the body or the object and body will be misaligned.”

Is your goal to have the Earth rotate around the sun? However it can change distances from the sun? I can probably advise ways for you to solve this scenario if you provide more details on your goals.

Best regards,

Brent

Hi @Brent Sorrentino,

I did the same thing that is mentioned in the documentation. I mean, I didn’t implement the anchorX property after adding a physics body. Before the movement, the body positions relative to the adjusted anchor, properly. However, during the movement, physics body still exists in the same place when the Earth is moving right or left. I think, it might be a bug.

Basically, my goal looks like the Earth rotates around the sun and also, there are livings that moves on the Earth, randomly. In this way, the livings and the Earth need to rotate around the Sun, properly. The important one is all the objects must have physics bodies.

Hi @spritekit,

OK, I may suggest one alternative is to use a long “bar” that remains fixed to the center point of the sun, and then attach the earth to that bar with a “pivot” joint. That would allow you to rotate the bar at any speed you see necessary, and the earth would smoothly travel around the sun. If, at some point, you wanted to change the distance of the earth from the sun, you could break (destroy) the pivot joint, move the earth some distance along the span of the bar, then re-create a new pivot joint.

Here’s a very simple code example of this:

[lua]

physics.setGravity( 0,0 )

local sun = display.newCircle( display.contentCenterX, display.contentCenterY, 30 )

physics.addBody( sun, “static”, { radius=30 } )

local rotator = display.newRect( display.contentCenterX, display.contentCenterY, 320, 10 )

physics.addBody( rotator, “kinematic”, { isSensor=true } )

rotator.isVisible = false

local earth = display.newCircle( display.contentCenterX, display.contentCenterY, 20 )

earth.isFixedRotation = true

physics.addBody( earth, “dynamic”, { radius=20 } )

local earthJoint = physics.newJoint( “pivot”, earth, rotator, earth.x, earth.y )

rotator.angularVelocity = 20

[/lua]

Brent

Both the physics bodies are static. You are changing the anchor values of the display objects, not the physics bodies.

If you are trying to move the physics bodies and have the images move with them, you want to simply change the x and y values, not the anchorX and anchorY values, or the objects.

It kinda looks like you’re going about a very simple operation in the most difficult way.

#1 Why does the issue have to be done with anchor points?

Hi @spritekit,

It’s mentioned in the documentation:

“If you change the anchorX or anchorY property of a display object before adding a physics body, the body will be positioned properly relative to the adjusted anchor. However, you should not change the anchor point after adding the body or the object and body will be misaligned.”

Is your goal to have the Earth rotate around the sun? However it can change distances from the sun? I can probably advise ways for you to solve this scenario if you provide more details on your goals.

Best regards,

Brent

Hi @Brent Sorrentino,

I did the same thing that is mentioned in the documentation. I mean, I didn’t implement the anchorX property after adding a physics body. Before the movement, the body positions relative to the adjusted anchor, properly. However, during the movement, physics body still exists in the same place when the Earth is moving right or left. I think, it might be a bug.

Basically, my goal looks like the Earth rotates around the sun and also, there are livings that moves on the Earth, randomly. In this way, the livings and the Earth need to rotate around the Sun, properly. The important one is all the objects must have physics bodies.

Hi @spritekit,

OK, I may suggest one alternative is to use a long “bar” that remains fixed to the center point of the sun, and then attach the earth to that bar with a “pivot” joint. That would allow you to rotate the bar at any speed you see necessary, and the earth would smoothly travel around the sun. If, at some point, you wanted to change the distance of the earth from the sun, you could break (destroy) the pivot joint, move the earth some distance along the span of the bar, then re-create a new pivot joint.

Here’s a very simple code example of this:

[lua]

physics.setGravity( 0,0 )

local sun = display.newCircle( display.contentCenterX, display.contentCenterY, 30 )

physics.addBody( sun, “static”, { radius=30 } )

local rotator = display.newRect( display.contentCenterX, display.contentCenterY, 320, 10 )

physics.addBody( rotator, “kinematic”, { isSensor=true } )

rotator.isVisible = false

local earth = display.newCircle( display.contentCenterX, display.contentCenterY, 20 )

earth.isFixedRotation = true

physics.addBody( earth, “dynamic”, { radius=20 } )

local earthJoint = physics.newJoint( “pivot”, earth, rotator, earth.x, earth.y )

rotator.angularVelocity = 20

[/lua]

Brent