Jump to content
  • Advertisement
Sign in to follow this  

Want to move view cam up making triangle move[Updated]

This topic is 3771 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

Hi , I have been doing a guide and Im going to systematically work with each camera until I understand them. One of the exercise with no code was to use the tutorial code and make the spinning triangle shoot up. Anyway I had a variation on that , which was I work on the transitions 1 at a time to aid understanding. So I have it so the view cam goes from 0 up the Y axis , making the triangle like its sinking down the Y axis and off the screen. I have had the original working fine, I even have the triangle acting as expected, well to a degree - I very slight degree but on the positive side it made me realise well the original program main loop was running to fast in respect that the triangle shot of the screen by the time the screen displayed, thats what I THINK anyway as I changed the frame pause from <25 to <250 The thing is although my submission is alot of code that will seriously show better what I have and hopefully what Im aiming for better than the above description.
// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

// define the screen resolution and keyboard macros
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

// global declarations
LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL;    // the pointer to the vertex buffer
float moved = 1.0f;
float cview = 0.0f;
float tb =0.0f;
static float index = 0.0f;
// function prototypes
void initD3D(HWND hWnd);    // sets up and initializes Direct3D
void render_frame(void);    // renders a single frame
void cleanD3D(void);    // closes Direct3D and releases memory
void init_graphics(void);    // 3D declarations
void set_aview(D3DXMATRIX);

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
    HWND hWnd;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = L"WindowClass";


    hWnd = CreateWindowEx(NULL, L"WindowClass", L"Our Direct3D Program",
                          WS_EX_TOPMOST | WS_POPUP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL, NULL, hInstance, NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D

    // enter the main loop:

    MSG msg;

        DWORD starting_point = GetTickCount();

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            if (msg.message == WM_QUIT)



            PostMessage(hWnd, WM_DESTROY, 0, 0);

		while ((GetTickCount() - starting_point) < 45);
		 // check the 'escape' key


		//possibly bad , a fixed triangle rotates if this < 25 but that is too fast to see an triangle appearing to move down , slow things down and no rotate viewable
        //current setups bad but I have the effect of the triangle moving down , well its actually the cam moving up but I want to do this but make it
		//smooth like an animation . I realise this is not the way to do things but Im experimenting with all the stages to help understanding and would like 
		//an animation of a sort where the camera moves smoothly, currently I either see the movement , to fast to see OR 
		//to slow to the point the exit escape key takes ages and when its slow it doesnt appear to have rotations
		//I realise also that during the rotation we see a black side to a texture, this is like this from the default tutorial code
		//and Im sure he will remedy in next lesson.

    // clean up DirectX and COM

    return msg.wParam;

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
        case WM_DESTROY:
                return 0;
            } break;

    return DefWindowProc (hWnd, message, wParam, lParam);

// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
    d3d = Direct3DCreate9(D3D_SDK_VERSION);


    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;

    // create a device class using this information and the info from the d3dpp stuct

    init_graphics();    // call the function to initialize the triangle

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting


// this is the function used to render a single frame
void render_frame(void)
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);


    // select which vertex format we are using


    D3DXMATRIX matRotateY;    // a matrix to store the rotation information

     index+=0.05f;    // an ever-increasing float value

    // build a matrix to rotate the model based on the increasing float value
    D3DXMatrixRotationY(&matRotateY, index);

    // tell Direct3D about our matrix
    d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);

    D3DXMATRIX matView;    // the view transform matrix
                       &D3DXVECTOR3 (0.0f, 0.0f, 10.0f),    // the camera position
                       &D3DXVECTOR3 (0.0f, cview+=0.05f, 0.0f),    // the look-at position //change cview var for 0.0f for original code
                       &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction

	d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView*/
/*    D3DXMatrixLookAtLH(&matView,
                       &D3DXVECTOR3 (0.0f, 0.0f, 10.0f),    // the camera position
                       &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),    // the look-at position
                       &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction

	d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView*/

    D3DXMATRIX matProjection;     // the projection transform matrix

                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
                               1.0f,    // the near view-plane
                               100.0f);    // the far view-plane

    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);    // set the projection

    // select the vertex buffer to display
    d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));

    // copy the vertex buffer to the back buffer
    d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);


    d3ddev->Present(NULL, NULL, NULL, NULL);


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
    t_buffer->Release();    // close and release the vertex buffer
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D


// this is the function that puts the 3D models into video RAM
void init_graphics(void)
    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX t_vert[] = 
        { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 0.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },

    // create a vertex buffer interface called t_buffer

    VOID* pVoid;    // a void pointer

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, t_vert, sizeof(t_vert));


void set_aview(D3DXMATRIX aView)
//float moved = 0.1f;
//float cview = 0.0f;


Now does what I expected visually + no lockup but is still wrong :) [going to try the submitted code later thanks! I hate being new lol] [Edited by - firefly28 on February 25, 2008 11:31:12 AM]

Share this post

Link to post
Share on other sites
Well still not sure if I have the correct idea here but I got it working as I wanted visually. I ditched the really badly done if condition in render frame and on the view cam Y axis did cview=cview+0.05f

cview on the first iteration was 0.0f and +0.05f thereafter, the 0.05f figure was the Y axis rotation movement so since that run smoothly I used that for the view cam Y axis movement. I'm going to have to get to grips with doing calculations based on time and fps again. Being a newbie to game programming thats what stung me bad when I did my pong game (My first attempt at a game seriously)


Although I got it working , Im not to happy with the menthod I used . I was did some searching on gamdev and came up with the following link that may be off some use to people experiancing animations running to fast or slow :

Animations and Time

Im still pleased that Im managing the exercises to some degree and I will try some of the methods discussed in the linked topic tommorow :)

[Edited by - firefly28 on February 21, 2008 6:09:10 PM]

Share this post

Link to post
Share on other sites
This might seem confusing if you're not familiar with trigonomtry, but you don't want to rotate your camera by changing the y-value of your LookAt point. That's because the linear changes in the y-value of the LookAt point doesn't result in linear changes in the amount that the camera is rotated. Mathematically, the resulting angle you'd get is equal to tan( LookAt.y / LookAt.Z ). This means that at a y-value of 0 you get an angle of 0 degrees, at a y-value of 1 you get an angle of 45 degrees, andat a y-value of 10,000 you get about 90 degrees. See what I'm talking about?

What you want to instead is directly manipulate your angle value for your camera. Try something like this instead:

// Start the lookAt point right in front of the camera
D3DXVECTOR3 vLookAt ( 0, 0, 1 );

// Have a constant speed at which the camera will rotate
const FLOAT fCameraAngularVelocity = 1.0f;

// Change the rotation based on the amount of time that has
// elpased since last frame and the rotation speed
fCameraAngleX += fCameraAngularVelocity * fDT;

// Create a rotation matrix that rotates about the x-axis
// (rotating about the x-axis makes the camera pan up and down)
D3DXMATRIXA16 matRotationX;
D3DXMatrixRotationX( &matRotationX, fCameraAngleX );

// Rotate the lookAt point
D3DXVec3TransformCoord( &vLookAt, &vLookAt, &matRotationX );

// Make your view matrix the usual way

Also, in the future please use the "source" tags when posting long bits of code. If you need to see how, edit and my post and see how I did it.

Share this post

Link to post
Share on other sites
Scratch alot of what I was saying :) I think I see what your getting at now but I was altering the Y value of the camera position , thats what I was doing although at the time I may have submitted botched code where I had altered the camera look at point. What I essentially did was kept the lookat point the same so the camera stayed fixed on the same point and physically moved the camera down while it maintained the same lookat point. (Probably bad practice :) but was just to help understanding , I think I see what you are saying about rotation now ......so what does the unit increments on the lookat point represent , is it radians? Also the division you do that would give the current point of rotation each frame ? [is this correct]

Im messing with actual camera panning now , so some of it is gradually making more sense.

[Edited by - firefly28 on February 25, 2008 11:51:18 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!