154
edits
Changes
→Useful code snippets
==Useful code snippets==
foo("Hello World");
==Collision Code Snippets ==
Two teams have asked me to upgrade the framework to handle collisions. You can complete the upgrade by adding the follow snippets:
// add this code to Design::initialize() - use proper values for min max points
jumper->setAxisAligned(Vector(-10, -10, -10), Vector(10, 10, 10));
pedestal->setAxisAligned(Vector(-10, -10, -10), Vector(10, 10, 10));
// add this code to Design::update() where dx, dy, dz are time increments in x, y, z directions
// check for collision
unsigned flags = 0u;
Vector d((float)dx, (float)dy, (float)dz);
d *= FORWARD_SPEED;
if (collision(pedestal, jumper, d, flags)) {
// respond to collision with left object
rollRight->translate(-d.x, -d.y, -d.z);
if (flags & 1u)
d.x = -d.x;
if (flags & 2u)
d.y = -d.y;
if (flags & 4u)
d.z = -d.z;
rollRight->translate(d.x, d.y, d.z);
}
// add this code to Frame.cpp and change signature of collision() in Frame.h
bool collision(const Shape* f1, const Shape* f2, Vector& d, unsigned& flags) {
// ...
else if (f1->axisAligned && f2->axisAligned) {
Vector a = f1->position();
Vector b = f2->position();
Vector ax = f1->maximum * f1->world();
Vector an = f1->minimum * f1->world();
Vector bx = f2->maximum * f2->world();
Vector bn = f2->minimum * f2->world();
collide =
ax.x >= bn.x && an.x <= bx.x &&
ax.y >= bn.y && an.y <= bx.y &&
ax.z >= bn.z && an.z <= bx.z;
if (collide) {
float lambdaxp, lambdayp, lambdazp, lambdaxn, lambdayn, lambdazn, lambda = 1.0f;
Vector n = d / d.length();
if (fabs(n.x) > 1.0e-5f) {
lambdaxp = (ax.x - bn.x) / n.x;
lambdaxn = (an.x - bx.x) / n.x;
if (lambdaxp <= 1.0f && lambdaxn >= 0.0f && lambdaxp < lambdaxn)
lambda = lambdaxp;
else if (lambdaxp <= 1.0f && lambdaxn >= 0.0f && lambdaxn < lambdaxp)
lambda = lambdaxn;
else if (lambdaxp <= 1.0f && lambdaxp >= 0.0f)
lambda = lambdaxp;
else if (lambdaxn >= 0.0f && lambdaxn <= 1.0f)
lambda = lambdaxn;
flags = 1u;
}
if (fabs(n.y) > 1.0e-5f) {
lambdayp = (ax.y - bn.y) / n.y;
lambdayn = (an.y - bx.y) / n.y;
float lambda0 = lambda;
if (lambdayp <= 1.0f && lambdayn >= 0.0f && lambdayp < lambdayn && lambda > lambdayp)
lambda = lambdayp;
else if (lambdayp <= 1.0f && lambdayn >= 0.0f && lambdayn < lambdayp && lambda > lambdayn)
lambda = lambdayn;
else if (lambdayp <= 1.0f && lambdayp >= 0.0f && lambda > lambdayp)
lambda = lambdayp;
else if (lambda >= 0.0f && lambdayn <= 1.0f && lambda > lambdayn)
lambda = lambdayn;
if (lambda != lambda0)
flags = 2u;
}
if (fabs(n.z) > 1.0e-5f) {
lambdazp = (ax.z - bn.z) / n.z;
lambdazn = (an.z - bx.z) / n.z;
float lambda0 = lambda;
if (lambdazp <= 1.0f && lambdazn >= 0.0f && lambdazp < lambdazn && lambda > lambdazp)
lambda = lambdazp;
else if (lambdazp <= 1.0f && lambdazn >= 0.0f && lambdazn < lambdazp && lambda > lambdazn)
lambda = lambdazn;
else if (lambdazp <= 1.0f && lambdazp >= 0.0f && lambda > lambdazp)
lambda = lambdazp;
else if (lambdazn >= 0.0f && lambdazn <= 1.0f && lambda > lambdazn)
lambda = lambdazn;
if (lambda != lambda0)
flags = 4u;
}
d -= lambda * n;
}
}