Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Ball
- {
- private Vector2 m_Velocity;
- private Vector2 m_m_Position;
- public Ball(Vector2 m_Position)
- {
- m_m_Position = m_Position;
- m_Velocity = 0;
- }
- public void Update()
- {
- m_m_Position += m_Velocity;
- }
- public boolean colliding(Ball ball)
- {
- float xd = m_m_Position.getX() - ball.m_m_Position.getX();
- float yd = m_m_Position.getY() - ball.m_m_Position.getY();
- float sumRadius = getRadius() + ball.getRadius();
- float sqrRadius = sumRadius * sumRadius;
- float distSqr = (xd * xd) + (yd * yd);
- if (distSqr <= sqrRadius)
- {
- return true;
- }
- return false;
- }
- public void resolveCollision(Ball ball)
- {
- // get the mtd
- Vector2d delta = (m_Position.subtract(ball.m_Position));
- float d = delta.getLength();
- // minimum translation distance to push balls apart after intersecting
- Vector2d mtd = delta.multiply(((getRadius() + ball.getRadius()) - d) / d);
- // resolve intersection --
- // inverse mass quantities
- float im1 = 1 / getMass();
- float im2 = 1 / ball.getMass();
- // push-pull them apart based off their mass
- m_Position = m_Position.add(mtd.multiply(im1 / (im1 + im2)));
- ball.m_Position = ball.m_Position.subtract(mtd.multiply(im2 / (im1 + im2)));
- // impact speed
- Vector2d v = (this.velocity.subtract(ball.velocity));
- float vn = v.dot(mtd.normalize());
- // sphere intersecting but moving away from each other already
- if (vn > 0.0f) return;
- // collision impulse
- float i = (-(1.0f + Constants.restitution) * vn) / (im1 + im2);
- Vector2d impulse = mtd.multiply(i);
- // change in momentum
- this.velocity = this.velocity.add(impulse.multiply(im1));
- ball.velocity = ball.velocity.subtract(impulse.multiply(im2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement