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
Related
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
I use ball.frame.intersects(bar.frame), where ball = SKShapeNode(circleOfRadius: BALL_RADIUS) and bar = SKShapeNode(rectOfSize: <Some CGSize object>)
While this works, there is an invisible square in which the circle is inscribed. The diameter of the circle is the same as the side of the square. This square is what ball.frame is, so sometimes, the ball will act like it's intersecting the bar, but visually it's not.
In this screenshot you can see that the ball is stopped, and therefore the game is over because ball.frame.intersects(bar.frame) returned true, even though visually the ball isn't even touching the bar.
So how do I check if the ball is really intersecting the bar, and not if the ball's frame is intersecting?
SpriteKit has decently powerful collision detection in its physics subsystem — you don't have to roll your own.
Create SKPhysicsBody objects for the ball and the bars, using init(circleOfRadius:) for the former and init(rectangleOfSize: for the latter.
Then, you can either:
Drive all the movement yourself, and use SKPhysics only to test for collisions. In this case you need to set your scene's physicsWorld.gravity to the zero vector, and call allContactedBodies() on the ball when you want to see if it's contacting a bar.
Let SKPhysics drive the movement, by setting velocities or applying impulses. In this case you'll need to set a contact delegate for your physics world, and then you'll get a callback whenever a collision occurs.
For more details on either approach, see the docs.
After detecting that ball frame intersects box frame:
Determine the radius of the ball
Determine the point on the line that is the edge of the ball from the center of the ball to the edge of the box. (Hint: pythagorean theorem - hypotenuse = radius)
Is this point inside the box's rect?
Repeat for each point in the box.
So I want to fire a beam of laser from the enemy to the player.
so the laser sweeps from left to right. So i use the zrotation as seen below
As you can see, the frame of the player is intersecting the laserbeam. How do i overcome this problem such that i can detect the collision of laser image and the player image?
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 new to Objective-C and I am trying to make a simple spriteKit game which contains a circle and a ball inside it. I want to move the ball around in this circle as well on the iPhone movement. So far I have created the circle and am able to put the ball on the screen.
My problem is that I am not sure the ball is actually inside the circle or on top of it. When I attach a physics body on the ball, it drops out of the screen and does not stop on the circle's bottom edge. I am using SKShapeNode for both the circle and ball.
Please help me to provide the right documentation to go through or a little piece of code that can resolve this.
You need to assign appropriate physics bodies to your nodes. See Simulating Physics in the Sprite Kit Programming Guide.
From your description, it sounds like you need to create the outter circle's physics body with the bodyWithEdgeLoopFromPath: method (and provide a path representing the circle). Note that an edge loop physics body is not dynamic and can only collide with volume-based bodies.
The inner balls physics body can be created with bodyWithCircleOfRadius:.