1

Details of program: by looking at this picture(https://i.stack.imgur.com/QOZ53.png), what I'm trying to do is have the spaceship circle the planet. I achieved this by making a line node and changes its anchor point to the top and then position it to the centre of the planet which then upon impact of the spaceship and planet, the line is angled towards the ship in which then the ship is removed from the view and added to the line node and together they rotate around the planet. (hopefully that makes sense)

Problem: the problem is after adding the ship to the line node, the coordinates of the spaceship get all weird, I'm not sure what's happening but if you take a look at the console logs, the x position and y position are totally off. The first "I'm touched" is activated at the beginning to make the spaceship go, the initial starting point of the ship is [100,100], once it comes in contact with a planet the "WE'RE TOUCHING" is activated and the position of the ship is reasonable [116.000015258789, 162.0]. The ship then starts to circle the planet but when i call "I'm touched" to get more info on the ships position, its totally off [4.1961669921875e-05, -59.0515747070312 ]. At this point I have no idea what's happening, for more info you can look at the code and console logs.

Cosnole Log:

I'm Touched ship angle in degrees: 72.9146667862848 PLAYER POSITION X: 100.0 Y: 100.0 *PLAYER ROTATION: 1.27260100841522

WE'RE TOUCHING PLANET POSITION X: 116.000015258789 Y: 162.0
PLAYER POSITION X: 101.464706420898 Y: 104.765426635742 ANGLE OF HANDLE RADIANS: -1.81949883426405 DEGRESS: -104.249604032303 *PLAYER ROTATION: 1.27260100841522

I'm Touched
ship angle in degrees: 10.6651491436781 PLAYER POSITION X: 7.33137130737305e-05 Y: -59.0515937805176 *PLAYER ROTATION: 0.186141967773438

I'm Touched
ship angle in degrees: 10.6651491436781 PLAYER POSITION X: 4.1961669921875e-05 Y: -59.0515747070312 *PLAYER ROTATION: 0.186141967773438

I'm Touched
ship angle in degrees: 10.6651491436781 PLAYER POSITION X: 3.4332275390625e-05 Y: -59.0515594482422 *PLAYER ROTATION: 0.186141967773438

I'm Touched
ship angle in degrees: 10.6651491436781 PLAYER POSITION X: 2.6702880859375e-05 Y: -59.0515365600586 *PLAYER ROTATION: 0.186141967773438

I'm Touched ship angle in degrees: 10.6651491436781 PLAYER POSITION X: 3.0517578125e-05 Y: -59.0515327453613 *PLAYER ROTATION: 0.186141967773438


This is the collision code:

  func didBeginContact(contact: SKPhysicsContact) {

    if contact.bodyA.categoryBitMask == planetGroup || contact.bodyB.categoryBitMask == planetGroup {

         print(" **WE'RE TOUCHING** ")

        moving = false
        touching = true

        let degrees = 45.0
        let radians = degrees * M_PI / 180.0

        var rotate = SKAction.rotateByAngle(CGFloat(radians), duration: 0.5)
        var repeat = SKAction.repeatActionForever(rotate)
        playerShip.runAction(repeat, withKey: "rotate")

        playerShip.physicsBody?.velocity = CGVector(dx: 0, dy: 0)


        planetNode = contact.bodyA.node as! SKSpriteNode


        planetX = planetNode.position.x
        planetY = planetNode.position.y

        playerX = playerShip.position.x
        playerY = playerShip.position.y


        var angleOfAnchor = AngleBetweenPoints(planetNode.position, endPoint: playerShip.position)
        var three60 = 360 * CGFloat(M_PI) / 180.0
        var nintey = 90 * CGFloat(M_PI) / 180.0
        var inDegree = angleOfAnchor * 180.0 / CGFloat(M_PI)

        var shipPlanetDistance = SDistanceBetweenPoints(planetNode.position, p2: playerShip.position)


        line = SKSpriteNode(color: UIColor.blackColor(), size: CGSize(width: 2, height: planetNode.size.height))
        line.anchorPoint = CGPoint(x: 0.5, y: 1)
        line.position = CGPoint(x: planetX, y: planetY)

        // this sets the angle of the line when the ship and planet collide
        line.zRotation = -(three60 - nintey - angleOfAnchor)

        self.addChild(line)
        playerShip.removeFromParent()
        angle.runAction(repeat, withKey: "rotate")

        line.addChild(playerShip)

        print("*PLANET POSITION* X: \(planetX) Y: \(planetY)  \r *PLAYER POSITION* X: \(playerX) Y: \(playerY) \r *ANGLE OF HANDLE* RADIANS: \(angleOfAnchor) DEGRESS: \(inDegree) *PLAYER ROTATION: \(playerShip.zRotation)")
    }

}

Screen Touch Code:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    print(" I'm Touched ")

    playerX = playerShip.position.x
    playerY = playerShip.position.y


        var radians:CGFloat = playerShip.zRotation
        var degrees = radians * 180.0 / CGFloat(M_PI)
        var dx = cos(radians)
        var dy = sin(radians)
        print(" ship angle in degrees: \(degrees) ")

       //playerShip.removeFromParent()
        //self.addChild(playerShip)

        //playerShip.position = CGPoint(x: playerX, y: playerY)


       // playerShip.physicsBody?.angularVelocity = 0 // forgot what this does
        playerShip.removeActionForKey("rotate")

    if moving == true {
        playerShip.physicsBody?.velocity = CGVector(dx: 100*dx, dy: 100*dy)// speed of direction
    }

        print("*PLAYER POSITION* X: \(playerX) Y: \(playerY) *PLAYER ROTATION: \(playerShip.zRotation)")




}
Rohit Kumar
  • 51
  • 1
  • 7

0 Answers0