BANANAMAN
Resident Star Battle Expert.
- Reaction score
- 150
Headerfile
cpp file
I have a header file and it's cpp.
The header file has all the variables the cpp file needs but the cpp file doesn't recognize the variables in the headerfile.
The other header files/cpp files work this is the only one with the problem.
Any suggestions?
Code:
#ifndef _RotatingLine_H_
#define _RotatingLine_H_
#include <math.h>
#include <stdio.h>
#include "Vector2D.h"
#include "Shape.h"
#include "Line.h"
#include "Wall.h"
#include "CollisionData.h"
#include "Bola.h"
#include "Part.h"
#include "Table.h"
class RotatingLine : public Shape
{
public:
Vector2D c, p; // points of the line
Vector2D n; // normal of the line
static const double l; // length of the line
static const double angle; // angle of the line
static const rot; // amount the line should rotate this frame
static const inertia; // moment of inertia scaler
double lineCoefficient; // coefficient of the point of collision along the line of collision
Vector2D pInitial, pFinal; // initial and final end-point positions of the line
Vector2D nInitial, nFinal; // final normal of the line
Vector2D pUpper, pLower; // upper and lower end-point positions of the line
Vector2D nUpper, nLower; // upper and lower normals of the line
RotatingLine(int x, int y, double x2, double y2, Part owner);
void Rotate(double t);
void PostCheck();
CollisionData *CheckCollision(Bola bola);
CollisionData *CheckBallInside(Bola bola);
CollisionData *CheckCollisionLine(Bola bola);
CollisionData *CheckCollisionPoint(Vector2D c, Bola bola);
CollisionData *CheckCollisionSide(Bola bola);
};
#endif
cpp file
Code:
#include <math.h>
#include <stdio.h>
#include "Vector2D.h"
#include "Shape.h"
#include "Line.h"
#include "Wall.h"
#include "CollisionData.h"
#include "Bola.h"
#include "Part.h"
#include "Table.h"
#include "RotatingLine.h"
RotatingLine::RotatingLine(int x, int y, double x2, double y2, Part owner)
{
this->c = new Vector2D(x, y);
this->p = new Vector2D(x2, y2);
l = sqrt((x2 - x)*(x2 - x) + (y2 - y)*(y2 - y));
angle = atan2(y2 - y, x2 - x);
n = p->getNormal(c);
setBoundaries(x - (int)l, y - (int)l, x + (int)l, y + (int)l);
setOwner(owner);
}
//*********************************************************
// Rotate
//
// sets the amount the line should rotate this frame. Must
// be -90 < t < 90
//*********************************************************
void RotatingLine::Rotate(double t)
{
rot = t;
}
//*********************************************************
// Post Check
//
// after the collision check, update variables
//*********************************************************
void RotatingLine::PostCheck()
{
angle += rot;
rot = 0;
p->set(c->x + cos(angle)*l, c->y + sin(angle)*l);
n = p->getNormal(c);
}
//*********************************************************
// Check Collision
//
// check for and report collisions with the ball
//*********************************************************
RotatingLine::CollisionData CheckCollision(Bola *bola)
{
// the flipper is not moving, treat as a line
if (rot == 0)
{
p->set(c->x + cos(angle)*l, c->y + sin(angle)*l);
n = p->getNormal(c);
CollisionData data = CheckCollisionLine(bola);
if (data != NULL)
{
return data;
}
n = c->getNormal(p);
data = CheckCollisionLine(bola);
if (data != NULL)
{
return data;
}
data = CheckCollisionPoint(c, bola);
if (data != NULL)
{
return data;
}
data = CheckCollisionPoint(p, bola);
if (data != NULL)
{
return data;
}
return NULL;
}
// the flipper is moving
else
{
if (rot > 0)
{
pUpper = new Vector2D(c->x + cos(angle)*l, c-> + sin(angle)*l);
pLower = new Vector2D(c->x + cos(angle + rot)*l, c->y + sin(angle + rot)*l);
nUpper = pUpper->getNormal(c);
nLower = c->getNormal(pLower);
pInitial = pUpper->copy();
nInitial = pUpper->copy();
pFinal = pLower->copy();
nFinal = nLower->copy();
}
else
{
pUpper = new Vector2D(c->x + cos(angle + rot)*l, c->y + sin(angle + rot)*l);
pLower = new Vector2D(c->x + cos(angle)*l, c->y + sin(angle)*l);
nUpper = pUpper->getNormal(c);
nLower = c->getNormal(pLower);
pInitial = pLower->copy();
nInitial = pLower->copy();
pFinal = pUpper->copy();
nFinal = nUpper->copy();
}
CollisionData data;
// check if the ball is inside of the line's path
data = CheckBallInside(bola);
if (data != NULL)
{
System->out->println("inside");
return data;
}
// check if the ball collides with the initial position of the line
p->set(pInitial);
n = nInitial->copy();
data = CheckCollisionLine(bola);
if (data != NULL)
{
System->out->println("inital");
return data;
}
// check if the ball collides with the final position of the line
p->set(pFinal);
n = nFinal->copy();
data = CheckCollisionLine(bola);
if (data != NULL)
{
// calculate the amount of energy to transfer to the ball
vector2d e = data->n->copy();
e->scale(lineCoefficient * abs(rot) * inertia);
data->e = e;
System->out->println("final");
return data;
}
// check if the ball collides with the pivot point
data = CheckCollisionPoint(c, bola);
if (data != NULL)
{
System->out->println("pivot");
return data;
}
//check if the ball collides with the side
data = CheckCollisionSide(bola);
if (data != null)
{
System->out->println("side");
return data;
}
// check if the ball collides with the initial end point
data = CheckCollisionPoint(pInitial, bola);
if (data != NULL)
{
System->out->println("inital end");
return data;
}
// check if the ball collides with the final point
data = CheckCollisionPoint(pFinal, bola);
if (data != NULL)
{
// calculate the amount of energy to transfer to the ball
vector2d e = data->n->copy();
e->scale(Math->abs(rot) * inertia);
data->e = e;
System->out->println("final end");
return data;
}
return null;
}
}
//*********************************************************
// Check Ball Inside
//
// check if the ball is inside the flipper's path
//*********************************************************
RotatingLine::CollisionData CheckBallInside(Bola bola)
{
Vector2D c_ballPos = new vector2d(c, bola->pos);
// check if the distance from the pivot point
// if the distance is less than the ball's radius, ignore because that means the ball is inside the pivot point, which should not be possible
if (c_bolaPos->magnitude <= l + bola->r && c_bolaPos->magnitude > bola->r)
{
// get the distance from the ball to the upper and lower lines
double dUpper = c_ballPos->dot(nUpper);
double dLower = c_ballPos->dot(nLower);
// check if the ball is inside both the top and bottom lines
if (dUpper < bola->r && dLower < bola.r)
{
// set the point of collision on the line by scaling c->pFinal normal by c->ballPos magnitude and adding it to c
Vector2D PoCL = new Vector2D(c, pFinal);
PoCL->scale(c_ballPos->magnitude / PoCL->magnitude); //(magnitude is now c_ballPos.magnitude)
PoCL->add(c);
// set the point of collision by projecting the ball along the normal of the final line ballR units
Vector2D PoC = PoCL->copy();
Vector2D nFinalR = nFinal->copy();
// if the ball is hitting the end of the rotating line
if (c_ballPos->magnitude > l)
{
// don't move the ball all the way out
nFinalR->scale(Math->sqrt(bola->r*ball->r - (c_ballPos->magnitude - l)*(c_ballPos->magnitude - l)) + 0.1d);
PoC->add(nFinalR);
n = new vector2d(pFinal, PoC);
n->normalize();
}
else
{
nFinalR->scale(bola->r + 0->2d);
PoC->add(nFinalR);
n->set(nFinal);
}
// calculate the amount of energy to transfer to the ball
// calculate the Coefficient of the collision along line of the length of the line
PoCL->subtract(c);
lineCoefficient = PoCL->magnitude / l;
if (lineCoefficient > 1.0d)
lineCoefficient = 0.0d;
Vector2D e = n->copy();
e->scale(lineCoefficient * abs(rot) * inertia);
// check to see if the ball is moving in the same direction as the flipper
double idp = ball->vel->dot(n);
double aDamping;
if (idp > 0)
{
// if so, set the damping to -1. This will keep the ball's velocity from being changed BEFORE the energy from the flipper is added (no damping will occur sense the ball is moving in the same direction as the flipper, therefore no loss in energy, only energy added)
aDamping = -1.0d;
}
else
aDamping = damping;
return new CollisionData(PoC, n, 0.0001d, aDamping, friction, e, this);
}
}
return null;
}
//*********************************************************
// Check Collision Line
//
// checks if the ball collides with a line specified by
// c, p, and n
//*********************************************************
RotatingLine::CollisionData CheckCollisionLine(Bola bola)
{
// get the inner dot product
double idp = bola->vel->dot(n);
// if it is positive, we are moving in the same direction as the normal
if (idp > 0)
return NULL;
// calculate how long the point on the ball nearest the line takes to hit the line
// the nearest point on the ball is simply the center of the ball minus the normal times the radius of the ball
Vector2D ballPosNear = new Vector2D(bola->pos->x - n->x * bola->r, bola->pos->y - n->y * bola->r);
// solve for when the nearest position collides with the line
// p1->(ballPosNear) dot n = 0
// ballPosNear = <Box + VxT i, Boy + VyT j>
if (ball->vel->x * n->x + ball->vel->y * n->y == 0)
return null;
double t = ((c->x - ballPosNear->x) * n->x + (c->y - ballPosNear->y) * n->y)/(ball->vel->x * n->x + ball.vel->y * n->y);
double d1 = new Vector2D(p1, ball->posLast)->dot(n);
double d2 = new Vector2D(p1, ball->pos)->dot(n);
double t = (d1 - ball.r) / (d1 - d2);
// make sure the collision will happen during this frame
if (t < 0 || t > ball->time)
return NULL;
// calculate the point of collision on the line u
// PosNear + vel*t = p1 + u(p2 - p1)
// u = (PosNear + vel*t - p1)/(p2 - p1)
double u;
if (p->x == c->x)
u = (ballPosNear->y + ball->vel->y*t - c->y)/(p->y - c->y);
else
u = (ballPosNear->x + ball->vel->x*t - c->x)/(p->x - c->x);
// make sure the point of collision is on the line
if (u < 0 || u > 1)
return NULL;
// calculate the point of collision
vector2d PoC = new Vector2D(ball->pos->x + ball->vel->x * t, ball->pos->y + ball->vel->y * t);
lineCoefficient = u;
return new CollisionData(PoC, n, t, damping, friction, this);
}
//*********************************************************
// Check Collision Point
//
// checks if the ball collides with a point
//*********************************************************
RotatingLine::CollisionData CheckCollisionPoint(Vector2D c, Ball ball)
{
// solve for the possible times of collision
// ||(B + VT) - C|| = Br
// sqrt(((Bx + VxT) - Cx)^2 + ((By + VyT) - Cy)^2) = Br
double f = pow(2*(ball->pos->x*ball->vel->x + ball->pos->y*ball->vel->y - c->x*ball->vel->x - c->y*ball->vel->y), 2) - 4*(ball->vel->x*ball->vel->x + ball->vel->y*ball->vel->y)*(-2*ball->pos->x*c->x - 2*ball->pos->y*c->y + ball->pos.x*ball->pos->x + ball->pos->y*ball->pos->y + c->x*c->x + c->y*c->y - ball->r*ball->r);
if (f < 0)
return NULL;
double a = sqrt(f);
double b = -2*ball->pos->x*ball->vel->x - 2*ball->pos->y*ball->vel->y + 2*c->x*ball.vel.x + 2*c->y*ball->vel->y;
double d = 2*(ball->vel->x*ball->vel->x + ball->vel->y*ball->vel->y);
double t;
double t1 = (a + b) / d;
double t2 = (-a + b) / d;
// both possible times of collision must be in front of the ball, otherwise we are already inside the ball
if (t1 < 0 || t2 < 0)
return NULL;
// choose the time that is soonest
if (t1 < t2)
t = t1;
else
t = t2;
// make sure the collision will happen in this frame
if (t > ball->time)
return NULL;
// calculate the point of collision
Vector2D dPos = ball->vel->copy();
dPos->scale(t);
Vector2D PoC = ball->pos->copy();
PoC->add(dPos);
// calculate the normal
// (C - PoC)/(||C - PoC||)
vector2d n = new vector2d(PoC, c);
n->normalize();
return new CollisionData(PoC, n, t, damping, friction, this);
}
//*********************************************************
// Check Collision Side
//
// checks if the ball collides with the side
//*********************************************************
RotatingLine::CollisionData CheckCollisionSide(Ball ball)
{
// check if the ball collides with the side
// solve for the possible times of collision
// ||(B + VT) - C|| = Br + Cr
// sqrt(((Bx + VxT) - Cx)^2 + ((By + VyT) - Cy)^2) = Br + Cr
double f = pow(2*(ball->pos->x*ball->vel->x + ball->pos->y*ball->vel->y - c->x*ball->vel->x - c->y*ball->vel->y), 2) - 4*(ball->vel->x*ball->vel->x + ball->vel->y*ball->vel->y)*(-2*ball->pos->x*c->x - 2*ball->pos->y*c->y + ball->pos->x*ball->pos->x + ball->pos->y*ball->pos->y + c->x*c->x + c->y*c->y - l*l - 2*l*ball->r - ball->r*ball->r);
if (f < 0)
return NULL;
double a = sqrt(f);
double b = -2*ball->pos->x*ball->vel->x - 2*ball->pos->y*ball->vel->y + 2*c->x*ball->vel->x + 2*c->y*ball->vel->y;
double d = 2*(ball->vel->x*ball->vel->x + ball->vel->y*ball->vel->y);
double t;
double t1 = (a + b) / d;
double t2 = (-a + b) / d;
// both possible times of collision must be in front of the ball, otherwise we are already inside the circle
if (t1 < 0 || t2 < 0)
return NULL;
// choose the time that is soonest
if (t1 < t2)
t = t1;
else
t = t2;
// make sure the collision will happen in this frame
if (t > ball.time)
return NULL;
// calculate the point of collision
vector2d PoC = new vector2d(ball->pos->x + ball->vel->x * t, ball->pos->y + ball->vel->y * t);
// calculate the normal
// (C - PoC)/(||C - PoC||)
n = new vector2d(PoC, c);
n->normalize();
// get the distances from the ball to the upper and lower lines
Vector2D c_PoC = new Vector2D(c, PoC);
double dUpper = c_PoC->dot(nUpper);
double dLower = c_PoC->dot(nLower);
if (dUpper > 0 || dLower > 0)
return NULL;
// calculate the amount of energy to transfer to the ball
V ector2d e = n->copy();
e->scale(abs(rot) * inertia);
return new CollisionData(PoC, n, t, damping, friction, e, this);
}
}
I have a header file and it's cpp.
The header file has all the variables the cpp file needs but the cpp file doesn't recognize the variables in the headerfile.
The other header files/cpp files work this is the only one with the problem.
Any suggestions?