2014-02-16 14:32:28 +00:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "object.h"
|
|
|
|
#include "vector.h"
|
|
|
|
#include "collision.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "fight.h"
|
|
|
|
#include "glapi.h"
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
Object::Object(void) {
|
|
|
|
appearance = NULL;
|
|
|
|
geometry = NULL;
|
|
|
|
invmass = 0.0;
|
|
|
|
setPosition(0, 0, 0);
|
|
|
|
vectorSet(momentum, 0, 0, 0);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
invmomentofinertia = 0.0;
|
|
|
|
matrixIdentity(rotation);
|
|
|
|
vectorSet(angularmomentum, 0, 0, 0);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
setCollisionGroup(COLLISIONGROUP_NONE);
|
|
|
|
gravity = false;
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::prepare(void) {
|
|
|
|
if (appearance != NULL)
|
|
|
|
appearance->prepare();
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DT 0.01
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::move(void) { moveStep(DT); }
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::moveStep(float dt) {
|
|
|
|
if (invmass == 0)
|
|
|
|
return;
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
if (vectorDot(momentum, momentum) > 1.0e+5)
|
|
|
|
vectorSet(momentum, 0, 0, 0);
|
|
|
|
if (vectorDot(angularmomentum, angularmomentum) > 1.0e+5)
|
|
|
|
vectorSet(angularmomentum, 0, 0, 0);
|
|
|
|
calculateStateVariables();
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
float velocitydt[3];
|
|
|
|
vectorScale(velocitydt, velocity, dt);
|
|
|
|
vectorAdd(position, velocitydt);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
float rotationdt[9];
|
|
|
|
if (vectorIsZero(angularmomentum)) {
|
|
|
|
matrixIdentity(rotationdt);
|
|
|
|
} else {
|
|
|
|
float angularvelocitydt[3];
|
|
|
|
vectorScale(angularvelocitydt, angularvelocity, dt);
|
|
|
|
matrixCreateRotation(rotationdt, angularvelocitydt);
|
|
|
|
}
|
|
|
|
matrixMultiply(rotation, rotation, rotationdt);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
vectorScale(angularmomentum, 0.99);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::calculateStateVariables(void) {
|
|
|
|
getVelocity(velocity);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
if (vectorIsZero(angularmomentum)) {
|
|
|
|
invmomentofinertia = 0;
|
|
|
|
} else {
|
|
|
|
invmomentofinertia =
|
|
|
|
invmass * 1.0 / geometry->calculateMomentOfInertia(angularmomentum);
|
|
|
|
}
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
vectorScale(angularvelocity, angularmomentum, invmomentofinertia);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::setPosition(float x, float y, float z) {
|
|
|
|
position[0] = x;
|
|
|
|
position[1] = y;
|
|
|
|
position[2] = z;
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::getPosition(float *position) {
|
|
|
|
vectorCopy(position, this->position);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::getVelocity(float *velocity) {
|
|
|
|
vectorCopy(velocity, momentum);
|
|
|
|
vectorScale(velocity, invmass);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::getVelocity(float *velocity, float *point) {
|
|
|
|
getVelocity(velocity);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
float tangentialvelocity[3];
|
|
|
|
getTangentialVelocity(tangentialvelocity, point);
|
|
|
|
// float tv[3];
|
|
|
|
// transformVector(tv, tangentialvelocity);
|
|
|
|
vectorAdd(velocity, tangentialvelocity);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::getTangentialVelocity(float *target, float *point) {
|
|
|
|
if (vectorIsZero(angularmomentum)) {
|
|
|
|
vectorSet(target, 0, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
vectorCross(target, angularmomentum, point);
|
|
|
|
vectorScale(target, invmomentofinertia);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::getMomentum(float *momentum) {
|
|
|
|
vectorCopy(momentum, this->momentum);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::setMass(float mass) {
|
|
|
|
if (mass == 0)
|
|
|
|
this->invmass = 0;
|
|
|
|
else
|
|
|
|
this->invmass = 1.0 / mass;
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
float Object::getMass(void) {
|
|
|
|
if (invmass == 0)
|
|
|
|
return 0;
|
|
|
|
return 1.0 / invmass;
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::setCollisionGroup(int group) { this->collisiongroup = group; }
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
int Object::getCollisionGroup(void) { return collisiongroup; }
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::addImpulse(float *impulse, float *contactpoint) {
|
|
|
|
if (invmass == 0)
|
|
|
|
return;
|
|
|
|
float angularimpulse[3];
|
|
|
|
vectorCross(angularimpulse, contactpoint, impulse);
|
|
|
|
vectorAdd(angularmomentum, angularimpulse);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
vectorAdd(momentum, impulse);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
float t1[3], t2[3];
|
|
|
|
vectorAdd(t1, contactpoint, position);
|
|
|
|
vectorNormalize(t2, impulse);
|
|
|
|
vectorAdd(t2, t1);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
// addGraphicsVector(t1, t2, vectorLength(impulse));
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::addExternalForce(float *force) {
|
|
|
|
float impulse[3];
|
|
|
|
vectorScale(impulse, force, DT);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
float contact[3] = {0, 0, 0};
|
|
|
|
this->addImpulse(impulse, contact);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::transformPoint(float *newpoint, float *oldpoint) {
|
|
|
|
vectorMatrixMultiply(newpoint, oldpoint, rotation);
|
|
|
|
vectorAdd(newpoint, position);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::unTransformPoint(float *newpoint, float *oldpoint) {
|
|
|
|
vectorSub(newpoint, oldpoint, position);
|
|
|
|
float rotmat[9];
|
|
|
|
matrixTranspose(rotmat, rotation);
|
|
|
|
vectorMatrixMultiply(newpoint, newpoint, rotmat);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::transformVector(float *newvector, float *oldvector) {
|
|
|
|
vectorMatrixMultiply(newvector, oldvector, rotation);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::unTransformVector(float *newvector, float *oldvector) {
|
|
|
|
float rotmat[9];
|
|
|
|
matrixTranspose(rotmat, rotation);
|
|
|
|
vectorMatrixMultiply(newvector, oldvector, rotmat);
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::hitForce(float speed, float *speed2, Object *source) {
|
|
|
|
float tolerance = 1.0;
|
|
|
|
if (speed > tolerance) {
|
|
|
|
Sound *sound;
|
|
|
|
if (rand() & 1)
|
|
|
|
sound = softhitsound1;
|
|
|
|
else
|
|
|
|
sound = softhitsound2;
|
|
|
|
float volume = (speed - tolerance) * 2;
|
|
|
|
if (volume > 1)
|
|
|
|
volume = 1;
|
|
|
|
sound->setVolume(volume);
|
|
|
|
sound->play(30 + random(70));
|
|
|
|
}
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::setGravity(bool enabled) { gravity = enabled; }
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Object::draw(void) {
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(position[0], position[1], position[2]);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
GLfloat glmatrix[16] = {rotation[0], rotation[1], rotation[2], 0,
|
|
|
|
rotation[3], rotation[4], rotation[5], 0,
|
|
|
|
rotation[6], rotation[7], rotation[8], 0,
|
|
|
|
0, 0, 0, 1};
|
|
|
|
glMultMatrixf(glmatrix);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
if (appearance != NULL)
|
|
|
|
appearance->draw();
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
glPopMatrix();
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|