Welcome! Log In Create A New Profile

Advanced

orbiting a point

Posted by g_man 
orbiting a point
February 19, 2010 05:49AM
I've made a class that lets me create a ball, and lets me add acceleration and change speed and acceleration vectors, but now I'm trying to use what I've got to make a ball orbit a center point. I tried, but it just accerates up.
Here is the code for the class:
class bullet{
	public:
	bullet(float,float,float,float,float,float);
	
	float getX() {return X;}
	void setX(float i) {X=i;}
	
	float getY() {return Y;}
	void setY(float i){Y=i;}	
	
	float getSpeed(){return speed;}
	void setSpeed(float i) {speed=i;}
	
	float getAngle() {return angle;}
	void setangle(float i) {angle=i;}	
	
	float getAccel() {return accel;}
	void setAccel(float i) {accel=i;}
	
	float getAccelAngle() {return accelAng;}
	void setAccelAngle(float i) {accelAng=i;}
	
	void setColor(GXColor i) {bulletQuad.SetFillColor(i);}
	
	void addNewSpeedVector(float,float);
	void addNewAccelVector(float,float);
	
	void updatePos();
	
	Quad bulletQuad;
	
	//Private
	private:
	float X;
	float Y;
	float speed;
	float angle;
	float accel;
	float accelAng;
	
};
bullet::bullet(float initX,float initY,float initspeed,float initangle,float initaccel,float initaccelAng){
	X=initX;
	Y=initY;
	speed=initspeed;
	angle=initangle;
	accel=initaccel;
	accelAng=initaccelAng;
	bulletQuad.SetWidth(5);
	bulletQuad.SetHeight(5);
	bulletQuad.SetBorder(false);
	bulletQuad.SetFillColor((GXColor){0xff,0,0,0xff});
}
void bullet::updatePos(){
	float speedX;
	float speedY;
	float accelX;
	float accelY;
	
	accelX = accel*cos(accelAng);
	accelY = accel*sin(accelAng);
	
	speedX = speed*cos(angle);
	speedY = speed*sin(angle);
	speedX+= accelX;
	speedY+= accelY;
	
	speed = sqrt(speedX*speedX+speedY*speedY);
	angle = asin(speedY/speed);
	
	if(speedX<0)
	angle=(M_PI/2+(M_PI/2-angle));
	
	X+=speed*(cos(angle));
	Y+=speed*(sin(angle+M_PI));
	bulletQuad.SetPosition(X,Y);
}
void bullet::addNewSpeedVector(float mps,float angfrom3){
	float X1;
	float Y1;	
	float X2;
	float Y2;	
	
	X1 = speed*cos(angle);
	Y1 = speed*sin(angle);
	X2 = mps*cos(angfrom3);
	Y2 = mps*sin(angfrom3);
	
	X1 += X2;
	Y1 += Y2;
	
	speed = sqrt(X1*X1+Y1*Y1);
	angle = tan(X1/Y1);

}
void bullet::addNewAccelVector(float aps,float angfrom3){
	float X1;
	float Y1;	
	float X2;
	float Y2;	
	
	X1 = accel*cos(accelAng);
	Y1 = accel*sin(accelAng);
	X2 = aps*cos(angfrom3);
	Y2 = aps*sin(angfrom3);
	
	X1 += X2;
	Y1 += Y2;
	
	accel = sqrt(X1*X1+Y1*Y1);
	accelAng = asin(Y1/accel);
	if(X1<0)
	accelAng=(M_PI/2+(M_PI/2-accelAng));

}
and here is the code for determining the value of the next acceleration vector to add:
		temp = M_PI+asin((0-(bullet1.getY()-240))/100);
		
		bullet1.addNewAccelVector(.002,temp);
right now the new vector I'm adding is going twords the center of the circle(the center of the screen) from the ball.
Could my problem be because of a specific ration there has to be between the current speede of the ball and the acceleration of gravity?
Any help is appriciated, thanks.
Re: orbiting a point
February 19, 2010 07:12AM
If you want a ball to orbit around a center point you need to take advantage of the physical quantity of centripetal acceleration to keep the ball continually changing direction so it does not fly off in a tangent.
Re: orbiting a point
February 20, 2010 12:08AM
Thanks, I looked it up and that makes sense. I am currently in a physics class and I guess we are going to get to that next.
Re: orbiting a point
February 20, 2010 08:50PM
I finally had time to test this out, and no dice, the ball just disapears. I know that there is nothing wrong with my class, because I have tested it. here is the code I added:
temp = M_PI+asin((0-(bullet1.getY()-240))/100);
temp2 = (temp2*temp2)/100;
		
bullet1.addNewAccelVector(temp2,temp);
If my understanding of Centripetal Acceleration is corect then I need to add a new vector that is pointing twords the center of the circle with an acceleration of (velocity2)/radius. Thanks

EDIT:
I figured out why it was dissapering, just plug 139 into M_PI+asin((0-(bullet1.getY()-240))/100)
It simplifies to PI+asin(1.01) which is an invalid answer.
I also looked through and fix a few future bugs, and found another problem in the statement above, what if my ball is farther away from the center that 100, so I used a function to find the distance between two points, and my ball is still disapearing.
Here is the code I changed:
temp = M_PI+asin((0-(bullet1.getY()-240))/100);
		if(bullet1.getX()<320){
			temp = M_PI/2+(M_PI/2-temp);
		}	
		
		temp2 = (bullet1.getSpeed()*bullet1.getSpeed())/calcDistance(bullet1.getX(),bullet1.getY(),320,240);
		
		bullet1.addNewAccelVector(temp2,temp);
And here is my calcDistance function:
float inline calcDistance(int x1,int y1,int x2,int y2){
	return (float)sqrt(pow((double)(x2-x1),(double)2)+pow((double)(y2-y1),(double)2));
}
The ball also disapears if the ball goes too far down, but according to the formula, the acceleration should be pointed up.



Edited 1 time(s). Last edit at 02/21/2010 03:08AM by g_man.
Sorry, only registered users may post in this forum.

Click here to login