perfectly elastic collisions in sprite kit - ios

I have two balls of equal mass that hit each other straight on. I can't seem to figure out why there is a rebound effect on the incoming ball. I am using sprite kit.
I thought it was the restitution property. I have it set to 1 (which is what I thought it should be), but have tried many.
The only way I can kill the rebound is to change the mass, which messes up other things.
If the speed is slow it pretty much stops as it should. But at higher speeds there is a bounce back. They should just exchange velocity as it is a head on collision. Is it possibly some numerical rounding at high speeds or something?
http://h2physics.org/?cat=4

actually restitution 0 should disable any bouncing effect, 1 means the body leaves the surface with the same velocity it impacted with

Related

SpriteKit collision angle of line and circle is odd

Suppose a circle is enclosed in a diamond, and the circle heads due right. I would expect a series of continual 90 degree angles as the circle draws a square within the diamond.
What I'm seeing, however, is that the circle eventually gets pushed toward the center of the diamond after one or two collisions. What's going on?
The line is set to dynamic -> NO.
The ball gets reflected symmetrically to its angle of approach assuming it's a completely elastic collision. That means, to get the behavior you are looking for, the ball would need to approach exactly from one line mid-point to the next without any loss of energy.
Sprite Kit's physics engine will not be able to handle these exact values. The energy after an elastic collision in Sprite Kit is not guaranteed to be conserved. There will be small inaccuracies in the physics engine's floating point calculations. Also the collision itself may not be resolved as a perfect point-to-point reflection, which means the ball might "slide," which will result in the angle of reflection changing.
However I would still expect to see more collisions then just one or two. Make sure the restitution of both objects is set to 1.0 and make sure the ball's linear damping is set to 0. affectedByGravity should obviously be false. Friction should be set to 0. You can try setting usesPreciseCollisionDetection to true.
If you need the behavior you described in you picture to last indefinitely, you will need to set the position and velocity manually.

SpriteKit - Why my bouncing ball passes through the ground?

I'm working for a small bouncing ball game using the SpriteKit physics engine. My problem is:
When I apply a huge impulse on the bouncing ball to have it fall on ground fast, sometimes it may pass through the ground (very thin, height=2).
I find this in the Apple document, but it doesn't work.
Specify High Precision Collisions for Small or Fast-Moving Objects
When Sprite Kit performs collision detection, it first determines the locations of all of the physics bodies in the scene. Then it determines whether collisions or contacts occurred. This computational method is fast, but can sometimes result in missed collisions. A small body might move so fast that it completely passes through another physics body without ever having a frame of animation where the two touch each other.
If you have physics bodies that must collide, you can hint to Sprite Kit to use a more precise collision model to check for interactions. This model is more expensive, so it should be used sparingly. When either body uses precise collisions, multiple movement positions are contacted and tested to ensure that all contacts are detected.
ship.physicsBody.usesPreciseCollisionDetection = YES;
You can set your sprite's node.physicsBody.usesPreciseCollisionDetection = YES; but this will not guarantee a collision flag all the time as your sprite's velocity could simply be just too high.
You should apply a speed limit to your nodes as to prevent them from going too fast. Something like this:
if(node.physicsBody.velocity.dx > 200)
node.physicsBody.velocity = CGVectorMake(200, node.physicsBody.velocity.dy);
The above code example will limit the right movement, dx, of your node to 200 while keeping the dy (up/down) velocity as is.

SKPhysicsBody ball won't bounce if impulse is too small

I have an infinitely bouncing ball simulation that works properly. The ball bounces around the screen borders forever.
One minor problem though is that if the starting impulse is too small the ball never bounces to begin with.
I experimented and eventually found that a starting impulse of at least 2.1 for my ball is required for it to bounce.
ball.physicsBody!.applyImpulse(CGVectorMake(-2.1, -2.1))
If I set the value to 2 the ball never starts bouncing it just stops at the bottom left corner.
ball.physicsBody!.applyImpulse(CGVectorMake(-2.0, -2.0)) //ball stops
I actually came across a question that answered this, but I forgot how to get to it. I know there is some lower limit that spritekit enforces to make a moving object stop when a collision occurs.
If I could get to that answer that would be great. Also if there is a way to override that, and make an object still bounce with a slower starting impulse that would be great too thanks.
edit: so I re-found the other question, SpriteKit ball loses all energy hitting wall, restitution=1
so my new question is, is there a way to set or lower the velocity threshold in sprite kit? I would like my ball to be able to move slower.
edit: anyone?
Your ball has a weight to it.
It's like in the real world - if you try push a chair with little force, it won't move as it has a weight.
However, if you push harder, the chair will start to move, but it does not start from 0.1 N of force (it really depends on the weight, friction and a few other factors).

corona physics - different mass object?

this is what i'm trying to do, 2 balls will drop at different height, their bounce is set to 1.0, which means after bounce they will go back to their original position. But the higher ball will drop and bounce faster than the lower ball, so when the higher ball bounces and goes back to it's original position once, the lower ball finishes its first bounce too.
is ok that they don't bounce once at the same time? i just want to know how i can change their speed.
so far what i have found is that we can't change their mass, density won't effect drop speed, set gravity will effect both. any solution please?
and i'm trying to change the mass of those ball, so the ball that higher from the ground can drop faster
Heavier things do not fall faster. Either in reality or a proper simulation thereof. The reason why we think of heavy things falling faster is because of air resistance. Objects moving through the air have resistance from that air, and this slows down light objects more than heavier ones.
So you would either have to break the physics system or implement air resistance.

Collision Handling Between Circle and Line Segments

I'm implementing a small game and am having trouble getting the physics working properly.
In this game, there is one ball (a circle which moves from frame to frame, and may change radius) and several walls (line segments which also change and move from frame to frame). I can detect collisions properly, and making the ball bounce off in the correct direction is no problem.
Difficulties arise in situations where the ball intersects a line in one frame, then intersects it again in the subsequent frame, causing a double bounce. I could move the ball back along the normal of the line until it is in a valid position, but this causes really weird behaviour when the line in question is being hit along its axis (imagine a ping pong ball falling down on an upright toothpick and suddenly shifting aside so that it is on one side of the toothpick...). There are also a few issues when the ball intersects more than one line in a given frame (imagine four lines together making a rectangle and the ball intersecting the corner of said rectangle) -- which direction should it bounce off? In which direction should it shift?
I don't really have a specific question, but am looking for tips or some useful tutorials. All the 2D ones I've managed to find so far only cover rectangle intersections.
I'm using XNA if it makes any difference.
Thanks,
Cameron
This is an universal problem with most physics libraries. If you google for "penetration depth" (together with "physics", I suggest, or you might find something entirely different :D) you will find that even these libraries use tricks like yours.
There are two solutions to this:
The cheap one is to increase your update frequency. Move objects in 10 smaller steps instead of a single big one and you will have less penetration, so fixing it by offsetting the ball away from the wall will be less visible.
The expensive one is continuous collision detection. There are algorithms that can tell you, given a moving and a stationary object, the exact point in time both will will intersect. Google "swept sphere rectangle intersection" to find some of these.
You can then update like this: ball needs to move by 1.0 units. Check for collision. Collision occurs after 0.25 units, so move ball by 0.25 units, calculate reflection vector (so the ball bounces off the wall), repeat collision check with remaining 0.75 units (until you know the final position of the ball). This avoids penetrations entirely and even if your ball is moving so fast that it would normally skip over the wall in a single update, the collision will be detected.
It's generally accepted that, because of the timestep your collisions will intersect past an acceptable point in between updates.
Basically, you have to interpolate back to the point in between the last and current frame where the collision truly happened, move the object back to that point, and then calculate the forces, etc.

Resources