Make SKSpriteNode character move with SKSpriteNode platform - swift

I'm building a platform game that has an SKSpriteNode character who jumps onto moving platforms.
When the platform moves the character doesn't and it ends up falling off the platform. If I tap the move button the character moves along nicely, but I want the character to 'stick' to the platform as it moves.
I haven't posted code as the code is working as expected. I have a feeling its a property that I can set?
EDIT:
I have a solution - just not sure it's how people should do it so I'll post below and wait for feedback. It's long and complex and probably doesn't need to be.
When the user presses the left or right directional button I move the scene from GameScene.swift by:
func moveGround(direction: String) {
if direction == "left" {
[snip...]
// move the platforms
self.enumerateChildNodesWithName("platform") {
node, stop in
if let foundNode = node as? PlatformSprite {
node.position.x += Helper().kMovingDistance
}
}
// move all other nodes
self.enumerateChildNodesWithName("*") {
node, stop in
if let foundNode = node as? SKSpriteNode {
node.position.x += Helper().kMovingDistance
}
}
[snip...]
} else if direction == "right" {
[snip...]
// move the platforms
self.enumerateChildNodesWithName("platform") {
node, stop in
if let foundNode = node as? PlatformSprite {
node.position.x -= Helper().kMovingDistance
}
}
// move all other nodes
self.enumerateChildNodesWithName("*") {
node, stop in
if let foundNode = node as? SKSpriteNode {
node.position.x -= Helper().kMovingDistance
}
}
[snip...]
}
This moves the scene along nicely. Then when the character lands on top of a platform I initiate the platform movements using SKAction sequence AND initiate the scene movements by sending the reverse sequence to GameScene:
func startMoving() {
if !self.isMoving {
[snip...]
// get the platform actions and initiate the movements
let actions = self.getMovements()
let seq:SKAction = SKAction.sequence(actions)
// move the platform
self.runAction(seq, completion: { () -> Void in
self.completedPlatformActions()
})
// get the reverse actions and initiate then on the scene / ground
let reverseActions = self.getReverseMovements()
let reverseSeq:SKAction = SKAction.sequence(reverseActions)
delegate!.moveGroundWithPlatform(reverseSeq)
self.isMoving = true
}
}
Then I have a function for moving the ground with a platform and add a key to the runAction so I can stop just that action and not the platforms actions if the user ceases to be in contact with the platform:
func moveGroundWithPlatform(seq: SKAction) {
[snip...]
self.enumerateChildNodesWithName("platform") {
node, stop in
if let foundNode = node as? PlatformSprite {
node.runAction(seq, withKey: "groundSeq")
}
}
[snip...]
}
Then I stop moving the scene, but let the remaining actions of the platform continue using:
func stopMovingGroundWithPlatform() {
[snip...]
self.enumerateChildNodesWithName("platform") {
node, stop in
if let foundNode = node as? PlatformSprite {
node.removeActionForKey("groundSeq")
}
}
[snip...]
}
Ugly I know - if others have suggestions about how better to do this I'd love to know :)

If you want friction to move your character along with the platform, you will need to move the platform with a force, impulse, or by settings its velocity. I suspect it's easier to control a platform by settings its velocity. You can do that in the update method by
platform.physicsBody?.velocity = CGVectorMake(dx,dy)
where dx and dy control the platforms speed in the x and y directions, respectively. You should also set the friction property of the platform's physics body as well.

Related

Scenekit: rotate camera to tap point (3D)

I have a camera node.
Around the camera node, there is another big node (.obj file) of a building.
User can move inside the building.
User can do LongPressGesture, and additional node (let's say a sphere) appears on the wall of the building. I want to rotate my camera to this new node (to tap location).
I don't know how to do it. Can someone help me?
Other answers are not correct for me. Camera just rotates in random directions.
I've found a way!
I take the location of a tap (or any coordinates you need to turn to)
#objc private func handleLongPress(pressRec: UILongPressGestureRecognizer) {
let arr: [UIGestureRecognizer.State] = [.cancelled, .ended, .failed]
if !arr.contains(pressRec.state) {
let touchPoint = pressRec.location(in: sceneView)
let hitResults = sceneView.hitTest(touchPoint, options: [:])
if let result: SCNHitTestResult = hitResults.first {
createAnnotation(result.worldCoordinates)
pressRec.state = .cancelled
}
}
}
func for turn camera:
func turnCameraTo(worldCoordinates: SCNVector3) {
SCNTransaction.begin()
SCNTransaction.animationDuration = C.hotspotAnimationDuration
cameraNode.look(at: worldCoordinates)
sceneView.defaultCameraController.clearRoll()
SCNTransaction.completionBlock = {
}
SCNTransaction.commit()
}

swift Mac OS: make trackpad reflect display size for NSTouch events

I am trying to make touch events take the trackpad as the display when moving it.
What I mean in this is:
Consider a Mac laptop display, and imagine it to be exactly the size of the trackpad, or so this is how I want the touch events to be implemented. There was a normalised x y coordinate property before in NSEvent which may have given me this, but it seems to have been deprecated.
Below are touchBegan and touchMoved overrides and what I mean:
override func touchesBegan(with event: NSEvent) {
CGDisplayMoveCursorToPoint(CGMainDisplayID(), event.location(in: overlayScene))
let player = SCNAudioPlayer(source: tick)
sineNode.addAudioPlayer(player)
}
override func touchesMoved(with event: NSEvent) {
let location = event.location(in: overlayScene)
guard let node = overlayScene.nodes(at: location).first else{
if currentSKNode != nil{
currentSKNode = nil
}
return
}
if currentSKNode != node{
currentSKNode = node
print(node.name)
let player = SCNAudioPlayer(source: tick)
sineNode.addAudioPlayer(player)
}
}
Imagine a fgraph reader where x and y axes are centred at exactly width/2 and height/2 of the trackpad. When I touch anywhere on it, this should reflect exactly on the apps window which is set to full screen.
Currently, the mouse cursor seems to go only partially across the display when I make a full left to right move, hence trying to reposition the mouse cursor to the position of the NSView, or in this case the SKScene.
Ok, so it turns out, as Kent mentioned, normalised position is not deprecated, but I was looking into NSEvent not NSTouch.
Here's the code for anyone who would stumble upon the same requirements. Works fine, just need to figure out now how to make this as responsive as possible.
override func touchesMoved(with event: NSEvent) {
DispatchQueue.main.async {
let touch = event.touches(matching: .moved, in: self.sceneView).first
let normalised = touch!.normalizedPosition
let translated = CGPoint(x: self.width*normalised.x - self.widthCenter, y: self.height*normalised.y - self.heightCenter)
guard let node = self.overlayScene.nodes(at: translated).first else{
self.currentSKNode = nil
return
}
if self.currentSKNode != node{
self.currentSKNode = node
let player = SCNAudioPlayer(source: self.tick)
self.sineNode.addAudioPlayer(player)
}
}
}

GameplayKit find path to moving sprite

I want a sprite to follow the player and I tried to achieve this with the Pathfinding of GameplayKit. This is working, but there's problem. I get the path from the enemy sprite to the player sprite with the following code:
func moveEnemy(to playerPos: CGPoint){
guard !moving else { return }
moving = true
let startNode = GKGraphNode2D(point: float2(Float(self.position.x), Float(self.position.y)))
let endNode = GKGraphNode2D(point: float2(Float(playerPos.x), Float(playerPos.y)))
PhysicsHelper.graph.connectUsingObstacles(node: startNode)
PhysicsHelper.graph.connectUsingObstacles(node: endNode)
let path = PhysicsHelper.graph.findPath(from: startNode, to: endNode)
guard path.count > 0 else{ return }
var actions = [SKAction]()
for node in path{
if let point2d = node as? GKGraphNode2D{
let point = CGPoint(x:CGFloat(point2d.position.x) , y:CGFloat(point2d.position.y))
let action = SKAction.move(to: point, duration: 1)
actions.append(action)
}
}
let seq = SKAction.sequence(actions)
self.run(seq) {
self.moving = false
}
}
This is called in another function of my enemy in this block:
case .follow:
if allowMovement{
if self.action(forKey: "Hurt") == nil{
// orientEnemy(to: playerPos)
moveEnemy(to: playerPos)
// animateWalk()
}
}
and this function is called in the update function of my GameScene in this block:
if enemy.checkCircularIntersection(player: player, node: enemy, radius: 40){
print("Enemy is close to player")
enemy.update(playerPos: player.position)
}
The checkCircularIntersection function only checks, if the player is near the enemy.
My problem is that the enemy follows the player but when the player moves, the enemy moves to the point where the player stands before, then it stops for a second and then it moves again to the point where the player stood. But the player is already moved away.
Is there a way to let the enemy permanently follow the player and avoid obstacles without stopping?
Your code specifically guards against moving again until the existing move is completed.
guard !moving else { return }
moving = true
If you want to use paths you will have to recompute and reapply the path movement regularly to account for the target's movement.
However, to achieve your aim, Paths are not the most suitable GameKit tool to use. GKAgents are specifically designed for that kind of thing. It will require a significant refactoring of your code, but take a look at Agents, Goals and Behaviours.

TouchesMoved Lag with Pathfinding

I'm making a game where the player follows a touch. The game has obstacles and pathfinding, and I'm experiencing extreme lag with touches moved. Upon loading the game, everything works, but after a few seconds of dragging my finger around (especially around obstacles) the game freezes to the point where moving a touch will freeze all physics (0 fps) until the touch is ended.
I'm assuming the culprit is my function makeGraph(), which finds a path for the player from player.position:CGPoint() to player.destination:CGPoint(), storing a path in player.goto:[CGPoint()], the update function then takes care of moving to the next goto point.
This function is called every cycle of touchesmoved, so the player can switch directions if the finger moves over an obstacle
My question is: 1) what in this code is causing the unbearable lag, 2) how can I make this code more efficient?
My code:
initializing vars:
var obstacles = [GKPolygonObstacle(points: [float2()])]
var navgraph = GKObstacleGraph()
setting up a graph (called at start of level):
func setupGraph(){
var wallnodes :[SKSpriteNode] = [SKSpriteNode]()
self.enumerateChildNodes(withName: "wall") {
node, stop in
wallnodes.append(node as! SKSpriteNode)
}
self.enumerateChildNodes(withName: "crate") {
node, stop in
wallnodes.append(node as! SKSpriteNode)
}
obstacles = SKNode.obstacles(fromNodeBounds: wallnodes)
navgraph = GKObstacleGraph(obstacles: obstacles, bufferRadius: Float(gridSize/2))
}
touchesmoved: I keep track of multiple touches using strings. only one finger may act as the "MoveFinger." this string is created at touchesbegan and emptied at touchesended
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
touchloop: for touch in touches {
if MoveFinger == String(format: "%p", touch) {
let location = touch.location(in: self)
player.destination = location
makeGraph()
player.moving = true
player.UpdateMoveMode()
continue touchloop
}
// .... more if statements ....
}
}
and graph function (called every touchesmoved cycle):
func makeGraph(){
let startNode = GKGraphNode2D(point: float2(Float(player.position.x), Float(player.position.y)))
let endNode = GKGraphNode2D(point: float2(Float(player.destination.x), Float(player.destination.y)))
let graphcopy = navgraph
graphcopy.connectUsingObstacles(node: startNode)
graphcopy.connectUsingObstacles(node: endNode)
let path = graphcopy.findPath(from: startNode, to: endNode)
player.goto = []
for node:GKGraphNode in path {
if let point2d = node as? GKGraphNode2D {
let point = CGPoint(x: CGFloat(point2d.position.x), y: CGFloat(point2d.position.y))
player.goto.append(point)
}
}
if player.goto.count == 0 {
//if path is empty, go straight to destination
player.goto = [player.destination]
} else {
//if path is not empty, remove first point (start point)
player.goto.remove(at: 0)
}
}
any ideas?

Agent following path moves forward indefinitely

I am trying to make a sprite move towards a point while avoiding some obstacles. The graph that I am using is a GKObstacleGraph, obtained from this scene:
For simplicity I decided not to use a circular physics body for the obstacles, but it's enough to use the sprites' bounds for now. So this is how I created the graph:
lazy var graph:GKObstacleGraph? =
{
guard let spaceship = self.spaceship else { return nil }
let obstacles = SKNode.obstacles(fromNodeBounds: self.rocks)
let graph = GKObstacleGraph(obstacles: obstacles, bufferRadius: Float(spaceship.size.width))
return graph
}()
When the user taps on any location of the scene, the spaceship should start moving toward that position by avoiding the obstacles:
func tap(locationInScene location:CGPoint)
{
guard let graph = self.graph else { return }
guard let spaceship = self.spaceship else { return }
let startNode = GKGraphNode2D(point: vector_float2(withPoint: spaceship.position))
let endNode = GKGraphNode2D(point: vector_float2(withPoint: location))
graph.connectUsingObstacles(node: startNode)
graph.connectUsingObstacles(node: endNode)
let path = graph.findPath(from: startNode, to: endNode)
print(path)
let goal = GKGoal(toFollow: GKPath(graphNodes: path, radius: Float(spaceship.size.width)) , maxPredictionTime: 1.0, forward: true)
let behavior = GKBehavior(goal: goal, weight: 1.0)
let agent = GKAgent2D()
agent.behavior = behavior
graph.remove([startNode, endNode])
spaceship.entity?.addComponent(agent)
// This is necessary. I don't know why, but if I don't do that, I
// end up having a duplicate entity in my scene's entities array.
self.entities = [spaceship.entity!]
}
But when I tap on a point on the scene, the spaceship starts moving indefinitely upwards. I tried to print the position of the GKAgent at every frame, and this is what I got:
With very low values, the spaceship still keeps moving upwards without stopping, ever.
This is my project on GitHub.