# Sprite Kit - keeping character level with the terrain 2d endless runner

I've been trying to figure this out exhaustively for the past few days and still haven't found an answer. I have an endless runner built using swift where the character is fixed and the terrain moves to the left. I have edge physics bodies on the terrain, and edge physics bodies that go up and down slopes.

The problem is, if I want my character to collide with the ground without being pushed back by it, since the only thing that can make contact with an edge is a dynamic volume.

My goal is to keep the character level with the terrain, (assuming they're not jumping). Even on slopes.

I've tried ray casting using enumeratebodiesalongraystart, but can't seem to figure out how to get coordinates of where it intersects with the ground. I've tried having an edge body that is vertical, that intersects with the ground(two edge bodies cannot make contact in sprite kit). I've tried dynamic volumes. I've tried Static volumes. Nothing works. I'm not posting code here since I'm really just looking for a general answer to sort of point me in the right direction, but I'm coding in swift. Thanks in advance. Also I have the character in a different bitmaskcatagory than the terrain catagory and they are set up to recognize each other, and do if one of them is a dynamic volume physics body.

• I'm still a bit unclear on what the problem is. What does "collide with the ground without being pushed back" mean? Could you explain the particular situation where it's not working how you expect? – Anko Apr 28 '15 at 12:46
• Fair enough. Basically, I want there to be a contact without a collision. So if two edge physics bodies cant make a contact then it would have to be a static volume body, though, that doesn't seem to register a contact either. If I add a dynamic volume to the sprite, it is pushed to the left by the terrain that is moving left due to the built in physic simulation. I'm wanting the player sprite to be fixed in one x position while moving up and down as the terrain height changes. – tedesignz Apr 28 '15 at 14:13

To keep a character level with a moving "ground" without its x position being affected by the physics engine, it would seem the best solution might be to place an SKConstraint on the character. Specifically:

character.constraints = [SKConstraint.positionX(SKRange(constantValue: 0.0))]


If you have any weird bugs from SpriteKit's physics engine, you might try placing a node with a physicsBody that's affect by gravity with that same constraint, and constraining the character to that node. You could place an additional rotational constraint on the character if you want the character to only rotate a certain angle while "climbing hills" or if you want your character to be "upright" constantly while not colliding with the ground.

• Thanks for the info on constraints. I did not know about constraints. this will prove very useful. I actually did this another way without using collisions. I just have a simple method that detects what tile piece the character is above, and sets the ground level based on that. I'm not sure if it's the most ideal way of doing it, but it seems to do the trick. Very useful info anyway. I'm sure it would've solved my problem if I decided to go with collisions. You get the upvote! – tedesignz May 6 '15 at 2:35