2
votes

How to make a free falling sprite node sit on another horizontally moving sprite body?

Now the both both bodies collide. But the horizontal moving body is deviated from its path and free falling body goes down after hitting horizontal body. How can fix this changing settings of the sprite nodes.

I have made given collision and category bit mask for collision to happen correctly.


freefalling body properties:

  • affectedbygravity = YES dynamic = YES allowsrotation = NO usesprecisecollisiondetection = YES restitution = 1 friction = 1.0 angulardamping = 0.0 lineardamping = 0.0

horizontal moving body properties

dynamic = YES affectedbyGravity = NO friction = 1.0

an oscillation action is performed on this body.

1
did you try adjusting the mass? make the falling body lighter and the moving body heavier. fallingNode.physicsBody?.massrakeshbs
I did try.. but result was not good.. i am trying different combinations of mass to check thatNandakumar R
Even with very high difference in mass, like 0.7 and 500, horizontal body is getting deflected.Nandakumar R
check my answer. its just an experimental coderakeshbs

1 Answers

1
votes

Adjusting the mass can help in not deflecting the moving object. For example try this code,

SKSpriteNode *fallNode = [[SKSpriteNode alloc] initWithColor:[UIColor redColor] size:CGSizeMake(25, 25)];
fallNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:fallNode.size];
fallNode.position = CGPointMake(100, 400);
fallNode.physicsBody.mass = 1;
fallNode.physicsBody.allowsRotation = NO;
fallNode.physicsBody.restitution = 0.0;
fallNode.physicsBody.friction = 1.0;
[self addChild : fallNode];

SKSpriteNode *moveNode = [[SKSpriteNode alloc] initWithColor:[UIColor greenColor] size:CGSizeMake(25, 25)];

moveNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:moveNode.size];
moveNode.position = CGPointMake(50, 100);
moveNode.physicsBody.mass = 100000;
moveNode.physicsBody.affectedByGravity = false;
moveNode.physicsBody.friction = 1.0;
moveNode.physicsBody.velocity = CGVectorMake(90, 0);

[self addChild : moveNode];