I'm making a game where a ball rolls down a series of blocks and the player has to anticipate where the ball is going to end up. However, I have a problem with the collision physics that I can't seem to understand.
The Problem
Whenever the ball touches the paddle (What the player moves), the ball does not stop and keeps rolling until it rolls off the paddle. What I would like to happen is for the ball to stop as soon as it touches the paddle and "stick" there. The attributes for the ball are :
and the attributes for the paddle are :
Any advice or solutions would be very helpful!
Are you implementing a SKPhysicsContactDelegate?
If so, when you detect the collision you could set the ball's velocity to zero, like this:
ball.physicsBody?.velocity = CGVector.zero
Related
I am making a simple game in spritekit where the screen is full of balls with physics bodies. when the player taps a ball the ball explodes. I want to apply an angular impulse at the point of touch which will push all the surrounding balls away like a real world explosion. I tried fields but they are not really good as all the physics field nodes are forces not impulses. I checked all the impulse methods but they are only to apply force to the node itself. I also tried this:
self.physicsBody?.applyAngularImpulse(100)
and
self.physicsBody?.applyImpulse(impulse: CGVector, at: CGPoint)
thinking it would apply impulse to all child nodes in the scene (all the balls are child of the scene) but had no effect.
I thought this would be very simple but I am out of ideas.
appreciate your time. cheers
This approach is not a real-world explosion simulation but can help you as an idea.
I'm making actions like this on each touched node:
node.run(SKAction.sequence([
SKAction.fadeOut(withDuration: 0.0),
SKAction.scale(to: 10, duration: 0.1),
SKAction.removeFromParent()
]))
You can get this example here: https://github.com/Maetschl/SpriteKitExamples/blob/master/BoxWithBalls/BoxWithBalls/GameScene.swift
I have a physics body that is uneven but not dynamic (the terrain) and a physics body (character) that is dynamic and is on top of the terrain, and I want this character to move along the terrain simulating kind of a "walking" action where it will keep going up the terrain but it won't fall back (or move back) like a ball because of gravity, and to set a maximum tilt so that it does not tip over.
My attempt was to add a force in the direction I want the character to move but this is causing the character to fall back due to gravity, and I don't want to disable gravity because then character won't fall down when going down the terrain.
Thank you very much
this is why using a physics engine for a platforming game is very difficult and not always a great approach..
you can try sprite.physicsBody.allowsRotation = false
might help
Create SKAction to move your character to destination point or dx and dy. Run that SKAction from your character node with the runAction. Like
SKAction.moveByX(...
When the action is finished, gravity will bring the character down.
If the movement is slow, play with the character's physicsBody friction and/or mass.
You can also change the the character's physicsBody.velocity = CGVectorMake(
And your terrain will be better with edgeLoop physicsBody which won't be dynamic.
And with uneven you mean inclined but smooth? If not smooth and with edges, your character may get held up at an edge depending on center of gravity of character and if you are allowing rotation.
I have a moving ball which is SKSpriteNode, and I want to change its direction alone without applying any force or impulse . When user starts touch interactions, I want the moving ball to move in the direction of touch.
In the picture the ball is initially given a impulse. I don't want to change the velocity of ball but what I want is I just want to move it to right or left from its current position based on user touch. When user tap on right side of ball, the ball should bend little bit toward right and vice - versa.
Things I Tried
Applied impulse over ball in the touch direction and it caused ball to move extremely fast after few touches.
Applied SKAction movetoPoint method and gave my touch point to that method but it resulted in direction flip (i.e.) when ball is moving from bottom to top and if touch point is below the ball, It resulted in inverting ball direction. I Just want ball to bend along touch point without inverting its direction.
Any help or suggestions would be greatly appreciated.
Made to to work by making velocity to 0 when user touches began and then by applying an impulse in the direction of touch. Anyways thank you bro for your help.
I'm making a game in which there are horizontal rectangles falling down and a ball in the scene, when the ball collides with the rectangle the moveTo action for the rectangle should stop. This [node removeAllActions] is the first thing called in the didbegincontact method. This should basically freeze the frame when the contact starts so the ball and recitingle should be in contact.
But this is not the some 50% of the time: here is an example:
I enabled the view physics option and the physics body fits tightly with the ball so I don't think the physics body for the ball is wrong.
I have enabled usesPreciseCollisionDetection for the ball only
Is this just a lag in the game calling the removeAllActions?
Please help thanks
I'm making a Breakout clone and having a little trouble with the ball-to-paddle collisions. I have a rectangle represent both the ball and the paddle and when they intersect, the Y vector representing the ball's velocity is negated (as shown below). That all works fine. The problem is when the paddle is moving to the right I want it to nudge the ball a little to the right (as opposed to it just reflecting off normally) and I want the same to happen in the opposite direction is the paddle is moving to the left. I'm not sure how to go about doing this and I've looked all over. Any help would be appreciated. Thanks.
if (paddleRectangle.Intersects(ballRectangle))
{
ballVelocity.Y *= -1;
collision.Play(); //a collision sound
}
EDIT: Basically I want to slightly change the angle at which the ball bounces off the paddle based on which direction the paddle is moving. If the paddle is not moving, then the ball will bounce normally (by inverting the Y component of the ball's velocity)
Add the paddle's velocity vector to the paddle's normal vector (this basically bends the normal in the direction the paddle is moving) and normalize the result. Use this as the collision normal for reflection.
Vector2 collisionNormal = Vector2.Normalize(paddleNormal + (paddleVelocity * desiredEffectAmount));
ballVelocity = Vector2.Reflect(ballVelocity, collisionNormal);
i did some grinding in my head... and here are results. to achieve that you will need, moving direction of paddle, speed of paddle, ball speed, ball direction. and then by some math function calucalte angle and speed of bounce.
i think this image (if bouncing is phisicaly correct) will give you idea how to create this. can't help you with function that will handle this but i would go and try that way as in image.
You want a little friction, but probably not real friction.
Try scaling the paddle speed down by some factor and adding it to the ball velocity.