Move SKSpriteNode with Physics - sprite-kit

I am trying to move an SKSpriteNode with physics by setting it's velocity. I am doing this on top of another SKNode (the map) that is part of the SKScene. Positions of "cities" are stored in custom Location objects.
I know the location of the initial SKSpriteNode (the ship) and I know the desired location. From what I've read, I can move the sprite by setting it's velocity. I do so like this:
float dy = _player.desiredLocation.yCoordinate - _mapNode.pin.position.y;
float dx = _player.desiredLocation.xCoordinate - _mapNode.pin.position.x;
_mapNode.pin.physicsBody.velocity = CGVectorMake(dx, dy);
This is all inside the didSimulatePhysics function inside the SKScene. Once the user taps a Location, I find the position and set the velocity. This seems to work well the FIRST time, but the sprite moves all over the place in subsequent times. Any idea what could be going wrong here?
PS: Setting skView.showsPhysics = YES; puts the circle of the physics body way off the position of the sprite.
In the map node:
self.pin = [SKSpriteNode spriteNodeWithImageNamed:[IPGameManager sharedGameData].world.player.ship.type];
self.pin.userInteractionEnabled = NO;
self.pin.position = CGPointMake([IPGameManager sharedGameData].world.player.location.xCoordinate, [IPGameManager sharedGameData].world.player.location.yCoordinate);
self.pin.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10.0];
self.pin.physicsBody.dynamic = YES;
self.pin.physicsBody.allowsRotation = NO;
self.pin.physicsBody.friction = 0.0;
self.pin.physicsBody.linearDamping = 0.0;
[self addChild:self.pin];

Related

Moving sprite "smoothly"

I'm trying to move a sprite in a set direction on-screen, using the physicsBody.velocity property. However, the motion is quite "jumpy" (makes the game look like it's lagging). I assume this has something to do with the different time intervals between each call to the update-method. How could I achieve a more "smooth" motion?
Right now this is the code I have in my update-loop to make the sprite move forward:
if (_movingForward) {
CGFloat forwardVel = _ball.physicsBody.velocity.dx;
forwardVel += 20.0;
if (forwardVel > bMaxForwardVelocity) {
forwardVel = bMaxForwardVelocity;
}
_ball.physicsBody.velocity = CGVectorMake(forwardVel, _ball.physicsBody.velocity.dy);
}
As you can see, I'm trying to make the speed pick up, so it doesn't immediately reach it's terminal velocity. This is to make the motion look more natural. However, the lagging is ruining this effect greatly, even when the sprite supposedly is traveling at a set speed.
EDIT 1:
I'm trying to make a platform-style game (like Super Mario etc) where the _ball moves horizontally along the ground when the user taps the screen. The code for moving the ball to the right is included above the edit. The ground is made up of multiple tiles, each 32x32 and positioned right next to each other with no gap in-between.
As per request, here's the code for setting up the _balls physicsBody:
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width/2.0];
self.physicsBody.dynamic = YES;
self.physicsBody.restitution = 0.5f;
self.physicsBody.usesPreciseCollisionDetection = YES;
self.physicsBody.allowsRotation = NO;
self.physicsBody.mass = 0.5f;
self.physicsBody.friction = 0.0f;
self.physicsBody.linearDamping = 0.0f;
self.physicsBody.categoryBitMask = CollisionCategoryBall;
And here's the code for the physicsBody of each individual tile:
tile.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:tile.size];
tile.physicsBody.dynamic = NO;
tile.physicsBody.categoryBitMask = CollisionCategoryGround;
The physicsWorld is set up like this:
self.physicsWorld.gravity = CGVectorMake(0, -4);
self.physicsWorld.contactDelegate = self;
Thanks!
EDIT 2:
This is a side-scrolling game, so the "camera" follows the ball. Here's the code for moving the background layer:
- (void)centerViewOn:(CGPoint)centerOn {
CGFloat x = Clamp(centerOn.x, (self.size.width / 2.0), (_bgLayer.layerSize.width) - (self.size.width / 2.0));
_worldNode.position = CGPointMake((int)-x, _worldNode.position.y);
}
The reason for the Clamp is so the "camera" doesn't scroll beyond the bounds of the background. This is when the ball is close to either the start or end of the level.
I'm casting the position to an int because without it, small gaps sometimes occur between the tiles. I was told this was because of some inaccuracy or something. However, even without casting the position to int, it's still a bitt "laggy". But because of the gaps between the tiles, it looks even worse.
The issue was with your scrolling code.
change this line :
_worldNode.position = CGPointMake((int)-x, _worldNode.position.y);
to
_worldNode.position = CGPointMake(-x, _worldNode.position.y);
The movement is then silky smooth.
But then you do get those seams. You should solve that in the placement of those tiles. You are likely positioning your _worldNode tiles with float values.

Using SpriteKit how can you move sprite nodes to a point with a random but consistent speed

I'm currently making my first sprite game and one of the things i have implemented into my game is objects coming in from the side, move all the way across the screen, and then be removed from the parent. Here's what that looks like.
MyScene.m
-(void)createObstacle0 {
CGPoint startPoint = CGPointMake(-20, 100);
SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithImageNamed:#"obstacle"];
obstacle.position = CGPointMake(startPoint.x, startPoint.y);
obstacle.name = #"obstacle0";
[self addChild:obstacle];
float randomNum = arc4random_uniform(2) + 1.92;
[self performSelector:#selector(createObstacle0) withObject:nil afterDelay:randomNum];
}
excerpt from update method
[self enumerateChildNodesWithName:#"obstacle0" usingBlock:^(SKNode *node, BOOL *stop) {
if (node.position.x > 360) {
[node removeFromParent];
} else {
node.position = CGPointMake(node.position.x + 5.2, node.position.y);
}
}];
At the moment, my objects only move across the screen at one speed. I tried using arc4random inside the currentTime method code, but because thats evaluated every frame, my object would not stay at one speed. It would speed up, slow down, and look very erratic. How can i change my code in CurrentTime to allow my objects crossing the screen to be set a random speed as soon as its made and to keep it until it is destroyed?
Second question:
int yMin = 90;
int yMax = 110;
CGPoint startPoint = CGPointMake(-20, yMin + arc4random_uniform(yMax - yMin));
I would suggest that instead of manually updating the child nodes position to move it from one side to another you create your obstacle and then call runAction: on it using an SKAction created with the moveTo:duration: method. You specify the final point you want your obstacle to end up at as well as how long you want this move to take. Pass in a random number with in the minimum and maximum time you want it to take to duration.
This should allow you to create consistent but randomised object movement.
int MAX_TIME = 20;
int MIN_TIME = 10;
[obstacle runAction:[SKAction moveTo:CGPointMake( 10, 100 ) duration:MIN_TIME + arc4random_uniform( MAX_TIME - MIN_TIME )]];

SpriteKit 1 Dimensional Movement

I'm using apple's Sprite Kit and I need to move a SKSprite Node in horizontal movement only. I want the rest of the physics to apply but only in the horizontal component.
Context: This is for an object supposedly on a slider that can bounce back and forth. I have everything done but if it is hit the end the wrong way it simply floats off vertically, how can I simply make it ignore all forces in the vertical direction.
By putting the node's position back at the desired Y coordinate every frame after physics has been simulated:
-(void) didSimulatePhysics
{
CGPoint pos = horizontalMoveNode.position;
pos.y = fixedVerticalPosY;
horizontalMoveNode.position = pos;
}
Add this method to your scene class and apply it to whichever node(s) you want to lock in at a given Y coordinate.
You could use constraints for this purpose. I made a short sample with a node that only moves in a fixed X range and never leaves a specified Y position:
SKSpriteNode* node = [SKSpriteNode node];
node.color = [SKColor greenColor];
node.size = CGSizeMake(20, 20);
SKRange* rangeX = [[SKRange alloc] initWithLowerLimit: 100 upperLimit: 400];
SKRange* rangeY = [SKRange rangeWithConstantValue: 100];
SKConstraint* positionConstraint = [SKConstraint positionX: rangeX Y: rangeY];
NSArray* constraintArray = [NSArray arrayWithObject: positionConstraint];
node.constraints = constraintArray;
[self addChild: node];
This method is from SKAction class to move objects only on Horizontal or X-axis:-
[mySpriteNode runAction:[SKAction moveToX:260 duration:0.5]];
I hope this work's for you.

can't offset ccnode to let me center my main character

I have a standard cocos2d startup layer( HelloWorldLayer). I created another class of type CCNode named "Terrain" for my terrain. Then i add it to my layer in the layer's init:
terrain = [[Terrain alloc] initWithWorld:world AndLevel:0];
[self addChild:terrain z:1];
i add a 'CarObject' class (a CCSprite class), and add a car object to my terrain
car = [[CarObject alloc] initWithWorld:world];
[terrain addChild:car];
-i.e. in both the initWithWorld for terrain and car, i initialize some Box2d code
I then try to center my car object to my screen when i move it, i do this in my update method:
float offsetX = car.position.x;
float offsetY = car.position.y;
[terrain setOffsetX:(int)offsetX andOffsetY:(int)offsetY];
where the setOffsetX.. method is:
- (void) setOffsetX:(int)newOffsetX andOffsetY:(int)newOffsetY {
_offsetX = newOffsetX;
_offsetY = newOffsetY;
CGSize winSize = [CCDirector sharedDirector].winSize;
self.position = CGPointMake(-(_offsetX - winSize.width/2), -(_offsetY - winSize.height/2));
}
When i use a NSLog to see if the terrain position changes, i can see that the position actually chages, but the view does not. What am i doing wrong? am sure it's a dumb mistake!
btw, if i try this in my HelloWorldLayer's update method (instead of [terrain setOffsetX..])
self.position = CGPointMake(self.position.x-1, self.position.y);
the terrain is moving.
Car is a child of Terrain. Car's position is therefore relative to Terrain's position. Since you base Terrain's position on Car's position, which is actually relative to Terrain's position, you may be simply running into the effect that your position updates simply cancel each other out.
If you want to move the Terrain while keeping the Car centered, you shouldn't add the Car as a child of Terrain. Instead add it to the same node as the Terrain (HelloWorldLayer). Then you can move the Car and Terrain independently of each other.

Correctly removing Box2D fixtures and updating b2Body

I have a Box2d body which I'm attempting to break into multiple pieces. To do this, I iterate over its fixtures and generate new bodies for each. Using debug draw, I can see that this seems to be working.
As you can see in the above image, the primary body is being broken and a secondary body (labelled: 2) is being generated. Based on the shape rendering from the debug layer, they're being represented correctly. The issue I'm having is that the CCSprite I'm associating with my primary b2body isn't being correctly positioned in reference to the new body. It seems as though the associated CCSprite is being positioned (given an anchor point of 0, 0) as if it were still part of a larger shape.
For reference, here's the code I'm using:
for (b2Fixture *f = body->GetFixtureList(); f; f = f->GetNext())
{
NSString *newSpriteFrameName = (NSString *)f->GetUserData();
// Steal some of our parent bodies properties
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position = [self physicsPosition];
bd.angle = [self angle];
b2Body *newBody = _world->CreateBody(&bd);
b2FixtureDef fixtureDef;
fixtureDef.shape = f->GetShape();
fixtureDef.density = f->GetDensity();
fixtureDef.restitution = f->GetRestitution();
fixtureDef.friction = f->GetFriction();
fixtureDef.userData = f->GetUserData();
newBody->CreateFixture(&fixtureDef);
// Try to transfer any angular and linear velocity
b2Vec2 center1 = [self worldCenter];
b2Vec2 center2 = newBody->GetWorldCenter();
CGFloat angularVelocity = parentBody->GetAngularVelocity();
b2Vec2 velocity1 = [self linearVelocity] + b2Cross(angularVelocity, center1 - center1);
b2Vec2 velocity2 = [self linearVelocity] + b2Cross(angularVelocity, center2 - center1);
newBody->SetAngularVelocity(angularVelocity);
newBody->SetLinearVelocity(velocity2);
// Create a new destructable entity
CCSprite *newSprite = [CCSprite spriteWithSpriteFrameName:newSpriteFrameName];
SIDestructableEntity *newEntity = [[SIDestructableEntity alloc] initWithBody:newBody node:newSprite];
[[newEntity ccNode] setAnchorPoint:CGPointMake(0, 0)];
[game.entities addObject:newEntity];
[game.entityLayer addChild:[newEntity ccNode]];
}
Here's how I'm setting my CCSprites location each logic tick:
b2Vec2 position = body->GetPosition();
ccNode.position = CGPointMake(PTM_RATIO*position.x, PTM_RATIO*position.y);
ccNode.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle());
This line looks suspicious.
[[newEntity ccNode] setAnchorPoint:CGPointMake(0, 0)];
The sprite usually has an anchor point of (0.5,0.5). If your body's anchor point is in the middle (can't tell from code above), then an anchor of (0.5,0.5) for the sprite would put it in the middle as well. Putting it at (0,0) puts the sprite's top left corner at the position of the sprite.
My guess is that your body anchor is at the bottom left of body and the sprite anchor is at the top right, giving the effect you are seeing.