How to move a sprite along a CGPath with variable velocity - ios

I am programming a game and want to move a sprite along a fixed path (straights and curves - think railroad engine) but I want to be able to drive the animation in terms of the sprites velocity - and change that velocity in response to game events.
followPath:duration: in SKAction is close, but there seems no way to adjust the speed "on the fly" - I definitely care about sprite speed - not 'duration to follow path'.
CGPath seems like the right construct for defining the path for the sprite, but there doesn't seem to be enough functionality there to grab points along the path and do my own math.
Can anyone suggest a suitable approach?

You can adjust the execution speed of any SKAction with its speed property. For example, if you set action.speed = 2, the action will run twice as fast.
SKAction *moveAlongPath = [SKAction followPath:path asOffset:NO orientToPath:YES duration:60];
[_character runAction:moveAlongPath withKey:#"moveAlongPath"];
You can then adjust the character's speed by
[self changeActionSpeedTo:2 onNode:_character];
Method to change the speed of an SKAction...
- (void) changeActionSpeedTo:(CGFloat)speed onNode:(SKSpriteNode *)node
{
SKAction *action = [node actionForKey:#"moveAlongPath"];
if (action) {
action.speed = speed;
}
}

Try some think like this :
CGMutablePathRef cgpath = CGPathCreateMutable();
//random values
float xStart = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float xEnd = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
//ControlPoint1
float cp1X = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float cp1Y = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.height ];
//ControlPoint2
float cp2X = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float cp2Y = [self getRandomNumberBetween:0 to:cp1Y];
CGPoint s = CGPointMake(xStart, 1024.0);
CGPoint e = CGPointMake(xEnd, -100.0);
CGPoint cp1 = CGPointMake(cp1X, cp1Y);
CGPoint cp2 = CGPointMake(cp2X, cp2Y);
CGPathMoveToPoint(cgpath,NULL, s.x, s.y);
CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
SKAction *planeDestroy = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];
[self addChild:enemy];
SKAction *remove2 = [SKAction removeFromParent];
[enemy runAction:[SKAction sequence:#[planeDestroy,remove2]]];
CGPathRelease(cgpath);

Related

Sprites spawning in incorrect position

I have a row of 5 sprites and they spawn every 4 seconds and move down along the Y-axis. The sprites are spawning the correct amount each time, I have 5 sprites equal spaced apart across the screen, they're all center perfectly.
However when the performSelector action is called, when it spawns them in they don't go to the right position. The move over to the left so far that I can only see half a sprite on the left side of the screen. After testing it, it seems they are all stacked on top of each other at the wrong position.
Any idea whats going on? I'd appreciate any help. Here's all the code I'm using in the scene.
-(void) addBricks:(CGSize)size {
for (int i = 0; i < 5; i++) {
//create brick sprite from image
SKSpriteNode *brick = [SKSpriteNode spriteNodeWithImageNamed:#"brick"];
//resize bricks
brick.size = CGSizeMake(60, 30);
//psoition bricks
int xPos = size.width/7.5 * (i+.5);
int yPos = 450;
brick.position = CGPointMake(xPos, yPos);
//add move action
SKAction *wait = [SKAction waitForDuration:3];
SKAction *move = [SKAction moveByX:0 y:-36.9 duration:1];
SKAction *sequence = [SKAction sequence:#[wait, move]];
SKAction *repeatMove = [SKAction repeatActionForever:sequence];
[brick runAction:repeatMove];
[self addChild:brick];
}
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor colorWithRed:(243.0f/255) green:(228.0f/255) blue:(165.0f/255) alpha:1.0];
//add action to spawn bricks
SKAction *spawn = [SKAction performSelector:#selector(addBricks:) onTarget:(self)];
SKAction *delay = [SKAction waitForDuration:4];
SKAction *delayThenSpawn = [SKAction sequence:#[delay, spawn]];
[self runAction:[SKAction repeatActionForever:delayThenSpawn]];
[self addBricks:size];
}
return self;
}
as LearnCocos2D mentioned in your last question.. you cant use a selector when youre passing a parameter into a method.
addBricks expects size .. you arent passing size into it.
change your spawn action to
SKAction *spawn = [SKAction runBlock:^{
// make this whatever size you want, i'm just using the scene's size
[self addBricks:self.size];
}];
that should get you started

how to make enemy sprite shoot at player

I'm using a sprite kit to create a game.
I have a player node which I control by touch but then I have an enemy node that moves on its own. I need the enemy to be able to shoot projectiles on its own. I'm guessing I would need a method for this and to call it. I need the direction of the projectiles to shoot wherever my player is. Any suggestions?
Here is the method I have:
-(void)monstershoot
{
SKSpriteNode * projectile = [SKSpriteNode spriteNodeWithImageNamed:#"projectile"];
projectile.position = _enemy.position;
CGPoint offset = rwSub(_player.position, projectile.position);
if (offset.x <= 0) return;
[_background addChild:projectile];
CGPoint direction = rwNormalize(offset);
CGPoint shootAmount = rwMult(direction, 1000);
CGPoint realDest = rwAdd(shootAmount, projectile.position);
// 9 - Create the actions
float velocity = 480.0/1.0;
float realMoveDuration = self.size.width / velocity;
SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[projectile runAction:[SKAction sequence:#[actionMove, actionMoveDone]]];
}
But nothing happens when I call it.
It will do "nothing" in two cases: the image doesn't exist or the enemy is right of the player. Replace this line
if (offset.x <= 0) return;
by a check if the length of the offset vector is 0. You could write a method to calculate the length by using
sqrtf(powf(offset.x,2) + powf(offset.y,2))
Further you should use the length of shootAmount to calculate
float realMoveDuration = 1000 / velocity;
which will give you the same (and correct) speed for every projectile. But otherwise I see nothing wrong with your code.

How can I keep my sprites velocity constant?

I'm making a simple space shooter in Sprite Kit. I have asteroid rock sprites that are randomly added to my scene. I give them a random velocity within a certain range. However, once added to the scene their velocity decreases every second and eventually they stop. as they get closer to the bottom of the screen their velocity decreases at a slower rate. I have gravity of my physics world set to (0,0).
below is the method that adds the rocks to the scene
- (void)addRock {
SPRockNode *rock = [SPRockNode rock];
float dy = [SPUtil randomWithMin:SPRockMinSpeed max:SPRockMaxSpeed];
rock.physicsBody.velocity = CGVectorMake(0, -dy);
float y = self.frame.size.height + rock.size.height;
float x = [SPUtil randomWithMin:10 + rock.size.width max:self.frame.size.width - 10 - rock.size.width];
rock.position = CGPointMake(x, y);
[self addChild:rock];
}
I set up the physics world in my scenes initWIthSize method
self.physicsWorld.contactDelegate = self;
self.physicsWorld.gravity = CGVectorMake(0, 0);
below is the code for my rock node class
+ (instancetype)rock {
SPRockNode *rock = [self spriteNodeWithImageNamed:#"rock"];
rock.name = #"Rock";
float scale = [SPUtil randomWithMin:50 max:100] / 100.0f;
rock.xScale = scale;
rock.yScale = scale;
NSInteger randomInt = [SPUtil randomWithMin:1 max:3];
if (randomInt == 1) {
SKAction *oneRevolution = [SKAction rotateByAngle:-M_PI*2 duration: 1.0];
SKAction *repeat = [SKAction repeatActionForever:oneRevolution];
[rock runAction:repeat];
} else {
SKAction *oneRevolution = [SKAction rotateByAngle:M_PI*2 duration: 1.0];
SKAction *repeat = [SKAction repeatActionForever:oneRevolution];
[rock runAction:repeat];
}
[rock setupPhysicsBody];
return rock;
}
- (void)setupPhysicsBody {
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.categoryBitMask = SPCollisionCategoryRock;
self.physicsBody.collisionBitMask = 0;
self.physicsBody.contactTestBitMask = SPCollisionCategoryProjectile | SPCollisionCategoryShip;
}
any ideas on what causing them to slow down? I dont modify their velocity anywhere else but the addRock method. is there air resistance or something like that on by default? I just dont understand why their velocity is decreasing.
It sounds like you are having an issue with linear damping.
From SKPhysicsBody reference :
This property is used to simulate fluid or air friction forces on the body. The property must be a value between 0.0 and 1.0. The default value is 0.1. If the value is 0.0, no linear damping is applied to the object.
Try adding this :
rock.physicsBody.linearDamping = 0.0f;
It's worthwhile to give the SKPhysicsBody class reference a reading at least once. It only takes a short while, but can save you a ton of time if you have become acquainted with it's properties and methods.

How to do impulse physics

Okay guys, I have two sprites one the enemy who moves to pick up items around the map as the items appear and it does that using SKActions then also I have the wall which he shouldn't go through. anyway. all I am trying to do is to make him bounce out if he hits the wall. How would I go about that?
Here is the code below:
// for the Enemy wall I have this code:
- (void) EnemyBelongings {
EnemyWall = [SKSpriteNode spriteNodeWithImageNamed:#"EnemyWall#2x"];
EnemyWall.name = #"hisWall";
EnemyWall.position = CGPointMake(512, 260);
EnemyWall.xScale = 0.09;
EnemyWall.yScale = 0.09;
EnemyWall.physicsBody.categoryBitMask = PhysicsCategoryEnemyWall;
EnemyWall.physicsBody.contactTestBitMask = PhysicsCategoryEnemy;
EnemyWall.physicsBody.collisionBitMask = 0;
[self addChild:EnemyWall];
}
// For the enemy character I have this code
- (void) Enemy {
_Enemy = [SKSpriteNode spriteNodeWithImageNamed:#"enemy"];
_Enemy.position = CGPointMake(520, _Enemy.size.height/1.50);
_Enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width];
_Enemy.physicsBody.usesPreciseCollisionDetection = YES;
_Enemy.physicsBody.categoryBitMask = PhysicsCategoryEnemy;
_Enemy.physicsBody.contactTestBitMask = PhysicsCategoryEnemyWall;
_Enemy.physicsBody.collisionBitMask = 0;
[self addChild:_Enemy];
}
// For the enemy movement I have this code
- (void) EnemySpawner {
[ForEnemy runAction:appear2 completion:^{
SKAction *wait = [SKAction waitForDuration:1.0];
[_Enemy runAction:wait];
SKAction *actionXMove2 = [SKAction moveToX:ForEnemy.position.x duration:0.14];
SKAction *actionYMove2 = [SKAction moveToY:ForEnemy.position.y duration:0.14];
[_Enemy runAction:actionYMove2];
[_Enemy runAction:actionXMove2];
}];
}
-(void)didBeginContact:(SKPhysicsContact *)contact
{
if (contact.bodyA.categoryBitMask == PhysicsCategoryEnemy && contact.bodyB.categoryBitMask
== PhysicsCategoryEnemyWall)
{
SKNode *enemy = contact.bodyA.node; // a is the enemy here
// Code here
SKNode *wall = contact.bodyB.node; // b is the enemy's wall here
// Code here
}
else if (contact.bodyB.categoryBitMask == PhysicsCategoryEnemy &&
contact.bodyA.categoryBitMask == PhysicsCategoryEnemyWall)
{
SKNode *enemy = contact.bodyB.node;
// Code here
SKNode *wall = contact.bodyA.node;
// Code here
}
}
You need to applyImpulse on the node's physicsBody to make it simulate naturally and interact with other physicsBodies.
Please look at my answer here on how to do the same.
Copy the rwAdd, rwSub, rwMult, rwLength and the rwNormalize methods from this tutorial by Ray Wenderlich.
Then, try using this code:
[ForEnemy runAction:appear2 completion:^{
SKAction *wait = [SKAction waitForDuration:1.0];
CGPoint offset = rwSub(location, ForEnemy.position);
CGPoint direction = rwNormalize(offset);
float forceValue = 200; //Edit this value to get the desired force.
CGPoint shootAmount = rwMult(direction, forceValue);
CGVector impulseVector = CGVectorMake(shootAmount.x, shootAmount.y);
[_Enemy.physicsBody applyImpulse:impulseVector];
}];
PhysicsWorld should simulate bounces automatically. Just set your nodes' physics properties accordingly. Look for the 'restitution' property. It will determine the bounciness of your objects.
And don't use SKAction to move your enemies. This will just "teleport" your nodes around. They don't get to have any real velocity in the physicsWorld (and hence don't bounce). Instead, apply forces to your bodies (or set velocity vector manually).

SpriteKit - How to add objects with specific distance

in my game I am adding enemies to the scene every second , I need to describe an specific distance between pervious object that my main character move through these enemy , here is my codes that add enemy to the scene :
- (void)createEnemy {
int GoOrNot = [self getRandomNumberBetween:0 to:1];
if(GoOrNot == 1){
int randomEnemy = [self getRandomNumberBetween:0 to:1];
if(randomEnemy == 0)
enemy = [[SKSpriteNode alloc]initWithImageNamed:#"car.png"];
else
enemy = [[SKSpriteNode alloc]initWithImageNamed:#"block.png"];
int xPostion = [self placeRandomObject] ;
int yPostion = 1150;
enemy.position = CGPointMake(xPostion, yPostion);
enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemy.size];
enemy.name = #"enemy";
enemy.physicsBody.categoryBitMask = enemyCategory;
enemy.physicsBody.contactTestBitMask = carCategory;
enemy.physicsBody.collisionBitMask = 0;
enemy.physicsBody.dynamic = YES;
enemy.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:enemy];
SKAction *wait = [SKAction waitForDuration:.20];
SKAction *move = [SKAction moveToY:self.scene.frame.origin.y-10 duration:enemySpeed];
SKAction *remove = [SKAction removeFromParent];
SKAction *runAction = [SKAction sequence:#[wait,move , remove]];
[enemy runAction:[SKAction repeatActionForever:runAction]];
}
}
adding enemy :
- (void)addEnemies {
SKAction *wait = [SKAction waitForDuration:.55];
SKAction *callEnemies = [SKAction runBlock:^{ [self createEnemy];}];
updateEnimies = [SKAction sequence:#[wait,callEnemies]];
[self runAction:[SKAction repeatActionForever:updateEnimies] withKey:#"addEnemy"];
}
You can create new variable to hold the last position of the previous enemy.
CGPoint positionOfLastEnemy;
when you create new enemy assign the position of created enemy to this variable:
positionOfLastEnemy = CGPointMake(xPostion, yPostion);
if you need to update the enemy position you can do it in your collision detection method.
After that just use the positionOfLastEnemy variable to calculate the distance you need.
The other way is to add variable to hold the last enemy
SKSpriteNode *lastEnemy;
in your createEnemy method save reference to your created enemy.
lastEnemy = enemy;
And if you need the distance of the last enemy just use
lastEnemy.position

Resources