Jump to content
  • Advertisement
Sign in to follow this  
TransformedBG

How to arc an object across the screen?

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

So I have been building a "world" out of triangle, Layering objects upon one another and learning a little bit about how to do things.


Basically what i am trying to do is build a sun that will arch 180 degrees across any given size screen and stop just outside of view. I have got the sun drawn and moving however i cant get my arc right. I know this has something to do with geometry but i havent figured it out yet.. (Last time i took it was 1998 lol)

so far what i have is:
//screen variables
const int Width = (GetSystemMetrics(SM_CXSCREEN)) - 10;
const int Height = (GetSystemMetrics(SM_CYSCREEN)) - 50;

//Build device pointer
IDirect3DDevice9* Device = 0;
IDirect3DVertexBuffer9* Triangle = 0; // vertex buffer to store

//Build a matrix to transform w/object
D3DXMATRIX World, sunRotation;
double sunXYcord = 0.0f;
float sunX = -4.5f, sunY = 0.0f; // start point for x
int choice = 0;

//colors
int r,g,b;
int roofR,roofG,roofB;

//sky
int skyR = 51, skyG =189 ,skyB = 255;
float rShift = 0.0f, gShift = 0.0f, bShift = 0.0f;


struct colorVertex
{
colorVertex(){}
colorVertex(float x, float y, float z, D3DCOLOR c)
{
_x = x;
_y = y;
_z = z,
_color = c;
}

float _x, _y, _z;
D3DCOLOR _color;

static const DWORD FVF;
};

const DWORD colorVertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;

void fillVertices(int vertCord,float x, float y, float z, int r, int g, int b)
{
colorVertex* vertices;
Triangle->Lock(0, 0, (void**)&vertices, 0);
vertices[vertCord] = colorVertex(x, y, z, D3DCOLOR_XRGB(r,g,b));
vertices[vertCord] = colorVertex(x, y, z, D3DCOLOR_XRGB(r,g,b));
vertices[vertCord] = colorVertex(x, y, z, D3DCOLOR_XRGB(r,g,b));
Triangle->Unlock();
}

bool Setup()
{
// Create the vertex buffer.
Device->CreateVertexBuffer(
3 * sizeof(colorVertex), // size in bytes
D3DUSAGE_WRITEONLY, // flags
colorVertex::FVF, // vertex format
D3DPOOL_MANAGED, // managed memory pool
&Triangle, // return create vertex buffer
0); // not used - set to 0

// Set the projection matrix.
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj, // result
D3DX_PI * 0.5f, // 90 - degrees
(float)Width / (float)Height, // aspect ratio
1.0f, // near plane
1000.0f); // far plane

Device->SetTransform(D3DTS_PROJECTION, &proj);

// Set wireframe mode render state.
Device->SetRenderState(D3DRS_LIGHTING, false); //allows us to see object
return true;
}

void drawSun(float x, float y)
{

r=255,g=255,b=0; //red+green = yellow

//make 1 triangle top //red
fillVertices(0, -0.5f, -0.5f, 1.0f,r,g,b);
fillVertices(1, 0.0f, 0.5f, 1.0f,r,g,b);
fillVertices(2, 0.5f, -0.5f, 1.0f,r,g,b);

D3DXMatrixTranslation(&World, x, y, 2.0f); //triangle, x,y, camara view(z)
Device->SetTransform(D3DTS_WORLD, &World); //transform
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT); //GOURAUD
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

//make 2 triangle bottom // yellow
fillVertices(0, 0.0f, -0.5f, 1.0f,r,g,b);
fillVertices(1, -0.5f, 0.5f, 1.0f,r,g,b);
fillVertices(2, 0.5f, 0.5f, 1.0f,r,g,b);

D3DXMatrixTranslation(&World, x, y-0.3f, 2.0f); //triangle, x,y, camara view(z)
Device->SetTransform(D3DTS_WORLD, &World);
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
}

void moveObjectLeft(float x, float y)
{
D3DXMatrixRotationX(&sunRotation, D3DXToRadian(x));
D3DXMatrixRotationY(&sunRotation, D3DXToRadian(y));
Device->SetTransform(D3DTS_WORLD,&sunRotation);

}

void moveObjectRight(float x, float y)
{
D3DXMatrixRotationX(&sunRotation, D3DXToRadian(sunX));
D3DXMatrixRotationY(&sunRotation, D3DXToRadian(sunY));
Device->SetTransform(D3DTS_WORLD,&sunRotation);

}

void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(Triangle);
}

void render(float timeDelta)
{
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); //clear screen



Device->BeginScene();
{
drawGrass();
drawSky();
drawHouse();
}
Device->EndScene();


Device->BeginScene();
Device->SetStreamSource(0, Triangle, 0, sizeof(colorVertex));
Device->SetFVF(colorVertex::FVF);
drawSun(sunX, sunY);

Device->EndScene();
}

bool Display(float timeDelta)
{
if( Device )
{
render(timeDelta);
Device->Present(0, 0, 0, 0);
}
return true;
}

LRESULT CALLBACK d3d::WinProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
::PostQuitMessage(0);
break;

case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd);

if(wParam == VK_RIGHT)
{
sunXYcord += 4.5;

if(sunXYcord < Width/2) //if circle != 90 do stuff else stop
{
sunX += 0.03f; sunY += 0.02f;
moveObjectLeft(sunX,sunY);
}

if(sunXYcord > Width/2-1)
{
sunX += 0.03f; sunY -= 0.02f;
moveObjectLeft(sunX,sunY);
}


}

if(wParam == VK_LEFT)
{
sunXYcord -= 4.5;

if(sunXYcord < Width/2) //if circle != 90 do stuff else stop
{
sunX -= 0.03f; sunY -= 0.02f;
moveObjectLeft(sunX,sunY);
}

if(sunXYcord > Width/2-1)
{
sunX -= 0.03f; sunY += 0.02f;
moveObjectLeft(sunX,sunY);
}
}

/*if(wParam == VK_0x047) //G
{
choice = 1;
}

if(wParam == VK_0x048) //F
{
choice = 0;
}*/
break;
}

return ::DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE prevInstance,
PSTR cmdLine,int showCmd)
{
if(!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}

d3d::EnterMsgLoop( Display );
Cleanup();
Device->Release();
return 0;
}



Just wondering if maybe someone out there could help me out a little and see what they could come up with.

Share this post


Link to post
Share on other sites
Advertisement
It sounds like you just want the sun to rotate around the center of your world?

This is very easy. All you have to do is first translate (move) the sun to the distance you want from the center of the world. Then you do a rotation, which will rotate the sun around the point (0,0,0) in world space.

You would just update the angle of rotation every frame (you might want to use a timer to keep a smooth animation), and stop updating the angle after the sun gets outside the screen.

I think this is what your asking right?

Share this post


Link to post
Share on other sites
its not really peoples screen size, just the aspect ratio of the client window you made with your program that matters, so you will have to do some extra calculations if thats what you want,but better yet, just have the sun rotate until it's outside of view no matter the client window size. like instead of just 180 degrees, make it 270 so that its under the ground

Share this post


Link to post
Share on other sites

It sounds like you just want the sun to rotate around the center of your world?

This is very easy. All you have to do is first translate (move) the sun to the distance you want from the center of the world. Then you do a rotation, which will rotate the sun around the point (0,0,0) in world space.

You would just update the angle of rotation every frame (you might want to use a timer to keep a smooth animation), and stop updating the angle after the sun gets outside the screen.

I think this is what your asking right?




Yeah that's pretty much what I am asking...

problem I am having is figuring out measurements...

I know my (0,0,0) cord would be (.5*width, .5*height, Zvar),

how do you go from


[color=#000088]const[color=#000000] [color=#000088]int[color=#000000] [color=#660066]Width[color=#000000] [color=#666600]=[color=#000000] [color=#666600]([color=#660066]GetSystemMetrics[color=#666600]([color=#000000]SM_CXSCREEN[color=#666600]))[color=#000000] [color=#666600]-[color=#000000] [color=#006666]10[color=#666600];[color=#000000]
[color=#000088]const[color=#000000] [color=#000088]int[color=#000000] [color=#660066]Height[color=#000000] [color=#666600]=[color=#000000] [color=#666600]([color=#660066]GetSystemMetrics[color=#666600]([color=#000000]SM_CYSCREEN[color=#666600]))[color=#000000] [color=#666600]-[color=#000000] [color=#006666]50[color=#666600];

to figure out where the edges are?

Share this post


Link to post
Share on other sites
So your camera is not moving I assume, since your able to compare a world coordinate with a screen coordinate.

Otherwise, if you want the sun to move relative to the camera, you will just have to do that transformation (translation then rotation) first, and then do another translation to the position of your camera. but doing this will have the sun always revolve around the camera instead of the world, which at first i thought would be weird, but I gave it a second thought and thats actually a good idea to do that, so that the sun never gets any closer to the camera, since it would look pretty unrealistic if the sun actually got closer to you while you walked towards it.

So, better than worrying about the screen space, like pixels and all that, It would be better to check if its inside or outside the "frustum", which is basically a cone shaped box of everything the camera can see. This shouldn't be terribly difficult, since you should already have the field of view angle, and after the sun has rotated that field of view angle, it should be out of sight (Or nearly out of sight, you'll probably have to play with the angle values a bit to get exactly what you want), and then you could stop rotating it.

I thought about it again, and to get an arc shape across the screen, it might be helpful to do two sets of transformations, the one I mentioned being SECOND. the first transformation would be a translation of a certain distance, and the rotation being the one that will give the sun an appearance of an arc across the screen (both depending on the arc you are looking for), then do the second transformation like i mentioned above

Share this post


Link to post
Share on other sites

So your camera is not moving I assume, since your able to compare a world coordinate with a screen coordinate.

Otherwise, if you want the sun to move relative to the camera, you will just have to do that transformation (translation then rotation) first, and then do another translation to the position of your camera. but doing this will have the sun always revolve around the camera instead of the world, which at first i thought would be weird, but I gave it a second thought and thats actually a good idea to do that, so that the sun never gets any closer to the camera, since it would look pretty unrealistic if the sun actually got closer to you while you walked towards it.

So, better than worrying about the screen space, like pixels and all that, It would be better to check if its inside or outside the "frustum", which is basically a cone shaped box of everything the camera can see. This shouldn't be terribly difficult, since you should already have the field of view angle, and after the sun has rotated that field of view angle, it should be out of sight (Or nearly out of sight, you'll probably have to play with the angle values a bit to get exactly what you want), and then you could stop rotating it.

I thought about it again, and to get an arc shape across the screen, it might be helpful to do two sets of transformations, the one I mentioned being SECOND. the first transformation would be a translation of a certain distance, and the rotation being the one that will give the sun an appearance of an arc across the screen (both depending on the arc you are looking for), then do the second transformation like i mentioned above


alright ill try to give that a go and see what happens.. fairly new to all this so Iv been learning as i go...


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!