I'm doing a kirby-esque type game and I'm having issues with my sprite rotating on during flight as well as falling. Once the sprite is tapped it rotates CCW slightly and then rises. When it begins falling it rotates CW more than the CCW rotation and never resets to its original angle. Any help would be appreciated.
Try setting
sprite_object.isFixedRotation = true
Related
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 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))
The user controls a snake moving on the surface of a sphere. I have the camera and the head of the snake attached to an imaginary sphere the same size as the visible one:
headNode.addChildNode(cameraNode)
controlNode.addChildNode(headNode)
scene.rootNode.addChildNode(controlNode)
The user taps two buttons to rotate the snake left and right. I use some basic trig to convert this "2D" rotation into its components which I use to rotate the controlNode every frame:
velocity.x = cos(rotation)
velocity.y = sin(rotation)
controlNode.eulerAngles.x += velocity.x
controlNode.eulerAngles.y += velocity.y
This works very smoothly when moving over the majority of the sphere but at the two poles of the sphere (along the y axis) the snake behaves strangely. Here is a gif that shows the normal movement along the majority of the sphere and then the strange movement at the poles:
the gif was too large to upload so it is on imgur
The way it moves is like it bounces off the poles and I cannot for the life of me work it out. If anyone has a suggestion of an easier way to do the task it would be much appreciated!
Gimbal Lock was the issue. I fixed it by using the transform component of the node.
I wanted to use the iPhone's rotation system to be able to have an object follow below a line the user rotates. To do this, I need the angle at which the line is being measured as. When I set the rotation equal to M_PI_2, the object rotates 90 degrees counterclockwise. Which begs the question: Is the iPhone's angle-measurement system backwards? In other words, in portrait orientation, screen facing you, is positive theta clockwise?
Thanks.
I figured it out after doing some researching.
Since the iPhone's +x axis is to the right (same orientation as question) and the +y axis is down, it should make sense that the +theta direction is clockwise.
The normal axes
The iPhone's Axes
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.