How can I set a physicsbody to be properly centered?

早过忘川 提交于 2021-01-29 19:47:02

问题


Below is the code I use to create a red border in my scene. Well no matter how hard I try I can't get the border to fall straight down. It falls at a tilt. The commented lines are my attempts at trying to fix this problem.

And below all that are two screen shots, one without physicsbody, and the other with, but no color so you can see the physicsbody debug lines.

I should add that at this moment I am not doing any collision detection. The balls do have their own physics body, and I assume the tilted border touches the orange one first and therefore just stops.

What I want the border to do is fall straight, as if gravity is affect the entire bottom.

Any thoughts?

func createBorder () -> SKSpriteNode
{
    let mBorder = MyBorder(color: .clear, size: CGSize(
                            width: myGlobalVars.widthPoints,
    height: myGlobalVars.heightPoints * 0.01 ))

    mBorder.isHidden = true
    mBorder.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    mBorder.position = CGPoint(x: (myGlobalVars.widthPoints/2), y: myGlobalVars.heightPoints * 0.2)
    
    let bSize = CGSize(width: myGlobalVars.widthPoints, height: myGlobalVars.heightPoints * 0.01 )
    let bPoint = CGPoint(x: bSize.width/2, y: bSize.height / 2 )
    
    mBorder.physicsBody = SKPhysicsBody(rectangleOf: bSize)
    //    mBorder.physicsBody = SKPhysicsBody(rectangleOf: bSize, center: bPoint)
    //    mBorder.physicsBody!.affectedByGravity = false
    //    mBorder.physicsBody?.isDynamic = false
    
    mBorder.zPosition = theZ.theBoarder
    mBorder.isHidden = false
    myGlobalVars.gameScene!.addChild(mBorder)

    return mBorder
}

来源:https://stackoverflow.com/questions/63986142/how-can-i-set-a-physicsbody-to-be-properly-centered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!