Change direction of moving SKSpriteNode? - ios

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.

Related

Need assistance with collision physics between two nodes

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

How can I make a Vector that goes to a position?

I have a spritenode that runs an action to go to a position. In the future I want to have the spritenode collide with other nodes in the way to stop it from reaching that position. So I thought of an impulse. But how do I make a vector that moves to a position?

Sprite-Kit: Applying physics to a projectile under the influence of moveTo

I am making a top down game where I am having cannons fire and having their projectiles move to the clicked location via the SKAction moveTo:duration. I am supposed to have wind change the trajectory so I have the cannonball implemented as an SKPhysicsBody and I am setting gravity to be a the windspeed since it is the only thing I can find that applies a constant force like wind would. The problem I am having is moveTo is probably the wrong way to be implementing the cannonball. The ball moves according to the path it should but then lands at the tapped location which is not what I want. I can't find a good alternative to moveTo. Any ideas?
You have to solve the projectile equations to determine the force needed and angle to applyImpulse. Solving the equation is only considering the gravity force and impulse force applied to projectile using the applyImpulse with your distances known from your cannons to user's click projectile destination.
Your applyImpulse launches the projectile, you start your wind and other external forces which will have an impact on the projectile path, changing it's destination from that of user's clicked.
You need to know few physics and math to re-arrange the equations and solve them :)
Not sure if you need to do all the advanced math, just set the angle of your cannon and apply impulse to the cannonball, and use the scene.physicsWorld.gravity as your wind, just make sure you also give your cannonball a physicsbody that has dynamic and affectedByGravity set to true.
To calculate the angle, you would do:
let angle = atan2(Double(touch.y - cannon.y),Double(touch.x - cannon.x))

Incorrect collision? SpriteKit

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

Breakout Paddle Collision Angle

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.

Resources