Have I Destroyed Gravity?

This is a discussion on Have I Destroyed Gravity? within the Game Programming forums, part of the General Programming Boards category; I am getting really weird results in my program the shows Projectiles using Direct_X. Basically when I set the angle ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    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. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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 ?
    Last edited by JaWiB; 12-08-2006 at 10:20 AM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    Quote 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.

  7. #7
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    whoops, I just read your reply and the code. You did have .98 for gravity.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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;
    
        //load the background image
        back = LoadSurface("background.bmp", NULL);
        if (back == NULL)
            return 0;
    
        //load the ball sprite
        ball_image = LoadTexture("ball.bmp", D3DCOLOR_XRGB(255,0,255));
        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;
    
        //load the paddle sprite
        platform = LoadTexture("platform.bmp", D3DCOLOR_XRGB(255,0,255));
        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.

    Quote 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.
    Last edited by bumfluff; 12-08-2006 at 04:14 PM.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    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. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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'

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well it is worse than I thought, it is going pretty much the same distance at 0 degrees. HELP

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lifetime of temporary during exception throw
    By brewbuck in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2009, 04:08 PM
  2. Platform game gravity problem
    By Akkernight in forum Game Programming
    Replies: 33
    Last Post: 02-22-2009, 12:10 PM
  3. Need help with gravity engine
    By madmech in forum Game Programming
    Replies: 11
    Last Post: 07-03-2005, 02:41 PM
  4. gravity effecting velocity
    By Josh Kasten in forum Game Programming
    Replies: 4
    Last Post: 02-08-2003, 08:22 AM
  5. Gravity? We don't need no steenking gravity!
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 03-28-2002, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21