# Thread: Have I Destroyed Gravity?

1. ## Have I Destroyed Gravity?

I am getting really weird results in my program the shows Projectiles using Direct_X. Basically when I set the angle to 37 degrees, the ball goes further than when I set the angle to 45 degrees, yet it should go furtherest with 45.

This is how I am working it out:

Code:
```ball.movex = 50 * cos(angle*3.14159265/180);
ball.movey =  50 * sin(angle*3.14159265/180);

ball.movex *= 0.1;
ball.movey *= -0.1;

//then in the game loop

ball.x += ball.movex;
ball.y += ball.movey;

if (keep going...)
ball.movey -= 0.98;```
I don't know what causes this error.

2. Why don't you use a constant for PI?

What's with the *= -0.1? What's with the *= 0.1, for that matter? Why not adjust the calculation above instead?

But aside from these stylistic issues, I can't find anything fundamentally wrong. Can you output the movex and movey values to confirm that they're initially equal?

3. What would really make a lot more sense is if you had your ball have various characteristics like displacement, velocity, and acceleration. Then, every draw routine, you translate it by the displacement vector, and then when you update, you make

velocity = displacement * delta_time
displaceement = velocity * delta_time

4. HMMM, maybe I haven't made what I am doing clear enough. Basically, I will have a ball which you set the angle for and then it works out both the horizontal and vertiacal components of the ball's velocity (movex and movery). I gave the resultant velocity a value of 50 and so calculated the different components. However, these gave too great a movement to show off the motion so I changed it to be 1th of the calculation and the movey has to be negative so that it moves in the right direction at first. Then every loop I take 0.098 off the movey for the effect of gravity. I just don't see what is going wrong in my calculation.

5. It makes sense to make movey negative, but why are you subtracting from it every frame? Wouldn't that make it accelerate upward on the screen?

Also, as Tonto said, it would make more sense to use names like "velocity" or even just "v" and possibly define a constant for gravity.

Edit: Tonto, shouldn't your first equation be velocity = acceleration * delta_time ?

6. Originally Posted by bumfluff
Then every loop I take 0.098 off the movey for the effect of gravity. I just don't see what is going wrong in my calculation.
I thought the calculation for gravity is .98 not .098, I doubt that this will help you though. I'll take a further look into your code to see if I can find any other physic's errors.

8. It doesnt really matter what gravity is as 45 should always go the furthest. It must be to do with the way I am calculating the the different components of movement.

9. Oh yeah what was I thinking

velocity += acceleration * delta_time
displacement += velocity * delta_time

Anyways, more complete code may be helpful if you still can't identify the problem.

10. Code:
```//background image
LPDIRECT3DSURFACE9 back;

//sprite handler
LPD3DXSPRITE sprite_handler;

//ball sprite
LPDIRECT3DTEXTURE9 ball_image;
SPRITE ball;

//platform
LPDIRECT3DTEXTURE9 platform;

//misc
long start = GetTickCount();
HRESULT result;
int angle = 45;

//initializes the game
int Game_Init(HWND hwnd)
{
//set random number seed
srand((int)time(NULL));

//initialize mouse
if (!Init_Mouse(hwnd))
{
MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
return 0;
}

//initialize keyboard
if (!Init_Keyboard(hwnd))
{
MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
return 0;
}

//create sprite handler object
result = D3DXCreateSprite(d3ddev, &sprite_handler);
if (result != D3D_OK)
return 0;

if (back == NULL)
return 0;

if (ball_image == NULL)
return 0;

//set the ball's properties
ball.x = 50;
ball.y = 175;
ball.width = 12;
ball.height = 12;
ball.movex = 50 * cos((angle*3.14159265)/180);
ball.movey =  50 * sin((angle*3.14159265)/180);
ball.movey *= -0.1;
ball.movex *= 0.1;

if (platform == NULL)
return 0;

//return okay
return 1;
}

//the main game loop
void Game_Run(HWND hwnd)
{
//ball position vector
D3DXVECTOR3 position(0,0,0);

//make sure the Direct3D device is valid
if (d3ddev == NULL)
return;

//update mouse and keyboard
Poll_Mouse();
Poll_Keyboard();

//after short delay, ready for next frame?
//this keeps the game running at a steady frame rate
if (GetTickCount() - start >= 30)
{
//reset timing
start = GetTickCount();

//move the ball sprite
ball.x += ball.movex;
ball.y += ball.movey;

if (ball.y > 350)
{
ball.movey = 0;
ball.movex = 0;
}
else
{
ball.movey += 0.098;
}
}

//start rendering
if (d3ddev->BeginScene())
{
//erase the entire background
d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);

//start sprite handler
sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);

//draw the platform
position.x = 50;
position.y = 200;
sprite_handler->Draw(
platform,
NULL,
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));

//draw the ball
position.x = (double)ball.x;
position.y = (double)ball.y;
sprite_handler->Draw(
ball_image,
NULL,
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));

//stop drawing
sprite_handler->End();

//stop rendering
d3ddev->EndScene();
}

//display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);

//check for mouse button (to exit program)
if (Mouse_Button(0))
PostMessage(hwnd, WM_DESTROY, 0, 0);

//check for escape key (to exit program)
if (Key_Down(DIK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);

}```
That is all the code that is relevant.

Originally Posted by JaWiB
It makes sense to make movey negative, but why are you subtracting from it every frame? Wouldn't that make it accelerate upward on the screen?

Also, as Tonto said, it would make more sense to use names like "velocity" or even just "v" and possibly define a constant for gravity.

Edit: Tonto, shouldn't your first equation be velocity = acceleration * delta_time ?
Well, it is designed so it starts going upwards as at 45 degrees it will go up and then start to fall. And it works so I have no quams about how I am adding in the effect of gravity.

11. What would really make a lot more sense is if you had your ball have various characteristics like displacement, velocity, and acceleration. Then, every draw routine, you translate it by the displacement vector, and then when you update, you make

velocity = displacement * delta_time
displaceement = velocity * delta_time
That's translational motion, he's doing rotational motion. He's got a some what simplified version, but it's still quite good.

Probably the best description of motion is the combination of translational and rotational (Or simple harmonic, if you only apply it to one axis):

x = x_initial + velocity_x*time + .5*acceleration_x*time^2 + radius*cos(theta_initial + angular_velocity*time + .5*angular_acceleration*time^2)

and

y = y_initial + velocity_y*time + .5*acceleration_y*time^2 + radius*sin(theta_initial + angular_velocity*time + .5*angular_acceleration*time^2)

bum --As for gravity... You don't even have it in your algorithm, I don't know why you'd ask if you've destroyed what you haven't made.

As for the error, I'm not sure what this does:

Code:
```ball.movex *= 0.1;
ball.movey *= -0.1;```
But I think it has potential to be what's causing problems.

12. 1) You prepend 'angular' to what I was saying, you get angular_displacement, angular_velocity, angular_acceleration. Same thing.

2) He's not rotating anything. He's sort of aiming a cannon.

>> x = x_initial + velocity_x*time + .5*acceleration_x*time^2 + radius*cos(theta_initial + angular_velocity*time + .5*angular_acceleration*time^2)

x = xo + vo * t + 1/2 * a * t^2
y = yo + yo * t + 1/2 * a * t^2

Valid physics equation. But I don't know where you're going after that. Just trying to even pretend, that would be sort of like getting the far end of some stick that's spinning around an axis moving through the air.

>> bum --As for gravity... You don't even have it in your algorithm, I don't know why you'd ask if you've destroyed what you haven't made.

'Gravity': ball.movey += 0.098;

And relatedly the

ball.movex *= 0.1;
ball.movey *= -0.1;

Pulls flips the y and shoots it downwards, gravity pulls it towards the 'cieling'

13. I dont see how the
Code:
```ball.movex *= 0.1;
ball.movey *= -0.1;```
Is causing the problems as it keeps the speeds proportional to one another.

Basically the equation that I used is the one we have been taught in Physics.
Code:
```vertical_velocity = resultant_velocity * sin angle
and
horizontal_velocity = resultant_velocity * cos angle```

14. Well it is worse than I thought, it is going pretty much the same distance at 0 degrees. HELP

15. OK, I think this is the point where you upload a ZIP of the complete source code and resources so that we can exeriment with the program.