QBasic Gorilas/Worm Physics help needed!

Started by
6 comments, last by kSquared 18 years, 4 months ago
I am doing a program in C++ and OpenGL. My program Is based on the old classic Qbasic Gorilas! This is basically a simple verson of worms! Basically you have two gorilas that throw a banana when the banana hits the gorila loses a life and the envirement changes. there is 10 building and 2 gorilas are randomly placed on the building! I have done this bit! Then the player types in the angle and strength/force they wish to throw the banana (this is all based in a 2d enviroment). Am basically looking for some one to help me with the physics side! So when I type angle 60 and strength 100. Banana goes up arcs and hits or misses gorlia, I can do collistion ditection points and that just strugling on physics! Here is What I have so far: .....double angleDouble=angle; //angle .....if (GotThrowTime1==false){ //gets the following code once ...........timeOfThrow=runTime; //finds how many seconds since banana thrown ...........bananaVelX = cos(angleDouble) * strength; ...........bananaVelY = sin(angleDouble) * strength; ...........if (Player_1_turn==true) { ...............bananaX = CurrentMonkey1CoX; ...............bananaY = CurrentMonkey1CoY; ...........} else { ...............bananaX = CurrentMonkey2CoX; ...............bananaY = CurrentMonkey2CoY; ...........} ...........GotThrowTime1=true;//this will be turned back on after colistion ......} //do each frame ......double SecondsSinceThrow=(timeOfThrow-runTime)/100;//100 slows motion down ......double wind=1,gravity=9.81; ......bananaX += bananaVelX * SecondsSinceThrow; ......bananaY += bananaVelY * SecondsSinceThrow; ......bananaVelX += (wind * SecondsSinceThrow); ......bananaVelY -= (gravity * SecondsSinceThrow); ......if (Player_1_turn==true){ ..........bananaVelX = -bananaVelX; ......} Thanks for your help in advance!
Advertisement
from what i can see i think you're on the right track

here is a link to a tutorial on Projectile Motion. it's about 2D but it may be helpful a bit

http://www.physicsclassroom.com/Class/vectors/U3L2a.html
As far as I see there is a mistake in your code. You are doing an integration of the current velocity to the current position, and you're updating your current velocity due to the current acceleration. That is ok in principle. But during integrating the velocity (as well as at computing the new velocity) you are multiplying it by the duration elapsed since the _start_ of the projectile simulation, instead of the duration elapsed since the last recent integration step.

As formulas, you compute
Sum v(n*dt) * (t-t0) + s0
instead of
Sum v(n*dt) * dt + s0 =: s(n*dt)
where
dt := t(n) - t(n-1)

So I'm missing a line like
timeOfThrow=runTime;
somewhere at the end of the block of
//do each frame
(ok, the names would no longer be sufficient, but the principle counts here).
So that the next run of the loop will compute an according difference as
SecondsSinceThrow
(what would become a SecondsSinceLastUpdate in fact).


BTW: Please use the [ source ] and [ /source ] tags (without the inner spaces) around longer (say more than 5 lines) source snippets: Besides other, it makes a layout that preservers psaces and hence indentation.

EDIT: Formulas added to enhance clarity, I hope.
ok this is what I have now!

         double angleDouble=angle;	if (GotThrowTime1==false){				bananaVelX = (cos(angleDouble) * strength);		bananaVelY = (sin(angleDouble) * strength);		if (Player_1_turn==true) {			bananaX = CurrentMonkey1CoX;			bananaY = CurrentMonkey1CoY;		} else {			bananaX = CurrentMonkey2CoX;			bananaY = CurrentMonkey2CoY;		}		if (Player_2_turn==true){			bananaVelX = -bananaVelX;		}		GotThrowTime1=true;	}	double wind=0,gravity=9.81;	bananaX += bananaVelX * timeSinceLastUpdate;	bananaY += bananaVelY * timeSinceLastUpdate;	bananaVelX += (wind) * timeSinceLastUpdate;	bananaVelY -= (gravity * timeSinceLastUpdate);		glBegin(GL_POLYGON);	glColor3d(1,1,0);	glVertex2f(bananaX,bananaY);	glVertex2f(bananaX,bananaY-5);	glVertex2f(bananaX-5,bananaY-5);	glVertex2f(bananaX-5,bananaY);	glEnd();	timeSinceLastUpdate=runTime/100;


Whats wrong with it? it's ok when I put in 5, 20, 40, 45, 60, 80.

But numbers like 10,30,50,70 make it go the wrong way am completely confused on what the hell is going on?
I didn't have much time to read your code but remember that the trig functions use radians not degrees.

Convert Player Angle to Radians. Use new angle.
From the posted snippet it is not visible whether the time computation is done right now. However, since you stated that some angles (you meant the angles that are "put in", don't you?) work while others don't, I assume Metorical is totally right: The actually used angle is the overhanded angle modulo 2pi if overhanding angles in degree.

Please give
double angleDouble = angle/360.0f*6.283185f;
a try, if you want still overhand angles in degree (e.g. due to your input system).
OMG QBASIC GORILLAS!!!!! [grin] I have nothing of value to add to this thread.
Please don't double-post. In the future, if you mistakenly post a thread twice, you can delete it. First, edit your original post in the thread you'd like to delete, then click the "Delete? To delete this post, check this box" checkbox. Then click "make modifications", and your thread disappears if you've made the changes within a certain time period of your original post.

I'm closing this thread since your other one has more replies.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}

This topic is closed to new replies.

Advertisement