-1

I have a sprite that I want to apply a constant force to, as opposed to reapplying that force every update, which is giving me strange results.

I have a sprite that has a mass of 60 kg and is affected by gravity. I want to apply a force that is equal to the force of gravity constantly. The way I'm doing it still causes the sprite to fall of the screen really fast because the force of gravity is somehow greater. I'm wondering how and when the force of gravity is applied, and if it is possible fore me to set a constant force on the object, similar to the force of gravity.

Currently, my scene looks like this:

#import "MyScene.h"

@interface MyScene ()

@property (nonatomic, strong)SKSpriteNode *parachutist;

@end

@implementation MyScene

- (id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        CGSize spriteSize = CGSizeMake(80.0f, 80.0f);
        self.parachutist = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"parachutist.png"] size:spriteSize];
        [self addChild:self.parachutist];
        self.parachutist.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        self.parachutist.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteSize];
        self.parachutist.physicsBody.dynamic = YES;
        self.parachutist.physicsBody.mass = 60.0;    // kg
        self.parachutist.physicsBody.affectedByGravity = YES;
    }

    return self;
}

- (void)update:(NSTimeInterval)currentTime
{
    // this ought to match the force of gravity
    [self.parachutist.physicsBody applyForce:CGVectorMake(0.0f, 60.0f * 9.8f)];
}

@end
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
  • If you are looking to apply force which cancels out the gravitational constant, why not simply set your object's physics property self.physicsBody.affectedByGravity = NO; ? – sangony Apr 29 '14 at 15:22
  • I don't want to get rid of gravity. I just want to have a force that I control that has realistic numbers and is equal to gravity when it equals the mass times g. – michaelsnowden Apr 29 '14 at 15:35
  • In your question you asked what you can do to NOT have to reapply force repeatedly in the update: method AND "want to apply a force that is equal to the force of gravity constantly". That translates to the object being stationary which means disabling the object's gravity property. If you want the object the fall slower then it currently is, then play around with the force applied numbers until you reach the desired downward velocity. – sangony Apr 29 '14 at 15:40
  • Dude, all I want to do is apply a constant force. If you can't help me with that, then stop commenting here please. – michaelsnowden Apr 29 '14 at 17:00

2 Answers2

0

Fixed:

#import "MyScene.h"
#define WEIRD_FACTOR 150.0f
#define GRAVITY -9.8f

@interface MyScene ()

@property (nonatomic, strong)SKSpriteNode *parachutist;

@end

@implementation MyScene

- (id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        CGSize spriteSize = CGSizeMake(80.0f, 80.0f);
        self.physicsBody.affectedByGravity = YES;
        self.physicsWorld.gravity = CGVectorMake(0.0f, GRAVITY / WEIRD_FACTOR);
        self.parachutist = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"Parachutist.png"] size:spriteSize];
        [self addChild:self.parachutist];
        self.parachutist.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        self.parachutist.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteSize];
        self.parachutist.physicsBody.dynamic = YES;
        self.parachutist.physicsBody.mass = 60.0;    // kg
        self.parachutist.physicsBody.affectedByGravity = YES;
    }

    return self;
}

- (void)update:(NSTimeInterval)currentTime
{
    [self.parachutist.physicsBody applyForce:CGVectorMake(0.0f,
                                                          self.parachutist.physicsBody.mass * -GRAVITY)];
}

@end

See this question for an explanation: applyForce(0, 400) - SpriteKit inconsistency

Community
  • 1
  • 1
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
-1

Doctordoder:

I am new to sprite kit myself but am currently working on a game as well. I believe that you need to make the vector's Y component be negative.

[self.parachutist.physicsBody applyForce:CGVectorMake(0.0f, -9.8)];

that should apply force straight down at -9.8 units. However unlike gravity I do not believe this to be cumulative. gravity is -9,8 meters per sec squared.

By multiplying the gravity by 60 your essentially making gravity 60 times what it is normally.

Alternatively you could adjust the property for physicsWorld Gravity and that would apply the gravity to any object with a physics body not set to physicsBody.dynamic = YES

self.physicsWorld.gravity = CGVectorMake(0, -9.8 );

edit: grammer and punctuation.

mduttondev
  • 105
  • 2
  • 10
  • The gravity vector by default is -9.8 in the y, so me adding a force like that would just compound it. Test it yourself. Anyway, multiplying gravity by 60 is to get the actual force. Gravity, g is a constant that needs to be multiplied by the mass, 60.0 kg, to produce a force. – michaelsnowden Apr 29 '14 at 20:06