Gravity Cocos2D? - ios

In my game, I am using Cocos2D for the Game part. I am now trying to implement gravity but I am hearing that I have to use Box2D or Chipmunk. I could use those but is there any way to do this in Cocos2D, can anyone share any ideas/code just so I can add some simple gravity using Cocos2D if possible?
Thanks!

Its very easy using Box 2d and Chipmunk. Its inbuilt in cocos2d framework. Just when you start with the cocos2d application template(for iOS) select the Box2D/Chipmunk template. Its very easy.
Inorder to start with some gravity you have to create a world and add gravity vectors to it. You have a very simple and detailed tutorial in
http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls
Its a tutorial that teaches you to create a bouncing ball app in Cocos2d Box2d Framework.

First create a CGPoint variable called gravity and set it's x value to 0 and it's y value to some negative number.
CGPoint *grav = ccp(0.0f,-9.8f);
Then, in your game loop, just use ccSub on each of your sprites positions.
sprite.position = ccSub(sprite.position,grav);

Related

Swift & Sprite-Kit: How to rotate sprites to the direction set by collision?

I'm new to game physics. I would like to create a sprite in Sprite-kit where the sprite would rotate as it collides with screen boundaries or other objects in the game. Basically like so.
I just have no idea how this is done in Sprite-Kit or with any other framework for that matter... Do I have to calculate rotations in code or does the framework offer assistance? If this is going to be done in my code, could someone help me get started? Thanks a bunch already!
Use the following SKAction:
let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:5)
sprite.runAction(action)
This will rotate the sprite 180ยบ

Swift unity raycast equivalent

I want to check if there is an object below my player in my endless 3D runner, something like ray casting in unity. Is there a equivalent or does someone know how to do it differently maybe?
Checking for collision I tried but since it's a ball, making it have a dynamic physics body makes it go all over the place.
SCNNode exposes -hitTestWithSegmentFromPoint:toPoint:options:.
In the Fox: Building a SceneKit Game with the Xcode Scene Editor sample obstacles and the ground are static physics bodies and -[SCNPhysicsWorld rayTestWithSegmentFromPoint:toPoint:options:] is used to achieve what you're looking for.

Sprite kit ios game slingshot machanism similar to angry bird game

I am newbie in IOS Gaming, and i need to create a game using Sprite Kit framework for functionality similar to angry bird pulley system, and also wants to find the distance the object is travelled from the pulley to its landing.
Can anyone help me out with this, i would be thankfull for it.
Thanks in advance.
One way you could code a slingshot effect would to use a starting point on the screen at let's say (x=100,y=100). You would display a SpriteNode of a slingshot with the Y centered at the (100,100).
The next step would be to use touchesBegan:withEvent: in the area of the slingshot to let your code know the player is looking to shoot the slingshot.
You would use touchesMoved:withEvent: to track how far back (how much tension) the player is pulling back from the slingshot.
The release would be triggtouchesEnded:withEvent. Based on how far the touch began (x=100) and how far back is was released (for example x=30), you can apply force like this:
float forceUsed = startTouchX - endTouchX;
[_projectile.physicsBody applyForce:CGVectorMake(forceUsed, 0)];
If you are looking to angle the shot you would also have to track Y and use that variable instead of the 0 above.
As for calculating distance between 2 points on the screen, it boils down to x and y coordinates. Subtract objectA.position.x from objectB.position.x
Things can get a lot more complex of course but that all depends on what you want to do in your code. Hope this helps.
P.S. The touches above are all part of the UIResponder Class.

Cocos2D equivalent code for this?

In my app before, I was using box2D methods to move my bodies around but now I changed it so that I am controlling my CCSprites directly. Anyway I have this code:
myBody->ApplyForce(b2Vec2(AccelPoint.x, -40.0f), myBody->GetPosition());
myBody was a b2Body. And AccelPoint.x was the x value so my body could moves around controlled by the UIAccelerometer only on the X axis.
Also, the -40 was a constant for gravity.
So, what would be the equivalent in Cocos2D code?
Thanks!
You will need to store a velocity for the sprite, and every time step add a little bit to that velocity in the direction of the force. Then just move the sprite by the velocity each time step.

Making a Sprite unmovable...[Chipmunk & Cocos2D]

Im using Chipmunk with Cocos2d to make a gravity based puzzle game, however I have reached a part of my project whereby I need a sprite that once drawn does not move and cannot be moved by the other sprites within the environment.
In essence...
Can I create a static (non moving) sprite that is not affected by in game gravity or other objects in the game.
Thanks in advance I've only been doing this project a week....
Yes, this is possible.
1) Create a body with infinite mass and moment.
2) Do not add the body to the space.
3) Create the desired shape.
Use cpSpaceAddStaticShape to add the shape to the space.

Resources