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.
Related
I am creating a simple breakout game using SpriteKit and am having an issue where the ball seems to hit off the edge of the bricks even when they are stacked next to each other.
The ball should bounce as though it's bouncing off of a smooth surface, however, it's almost like the bricks' physics bodies aren't fully realized until after the ball has made the first contact.
I have tried several things to solve the problem such as overlapping the bricks by a few pixels, turning on usesPreciseCollisionDetection. However, these don't seem to be having any effect.
This is how I am creating the bricks' physics bodies:
brick.physicsBody = SKPhysicsBody(rectangleOf: brick.frame.size)
Is there something that I am missing, or is this an issue with SpriteKit?
Image of issue
After the fist contact the ball probably get angular velocity, this can change your expected bounce.
If you look this example:
https://github.com/Maetschl/SpriteKitExamples/tree/master/BouncingBalls
The second ball has an angular velocity, this change the direction of the bounce:
Check this property:
ballNode.physicsBody?.angularVelocity and make it 0.
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
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.
Testing out my flash based iOS billiard game:
https://www.dropbox.com/s/hinx2j67xzq3ovs/poolX.jpg
I discovered that the white ball sometimes simply goes through the other balls,
while it collides with the other.
Is there a setting I miss?
I set the white ball body to awake before applying SetLinearVelocity().
Do other bodies need to be set awake or what?
Mirza
Try to use isBullet=true property on your ball body. (not proper collision in box2d)
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.