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.
Related
Im not even sure what is happening here but I'm just trying to make objects fall normally onto the floor - they need gravity/be able to be pushed around, but just fall onto a static floor object.
I have a cube and my floor mesh. Floor mesh has this:
Cube has this:
Initially I'll have my cube just positioned right over the floor. Then I press play and pause, within one second it's 100 m away, spinning/falling. What could be happening here?
You may face problems with small size colliders due to how the physics engine works. Increase size of colliders and see if it helps. If you really need colliders this small, try changing the Default Contact Offset in Physics Manager.
I am trying to create a block breaker game in Unity 3D but I am having problems (this is my first time creating a game). When I throw the ball it ends like this https://gfycat.com/LimitedVagueAnkole. The physics between the ball and the bricks should be problematic i think but I don't know why. My ball's material is frictionless and its bounciness is 1. My bricks materials are None (physics material).
I can send more info if it helps but I don't really know what else to send.
PS: I didn't code the breaking of the bricks yet. I just don't understand why the ball isn't acting the way it should.
The same thing is happening to me, I think I have a solution for this problem. I think the reason why the ball keeps bouncing at one place is because the ball didn't have much speed to escape from the loop. Try increasing the speed of the ball a bit and you could see the ball indeed escape bouncing at one particular angle.
I am making a 2D Brick Breaker game in Unity.
I have an issue with the scenario when ball hits in between two bricks. I have BoxCollider2D attached to all bricks and a CircleCollider2D attached to the ball. When the ball hits between 2 adjacent bricks, it bounces back in the same direction as if it hit the edge of the brick. There is no edge in between, but two adjacent bricks form a continuous surface. So, the ball should bounce off of the surface (in other direction) instead of bouncing back.
Anyone knows of any solution to tackle this problem? I asked this in the Unity Physics forums but didn't get any answer, so checking if anywhere here might have had this issue.
Thanks,
Mukul
I am guessing this could be the problem:
When your ball is hitting the bricks with a strong force, There is a chance it might move one of the bricks by a very very slight distance, even if the mass of the brick is much heavier.
This movement might cause an uneven surface, which is why the ball bounces back in the same direction.
Try adding a Rigidbody Component on every brick (if you have not done it already), and set its isKinematic property to true.
Let me know if this solves it.
Way 1:
Use one box collider for the wall, but not for every single brick.
This will fix your issue + will optimize your project.
Way 2:
You need to build wall programmaly and colliders in this case must be without spaces between them. This must fix the issue.
Way 3:
Make your own hitting wall logic.
OnColliderEnter you need to get balls velocity.
OnColliderEnd you need to set manually velocity.
I'm building a pacman style maze game.
I can apply impulse and limit it to whatever vertical or horizontal direction as desired on the hero.
However, when I add physics body sprites for walls, it gets weird.
I have set the hero physics body and the walls to have no bounce.
Running head on into a wall it stops dead like expected.
But if I then change direction and move along the wall I veer off at a slight angle from the wall.
Any ideas what causes this and how to avoid it?
I want to only move in vertical and horizontal straight lines.
Ok so the solution here is slightly counter intuitive.
Rather than using applyForce: or applyImpulse: family of methods on the physicsBody, use runAction:forKey: on the sprite itself.
For any change in direction, removeAction:forKey: just before calling runAction:forKey: to keep things orthogonal.
(Otherwise there will be a brief angular movement )
The other trick here is to find an appropriate speed for distance. Too fast and it will fly through walls. (Bug?)
Then apply the speed and distance with a multiplier based on the size of your scene.
The total distance should be something greater than possible in any direction.
That keeps the thing moving until it hits a wall or you tell it to move in a different direction.
This works really well.
Using the physics body movement, it seems best suited to a platformer situation where you have a gravity.
In a top down view, there isn't normally gravity.
( though it is imaginable that you could want something that exerts gravity on a sprite to suck it in)
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.