Swift Spritekit apply rotational impulse - swift

I am making a space shooter game with Swift and SpriteKit I would like that when the user presses a key, the ship will rotate. Currently I rotate the node, However, I would like to apply a physics impulse to rotate the nodes physics body, So it will continue rotating when I release the key.

Try:
node.physicsBody?.applyAngularImpulse
or:
node.physicsBody?.angularVelocity

Related

Ball moving in tube with rigidbody

Lets say you have a sphere(rigidbody, sphere collider) inside a tube(mesh collider). I want a ball to move in the direction of tube always by applying force on rigidbody.
My purpose is to simulate the circular gravity, so that ball can fall down back obviously in circular motion. This was the approch i was using to simulate fake circular gravity.
How can I check which direction of force has to be applied to continue motion following the path of the tube with addforce?
try applying physics material to your tube to control the ball reaction to the surface of the tube.

Unity Camera Bounces with Sphere Animation when trying to apply gravity with Rigidbody

I have an empty Object that has, among other things, a third and first person camera, and a sphere with a bouncing animation attached to it. I'm try to get it so that the sphere experiences gravity, but I'm having a lot of problems.
What I want is gravity on my sphere and the camera to not bounce with the ball.
Here a couple scenarios I've tried, and their outcomes:
1) When I attach a rigid body to the the Sphere (child of the empty Object) with apply root motion true or false, my character experiences no gravity. The ball bounces, but the camera follows the ball without bouncing with the ball. It also follows if i press the space button (to jump) The whole problem is i need gravity to fall back down when i jump. Right now i just go up.
2) When I attach a rigid body to the empty object (the parent), I gain gravity, but now my camera bounces with the ball, and if the ball tumbles and rotates, the camera rotates with it.
Is there a way to get the best of both worlds?
i.e: Ball experiences gravity, but the camera does not. The camera simply follows around the ball from first or third person view without bouncing.
Thanks so much
For me, I would like to create a Vector3D variable to record the offset between the ball and the camera. And you simply add the offset to your camera each time so your camera can chase the ball without bouncing with it.
For example:
Vector3D offset = cameraPos - ballPos;
//...
CameraPos = ballPos + offset;
If you don't want the camera moving with ball's gravity, you can block the change of Y axis.

How to rotate a SKPhysicsBody in SpriteKit with Swift

I am trying to make a brick breaking game with SpriteKit in Swift. To make the paddle I am trying to make it so that there are two SKPhysicBody, one for each half. To control the angle of which the ball bounces off the paddle, I want to rotate the physics body. I tried to use applyAngularImpulse or just applyForce, but they did not work. Is there a way to rotate the physics body??
You can't set the rotation of a physics body. You can however set the rotation of the node that the physics body is attached to. So what you can do is make a dedicated node for your physics body. This node will be invisible, it will only exist for you to rotate.

Sprite Kit physicsBody loses consistency during rotation - zrotation

I have this classic situation of a pinball game
All objects are physicsBody with rights collision masks and so on.. they collide perfectly.
The problem is that they only work when they are static...If I try to rotate a paddle on its anchorpoint, there is no collision with the ball and the ball falls down, passing through the paddle.
I thought it was a problem of speed but I think that during the rotation the physicsBody simply doesn't work.
Do you have suggestions?
Thank you so much.
That's exactly the problem you get when rotating a static body, it's not going to act physically correct.
For example static bodies have no force, no velocity - if you move or rotate it, it will just be there at the new position and new rotation without pushing any dynamic bodies around. Only if a dynamic body now happens to be intersecting with the static body will the physics engine try to resolve the collision, usually in a brute-force manner (aka "get me outta here").
Therefore the paddle has to be dynamic if you want it to move, accelerate and spin the ball. Both paddle and ball may need to have continuous collision detection enabled as to not lose any precision in the paddle motion applied to the ball, and vice versa.

Cocos2d Chipmunk gravity and collision detection

I have a simple animation in Cocos2d Chipmunk with the tasks:
One round shaped sprite situated in the center of the screen, rigid body type. Center of gravity needs to be located in center of this sprite.
From different sides of screen (spontaneously and beyond screen size) other rigid round sprites must fall into central sprite to fill visible screen space.
Sprites should not overlap one another.
So the questions are:
How to reassign the vector of gravity to the center of the screen?
How to implement collision detection between rigid body types in Cocos2d Chipmunk?
Thanks, colleagues!
You can't set a center for the gravity. You can only set a direction (the same for all objects). It's possible to create an effect you describe, but you'll have to do the work yourself. In every frame you have to set a force or apply an impulse on every body in the direction of your gravity. And the "regular" chipmunk gravity should be (0, 0).
See this tutorial about collision detection.