<script src="http://spelprogrammering.nu/simple.js">
function start()
{
A = {v: {x: 3, y: -2}, x: 40, y: 400, m: 1};
B = {v: {x: -2, y: -3}, x: 420, y: 480, m: 1.5};
}
function update()
{
fill("black");
A.x += A.v.x;
A.y += A.v.y;
B.x += B.v.x;
B.y += B.v.y;
circle(A.x, A.y, 20, "red");
circle(B.x, B.y, 20, "green");
if (distance(A, B) < 40)
{
var normalVector = {x: A.x - B.x, y: A.y - B.y};
var normalUV = math.normalizeVector(normalVector);
var tangentUV = {x: -normalUV.y, y: normalUV.x};
var AN = math.dotProduct(A.v, normalUV);
var AT = math.dotProduct(A.v, tangentUV);
var BN = math.dotProduct(B.v, normalUV);
var BT = math.dotProduct(B.v, tangentUV);
var newAN = (AN*(A.m - B.m) + 2*B.m*BN) / (A.m + B.m);
var newBN = (BN*(B.m - A.m) + 2*A.m*AN) / (A.m + B.m);
var vecAN = math.multiplyScalar(normalUV, newAN);
var vecAT = math.multiplyScalar(tangentUV, AT);
var vecBN = math.multiplyScalar(normalUV, newBN);
var vecBT = math.multiplyScalar(tangentUV, BT);
A.v = math.addVectors(vecAN, vecAT);
B.v = math.addVectors(vecBN, vecBT);
}
}
</script>
Gå tillbaka