Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[ad_1]

I’m attempting to maneuver two traces joint to a hard and fast place sprite in a manner just like that of a pendulum. I would love the traces to swing from finish to finish every time. The picture under exhibits what I wish to obtain:

enter image description here

I am shifting the traces utilizing angular velocity, however I am not getting the pendulum/swinging impact (a cycle of the traces usually swinging sooner as they transfer to the middle and slowing down a bit once they change from one course to a different). I slightly get some brisk motion then it slows down until the motion stops. Please see my SpriteKit code under:

extension Int {
  var degreesToRadians: Double { return Double(self) * .pi / 180 }
}
extension FloatingPoint {
  var degreesToRadians: Self { return self * .pi / 180 }
  var radiansToDegrees: Self { return self * 180 / .pi }
}

class GameScene: SKScene, SKPhysicsContactDelegate {

var anchorSprite = SKSpriteNode(imageNamed: "swingPin")
var armLeft = SKSpriteNode(imageNamed: "swingArm")
var armRight = SKSpriteNode(imageNamed: "swingArm")

override func didMove(to view: SKView) {


self.physicsWorld.gravity = CGVector(dx: 0, dy: -1.8)
self.physicsWorld.contactDelegate = self

var tealBg = SKSpriteNode(imageNamed: "tealBg")
tealBg.place = CGPoint(x: body.midX, y: body.midY)
tealBg.zPosition = 10
addChild(tealBg)

anchorSprite.place = CGPoint(x: body.midX, y: body.midY + body.midY/2)
anchorSprite.zPosition = 20

anchorSprite.physicsBody = SKPhysicsBody(rectangleOf: anchorSprite.body.dimension)
anchorSprite.physicsBody?.categoryBitMask = pinCategory
anchorSprite.physicsBody?.isDynamic = false
addChild(anchorSprite)

armRight.anchorPoint = CGPoint(x: 0.5, y: 1)
armRight.place = anchorSprite.place
armRight.zPosition = 20
armRight.physicsBody = SKPhysicsBody(rectangleOf: armRight.body.dimension)
armRight.zRotation = CGFloat(Double(15).degreesToRadians)//CGFloat(Double.pi/6)
armRight.physicsBody!.isDynamic = true

addChild(armRight)

armLeft.anchorPoint = CGPoint(x: 0.5, y: 1)
armLeft.place = anchorSprite.place
armLeft.zPosition = 20
armLeft.physicsBody = SKPhysicsBody(rectangleOf: armRight.body.dimension)
armLeft.zRotation = CGFloat(Double(-15).degreesToRadians)//CGFloat(-Double.pi/6)
armLeft.physicsBody!.isDynamic = true
addChild(armLeft)

// Create joint between two objects
//Pin joint
var pinAndRightArmJoint = SKPhysicsJointPin.joint(withBodyA: anchorSprite.physicsBody!, bodyB: armRight.physicsBody!, anchor: CGPoint(x: anchorSprite.place.x, y: self.armRight.body.maxY))
self.physicsWorld.add(pinAndRightArmJoint)

var pinAndLeftArmJoint = SKPhysicsJointPin.joint(withBodyA: anchorSprite.physicsBody!, bodyB: armLeft.physicsBody!, anchor: CGPoint(x: anchorSprite.place.x, y: self.armLeft.body.maxY))
self.physicsWorld.add(pinAndLeftArmJoint)

var fixArms = SKPhysicsJointFixed.joint(withBodyA: armLeft.physicsBody!, bodyB: armRight.physicsBody!, anchor: CGPoint.zero)
self.physicsWorld.add(fixArms)

pinAndRightArmJoint.shouldEnableLimits = true
pinAndRightArmJoint.lowerAngleLimit = CGFloat(Double(-60).degreesToRadians)
pinAndRightArmJoint.upperAngleLimit = CGFloat(Double(60).degreesToRadians)

}


override func touchesBegan(_ touches: Set<UITouch>, with occasion: UIEvent?) {
armRight.physicsBody?.angularVelocity = -100.0

}

Please word that although I consider that is essentially the most acceptable method to my drawback, I am open to a different method that may assist me obtain the identical consequence.

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *