410 likes | 545 Views
Animation 1. In animation. The code needs to cut and show different parts of the picture below. actorvertexbuffer = new VertexBuffer ( typeof ( CustomVertex.PositionTextured ), 4 , device, 0, CustomVertex.PositionTextured.Format , Pool.Default );.
E N D
The code needs to cut and show different parts of the picture below
actorvertexbuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, device, 0, CustomVertex.PositionTextured.Format, Pool.Default);
actorvertexbuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, device, 0, CustomVertex.PositionTextured.Format, Pool.Default); The four decides the number of postions in the buffer, previously we needed four. Since we usen 4 courdinates to render a rectangel shape / tile
In the vertex buffer we save the coordinates for the 4 ”corners” of the picture
Obs this is a rectangel • actorvertexbuffer[0].X = 0.25f; actorvertexbuffer[0].Y = 0f;actorvertexbuffer[0].Z = 0.5f;actorvertexbuffer[1].X = 0.25f; actorvertexbuffer[1].Y = -0.50f; actorvertexbuffer[1].Z = 0.5f; actorvertexbuffer[2].X = 0f; actorvertexbuffer[2].Y = -0.50f; actorvertexbuffer[2].Z = 0.5f; actorvertexbuffer[3].X = 0f;actorvertexbuffer[3].Y = 0f; actorvertexbuffer[3].Z = 0.5f;
In the vertex buffer there is also information regarding what part of the picture to show • // Top Right Hand Corneractorvertexbuffer[0].Tu = 1.0f;actorvertexbuffer [0].Tv = 0.0f; //Bottom Right HAnd Corneractorvertexbuffer[1].Tu = 1.0f;actorvertexbuffer [1].Tv = 1.0f;//Bottom Left Hand Corneractorvertexbuffer [2].Tu = 0.0f;actorvertexbuffer [2].Tv = 1.0f; //Top Left Hand Corneractorvertexbuffer [3].Tu = 0.0f;actorvertexbuffer[3].Tv = 0.0f;
You can try to change the vaues • // Top Right Hand Corneractorvertexbuffer[0].Tu = 0.5f;actorvertexbuffer [0].Tv = 0.0f; //Bottom Right HAnd Corneractorvertexbuffer[1].Tu = 0.5f;actorvertexbuffer [1].Tv = 1.0f;//Bottom Left Hand Corneractorvertexbuffer [2].Tu = 0.0f;actorvertexbuffer [2].Tv = 1.0f; //Top Left Hand Corneractorvertexbuffer [3].Tu = 0.0f;actorvertexbuffer[3].Tv = 0.0f;
So we can use these coordinates to cut in the picture • next
In order to animate show one third of the picture at the time
First we need to fix the vertex buffer • Instead of coordinates for one square //or rectangle we need coordinates for three different squares/rectangles
What should we change the 4 to? • actorvertexbuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, device, 0, CustomVertex.PositionTextured.Format, Pool.Default); The four decides the number of postions in the buffer, previously we needed four. Since we usen 4 courdinates to render a rectangel shape / tile
We need information of 3*4 different corners... That would be 12 positions in the buffer actorvertexbuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 12, device, 0, CustomVertex.PositionTextured.Format, Pool.Default);
In position 0-3 we will store information about the first postion • // Top Right Hand Corneractorvertexbuffer[0].Tu = 0.33f;actorvertexbuffer[0].Tv = 0.0f; //Bottom Right HAnd Corneractorvertexbuffer[1].Tu = 0.33f;actorvertexbuffer[ [1].Tv = 1.0f;//Bottom Left Hand Corneractorvertexbuffer[ [2].Tu = 0.0f;actorvertexbuffer[2].Tv = 1.0f; //Top Left Hand Corneractorvertexbuffer[ [3].Tu = 0.0f;actorvertexbuffer[ [3].Tv = 0.0f;
We still need to give the x,y,z coordinates... • actorvertexbuffer[0].X = 0.25f; actorvertexbuffer[0].Y = 0f;actorvertexbuffer[0].Z = 0.5f;actorvertexbuffer[1].X = 0.25f; actorvertexbuffer[1].Y = -0.50f; actorvertexbuffer[1].Z = 0.5f; actorvertexbuffer[2].X = 0f; actorvertexbuffer[2].Y = -0.50f; actorvertexbuffer[2].Z = 0.5f; actorvertexbuffer[3].X = 0f;actorvertexbuffer[3].Y = 0f; actorvertexbuffer[3].Z = 0.5f;
In position 4-7 we will store information about the second postion • // Top Right Hand Corneractorvertexbuffer[4].Tu= 0.66f;actorvertexbuffer[4].Tv = 0.0f; //Bottom Right HAnd Corneractorvertexbuffer[5].Tu = 0.66f;actorvertexbuffer[ [5].Tv = 1.0f;//Bottom Left Hand Corneractorvertexbuffer[ [6].Tu = 0.33f;actorvertexbuffer[6].Tv = 1.0f; //Top Left Hand Corneractorvertexbuffer[ [7].Tu = 0.33f;actorvertexbuffer[ [7].Tv = 0.0f;
They will be the same we do not want to change the position only the piece of the picture that is used .. • actorvertexbuffer[4].X = 0.25f; actorvertexbuffer[4].Y = 0f;actorvertexbuffer[4].Z = 0.5f;actorvertexbuffer[5].X = 0.25f; actorvertexbuffer[5].Y = -0.50f; actorvertexbuffer[5].Z = 0.5f; actorvertexbuffer[6].X = 0f; actorvertexbuffer[6].Y = -0.50f; actorvertexbuffer[6].Z = 0.5f; actorvertexbuffer[7].X = 0f;actorvertexbuffer[7].Y = 0f; actorvertexbuffer[7].Z = 0.5f;
In position 8-12 we will store information about the third postion • // Top Right Hand Corneractorvertexbuffer[8].Tu= 1.0f;actorvertexbuffer[8].Tv = 0.0f; //Bottom Right HAnd Corneractorvertexbuffer[9].Tu = 1.0f;actorvertexbuffer[ [9].Tv = 1.0f;//Bottom Left Hand Corneractorvertexbuffer[ 10].Tu = 0.66f;actorvertexbuffer[10].Tv = 1.0f; //Top Left Hand Corneractorvertexbuffer [12].Tu = 0.66f;actorvertexbuffer[12].Tv = 0.0f;
Coordinates are still the same • actorvertexbuffer[8].X = 0.25f; actorvertexbuffer[8].Y = 0f;actorvertexbuffer[8].Z = 0.5f;actorvertexbuffer[9].X = 0.25f; actorvertexbuffer[9].Y = -0.50f; actorvertexbuffer[9].Z = 0.5f; actorvertexbuffer[10].X = 0f; actorvertexbuffer[10].Y = -0.50f; actorvertexbuffer[10].Z = 0.5f; actorvertexbuffer[11].X = 0f;actorvertexbuffer[11].Y = 0f; actorvertexbuffer[11].Z = 0.5f;
When all the information is stored into the vertexbuffer • How do we get the program to read from the right postion in the buffer
When all the information is stored into the vertexbuffer • How do we get the program to read from the right postion in the buffer • When we want to show the middle picture we want the program to read from postion 4 and so on...
Where the render functions should start reading in the vertex buffer is Decided by the integer at position below marked in red • mydevice.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
So if want to show the middle part of the picture • Render function need to start reading at postion 4 in the buffer. • mydevice.DrawPrimitives(PrimitiveType.TriangleFan, 4, 2);
Need a new variable • int offset
Offset need to alternate between 0,4,and 8 • if(offset==8){offset=0;}else{offset=offset+4;}
mindevice.DrawPrimitives(PrimitiveType.TriangleFan,offset, 2);
This will work as an animation • The joker will be running very fast on the same spot. • How can we slow him down?
We can make a first attemt to create a very simple clock • If (clock==10) {clock=0;} • else • {clock=clock+1;}
So postion is only changed when clock is 10 • if (clock == 10) { clock = 0; } else { clock = clock + 1; } • QuadMatrix.Translate(0.4f, 1.0f, 0f); • if (clock == 10) • { • if (arrayposition == 8) • { arrayposition = 0; } • else • { arrayposition = arrayposition + 4; } • } • mindevice.SetTransform(TransformType.World, QuadMatrix); • mindevice.SetStreamSource(0, spelaren.actorvertexbuffer , 0); • mindevice.SetTexture(0, spelare); • mindevice.RenderState.AlphaTestEnable = true; mindevice.RenderState.AlphaFunction = Compare.NotEqual; • mindevice.DrawPrimitives(PrimitiveType.TriangleFan, arrayposition, 2);
Now he needs to move... • Not just run at same spot.. • So how can we move him forward? • It is time to connect the keyboard.. • But before we do that try to hard code a motion forwar on the screen....