• Advertisement

QBasic Gorilas/Worm Physics help needed!

This topic is 4440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post

Share on other sites
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

Share this post

Share on other sites
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.

Share this post

Share on other sites
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?

Share this post

Share on other sites
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.

Share this post

Share on other sites
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).

Share this post

Share on other sites
OMG QBASIC GORILLAS!!!!! [grin] I have nothing of value to add to this thread.

Share this post

Share on other sites
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.

Share this post

Share on other sites

This topic is 4440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

This topic is now closed to further replies.

• Advertisement
• Advertisement
• Popular Tags

• Advertisement
• Popular Now

• 10
• 11
• 9
• 16
• 19
• Advertisement