Path of a ball
im looking for an equation to calculate the path of a ball when kicked, taking into account the angle, the power the ball was kicked, and gravity.
this is on a 2D basis, and i want it to return the x and y co-ords
Thanks.
Well, I think I could help you out. I thought about it and I could either give you a complicated physics lesson on ALL the stuff you have to use and prepare or I could just tell you to cheat.
Let's go with the cheat! This is for real time after all right?
setup velocity and acceleration variables. Always add accel to velocity (even if it's negative). On each pass of the main loop, also make sure to add velocity to the ball (even if it's zero). This way you can use cosine and sine to find the initial ACCELERATION for both x and y respectively.
As of now, the ball will go into space, the final frontier!
We need to add a force. You can add a gravity (variable, since you are god) that acts on it's y component. This should be constant. When playing with the values, make sure your initial acceleration is more than the gravity force in order to get that "arc" trajectory.
When the ball hits the ground... you know what to do... but to make it even fancier, you can reflect y then reduce x and y by a little to make the "bounce" effect"... you can repeat this till both values are zero.
I hope this helped! I know it turned into a whole algorithm but if you have any problems with it, please let me know. I will bookmark this post.
-Lewis [m80]
Play QUADz MX @
www.m80produxions.com
[edited by - LewieM80 on December 23, 2002 5:01:31 AM]
Let's go with the cheat! This is for real time after all right?
setup velocity and acceleration variables. Always add accel to velocity (even if it's negative). On each pass of the main loop, also make sure to add velocity to the ball (even if it's zero). This way you can use cosine and sine to find the initial ACCELERATION for both x and y respectively.
As of now, the ball will go into space, the final frontier!
We need to add a force. You can add a gravity (variable, since you are god) that acts on it's y component. This should be constant. When playing with the values, make sure your initial acceleration is more than the gravity force in order to get that "arc" trajectory.
When the ball hits the ground... you know what to do... but to make it even fancier, you can reflect y then reduce x and y by a little to make the "bounce" effect"... you can repeat this till both values are zero.
I hope this helped! I know it turned into a whole algorithm but if you have any problems with it, please let me know. I will bookmark this post.
-Lewis [m80]
Play QUADz MX @
www.m80produxions.com
[edited by - LewieM80 on December 23, 2002 5:01:31 AM]
Lewis [m80]Interactive Designerhttp://ismstudios.com
atm, i''m using:
x=initx+(power*t*cos(angle));
y=inity+(power*t*sin(angle)+g*(t*t));
but for some reason, it doesn''t work properly...
Full code:
x=initx+(power*t*cos(angle));
y=inity+(power*t*sin(angle)+g*(t*t));
but for some reason, it doesn''t work properly...
Full code:
#include <SDL/sdl.h>#include <windows.h>#include <iostream.h>#include <MATH.h>void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) { Uint32 color = SDL_MapRGB(screen->format, R, G, B); switch (screen->format->BytesPerPixel) { case 1: { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *bufp = color; } break; case 2: { Uint16 *bufp; bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color; } break; case 3: { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { bufp[0] = color; bufp[1] = color >> 8; bufp[2] = color >> 16; } else { bufp[2] = color; bufp[1] = color >> 8; bufp[0] = color >> 16; } } break; case 4: { Uint32 *bufp; bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; *bufp = color; } break; }}void Slock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { if ( SDL_LockSurface(screen) < 0 ) { return; } }}void Sulock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { SDL_UnlockSurface(screen); }}void GenerateLandscape();void DrawLandscape(SDL_Surface *screen);int* Landscape = new int [640*480];SDL_Surface *screen;void FireWeapon(int power, int angle) { int x=0, y=0, g=1, t=0; int inity=240; int initx=320; int timer=GetTickCount(); int delay=5; int timenow; while(Landscape[y*640+x]==1) { timenow=GetTickCount(); if(timenow>=(timer+delay)){ x=initx+(power*t*cos(angle)); y=inity+(power*t*sin(angle)+g*(t*t)); t++; DrawPixel(screen,x,y,255,255,255); SDL_Flip(screen); timer=GetTickCount(); } } for(int i=0;i<11;i++){ int z=11; for(x=11;x>0;x--) { Landscape[(y+i)*640+(320-z)]=1; z--; } } DrawLandscape(screen);} int main(int argc, char *argv[]) { SDL_Init(SDL_INIT_VIDEO); atexit(SDL_Quit); screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); bool done=false; GenerateLandscape(); DrawLandscape(screen); while(!done) { SDL_Event event; while (SDL_PollEvent(&event)) { if ( event.type == SDL_QUIT ) { done=true; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done=true; } if ( event.key.keysym.sym == SDLK_SPACE ) { FireWeapon(10,90); } } } } delete [] Landscape; return 0;}void GenerateLandscape() { int height=0; int old1=299; int old2=300; int x; int y; int diff; for(x=0;x<640;x++){ for(y=0;y<480;y++) { Landscape[y*640+x]=0; } } srand(GetTickCount()); for(x=0;x<640;x++) { if(old2 > old1){ diff=old2-old1; height=old1-(rand()%(diff+2)); old2=old1-1; old1=height; } else { diff=old1-old2; height=old1+(rand()%(diff+2)); old2=old1+1; old1=height; } for(y=0;y<height;y++) { Landscape[y*640+x]=1; } }}void DrawLandscape(SDL_Surface *screen) { int x; int y; Slock(screen); for(x=0;x<640;x++){ for(y=0;y<480;y++) { if(Landscape[y*640+x]==1){ DrawPixel(screen,x,y,0,0,255-(y/2)); } else { DrawPixel(screen,x,y,0,200-(y/4),0); } } } Sulock(screen); SDL_Flip(screen);}
I have created a demo for this post. Email me if you would like a copy.
-Lewie [m80]
-Lewie [m80]
Lewis [m80]Interactive Designerhttp://ismstudios.com
void FireWeapon(int power, int angle) { double x=0, y=0, t=0, a=angle*(PI/180), ya=4; int inity=100; int initx=320; int timer=GetTickCount(); int delay=2; int timenow; while(t<10) { timenow=GetTickCount(); if(timenow>=(timer+delay)){ x = (power*sin(a)*t)+initx; y = (-power*cos(a)*t)+(ya/2)*(t*t)+inity; t++; DrawPixel(screen,x,y,255,255,255); SDL_Flip(screen); timer=GetTickCount(); } } DrawLandscape(screen);}
y = (-power*cos(a)*t)+(ya/2)*(t*t)+inity;
That piece of code makes me nervous.
I hope you understand what it does
6 months from now????
aaaaaaaaaaahhhhhhhhhhhhhhhhhhhh!!!
-Lewie [m80]
That piece of code makes me nervous.
I hope you understand what it does
6 months from now????
aaaaaaaaaaahhhhhhhhhhhhhhhhhhhh!!!
-Lewie [m80]
Lewis [m80]Interactive Designerhttp://ismstudios.com
i had 3 guys to help me, im bad at stuff like this, and they dont have a clue about programming. this was the best we could ccome up with.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement