"Undeclared" variables and other problems..(c++)

BANANAMAN

Resident Star Battle Expert.
Reaction score
150
Headerfile
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?
 

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
Did you put it into the include file of your compiler or include it in a linked folder?
 

BANANAMAN

Resident Star Battle Expert.
Reaction score
150
Header file
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  rot;         // amount the line should rotate this frame
	static  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
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
	//*********************************************************
	CollisionData& RotatingLine::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->y + 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)
			{
				printf("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)
			{
				printf("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;
				
				printf("final");
				
				return data;
			}
			
			// check if the ball collides with the pivot point
			data = CheckCollisionPoint(c, bola);
			if (data != NULL)
			{
				printf("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)
			{
				printf("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(abs(rot) * inertia);
				data->e = e;
				
				printf("final end");
				return data;
			}
			
			return null;
		}
	}
	
	//*********************************************************
	// Check Ball Inside
	//
	//  check if the ball is inside the flipper's path
	//*********************************************************
	CollisionData&  RotatingLine::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(sqrt(bola->r*ball->r - (c_ballPos->magnitude - l)*(c_ballPos->magnitude - l)) + 0.1);
					PoC->add(nFinalR);
					
					n = new vector2d(pFinal, PoC);
					n->normalize();
				}
				else
				{
					nFinalR->scale(bola->r + 0->2);
					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.0)
					lineCoefficient = 0.0;
				
				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 = bola->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.0;
				}
				else
					aDamping = damping;
				
				return new CollisionData(PoC, n, 0.0001, aDamping, friction, e, this);
			}
		}
		
		return NULL;
	}
	
	
	//*********************************************************
	// Check Collision Line
	//
	//  checks if the ball collides with a line specified by
	//   c, p, and n
	//*********************************************************
	CollisionData&  RotatingLine::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 (bola->vel->x * n->x + bola->vel->y * n->y == 0)
			return NULL;
		
		double t = ((c->x - ballPosNear->x) * n->x + (c->y - ballPosNear->y) * n->y)/(bola->vel->x * n->x + bola->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 - bola->r) / (d1 - d2);
		
		// make sure the collision will happen during this frame
		if (t < 0 || t > bola->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 + bola->vel->y*t - c->y)/(p->y - c->y);
		else
			u = (ballPosNear->x + bola->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
		Vector2DPoC = new Vector2D(bola->pos->x + bola->vel->x * t, bola->pos->y + bola->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
	//*********************************************************
	CollisionData&  RotatingLine::CheckCollisionPoint(Vector2D c, Bola *bola)
	{
		// 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*bola->vel->x + ball->pos->y*ball->vel->y - c->x*bola->vel->x - c->y*bola->vel->y), 2) - 4*(bola->vel->x*bola->vel->x + bola->vel->y*bola->vel->y)*(-2*bola->pos->x*c->x - 2*bola->pos->y*c->y + bola->pos.x*bola->pos->x + bola->pos->y*bola->pos->y + c->x*c->x + c->y*c->y - bola->r*bola->r);
		
		if (f < 0)
			return NULL;
		
		double a = sqrt(f);
		double b = -2*bola->pos->x*bola->vel->x - 2*bola->pos->y*bola->vel->y + 2*c->x*bola->vel.x + 2*c->y*bola->vel->y;
		double d = 2*(bola->vel->x*bola->vel->x + bola->vel->y*bola->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 > bola->time)
			return NULL;
		
		// calculate the point of collision
		Vector2D dPos = bola->vel->copy();
		dPos->scale(t);
		Vector2D PoC = bola->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
	//*********************************************************
	CollisionData&  RotatingLine::CheckCollisionSide(Bola *bola)
	{
		// 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*(bola->pos->x*bola->vel->x + bola->pos->y*bola->vel->y - c->x*bola->vel->x - c->y*bola->vel->y), 2) - 4*(bola->vel->x*bola->vel->x + bola->vel->y*bola->vel->y)*(-2*bola->pos->x*c->x - 2*bola->pos->y*c->y + bola->pos->x*bola->pos->x + bola->pos->y*bola->pos->y + c->x*c->x + c->y*c->y - l*l - 2*l*bola->r - bola->r*bola->r);
		
		if (f < 0)
			return NULL;
		
		double a = sqrt(f);
		double b = -2*bola->pos->x*bola->vel->x - 2*bola->pos->y*bola->vel->y + 2*c->x*bola->vel->x + 2*c->y*bola->vel->y;
		double d = 2*(bola->vel->x*bola->vel->x + bola->vel->y*bola->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 > bola->time)
			return NULL;
		
		// calculate the point of collision
		Vector2D PoC = new vector2d(bola->pos->x + bola->vel->x * t, bola->pos->y + bola->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
		Vector2D e = n->copy();
		e->scale(abs(rot) * inertia);

		return new CollisionData(PoC, n, t, damping, friction, e, this);
	}
}

I managed to fix the unrecognized "variables" but now the ff error messages pop up.

error C2232: '->Vector2D::x' : left operand has 'class' type, use '.'
error C2819: type 'Vector2D' does not have an overloaded member 'operator ->'
error C2232: '->Vector2D::copy' : left operand has 'class' type, use '.'

would it help if i post the Java version of this code which I'm trying to translate into C++?
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
It would help if you added the line where the errors pop up.

The C2232 usually happens when you try to write x->y where x is not a pointer.

For example in CheckCollisionSide you're using n wrongly.

n is of type Vector2D and you write:

Code:
n = new vector2d(PoC, c);
n->normalize();

Both these lines of code require it to be of type Vector2D*.


Pointer:
x->y //member-access
Vector2D* x = new Vector2D(a,b,c); //creation of local

Data:
x.y //member access
Vector2D x(a,b,c); //Creation of local

Another problem is that your function signatures don't match:

Code:
//Header
CollisionData *CheckBallInside(Bola *bola);

//Cpp
CollisionData&  RotatingLine::CheckBallInside(Bola *bola)

Header wants a CollisionData* type and Cpp wants a CollisionData& type.

What you return is:

Code:
return new CollisionData(PoC, n, 0.0001, aDamping, friction, e, this);

So you return a CollisionData* type.

Both isn't a good idea.
If you return CollisionData& you will return only a reference to the local copy of the data. But the local copy will be destroyed after the function ended. So in this case you'd return junk (your compiler should warn you about this).
If you return CollisionData* it will work, but you'd dynamically allocate the data on the heap and you'll be responsible for destroying it again. I don't see you do that.
And since you don't do that your code leaks more than a water bucket that has been used for target practice by da Russian army.

You should leave away as many of the fancy * and & as possible:

Code:
CollisionData CheckBallInside(Bola bola); //No pointer to Bola

//Cpp
CollisionData  RotatingLine::CheckBallInside(Bola bola){
    //blah blah
    return CollisionData(PoC, n, 0.0001, aDamping, friction, e, this); //We omit the 'new' so it's not dynamically allocated
}

//In some function:
CollisionData data = CheckBallInside(blah);


And I urge you to use better names for your member variables. 'n', 'c' and 'p' are horrible. Not only is it hard for others (like me! :p) to debug it, but I promise that you'll forget what they're used for in a month's time.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
The errors pop up at lines 16,23,50,103,106 and 105

16,103:
In RotatingLine you try to create a new vector on the heap even though you can't (see prev post)

23,50,105,106:
You treat a normal variable as a pointer.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top