Hey guys,
I got a little problem with an Array i made.
Here is the code:
<br />
using System;<br />
using MTV3D65;<br />
<br />
namespace Template<br />
{<br />
<br />
public class GrassFactory<br />
{<br />
<br />
const float LandScapeSize = 256 * 8;
const float GrassNumber = 200;
const float NumberOfTiles = 12; <br />
<br />
private int i;<br />
private int x;<br />
private int y;<br />
private GrassType[,,] Grass;<br />
private TVMesh[] GrassMeshes = new TVMesh[4];
<br />
<br />
private float CamTilePosX;<br />
private float CamTilePosY;<br />
private float Distance;<br />
private float DistX;<br />
private float DistZ;<br />
private float TranspDistance;<br />
private float Transparency;<br />
private float OneTileSize;<br />
private float Denominator;<br />
private float Transp2;<br />
<br />
TVGlobals Globals;<br />
TVEngine TV;<br />
<br />
<br />
public GrassFactory(TVLandscape Land, TVScene Scene)<br />
{<br />
Globals = new TVGlobals();<br />
TV = new TVEngine();<br />
<br />
float PosX;<br />
float PosZ;<br />
float MeshRandom;<br />
Random random = new Random();<br />
<br />
GrassType[,,] Grass = new GrassType[(int)NumberOfTiles, (int)NumberOfTiles, (int)GrassNumber];<br />
<br />
OneTileSize = LandScapeSize / NumberOfTiles;<br />
<br />
GrassMeshes[0] = Scene.CreateBillboard(Globals.GetTex("Tree01"), 0, 0, 0, 8, 4, "", true);<br />
GrassMeshes[1] = Scene.CreateBillboard(Globals.GetTex("Tree02"), 0, 0, 0, 5, 6, "", true);<br />
GrassMeshes[2] = Scene.CreateBillboard(Globals.GetTex("Tree03"), 0, 0, 0, 4, 7, "", true);<br />
GrassMeshes[3] = Scene.CreateBillboard(Globals.GetTex("Tree04"), 0, 0, 0, 3, 2, "", true);<br />
GrassMeshes[0].SetCollisionEnable(false);<br />
GrassMeshes[1].SetCollisionEnable(false);<br />
GrassMeshes[2].SetCollisionEnable(false);<br />
GrassMeshes[3].SetCollisionEnable(false);<br />
<br />
for (y = 0; y < (NumberOfTiles - 1); y++) <br />
{<br />
for (x = 0; x < (NumberOfTiles - 1); x++) <br />
{<br />
for (i = 1; i < GrassNumber; i++) <br />
{<br />
TV.AddToLog("X:" + x + " Y:" + y + " i:" + i);<br />
<br />
PosX = OneTileSize * x + (float)random.NextDouble() * OneTileSize;<br />
PosZ = OneTileSize * y + (float)random.NextDouble() * OneTileSize;<br />
MeshRandom = (float)random.NextDouble();<br />
Grass[x, y, i].Position.x = PosX;<br />
Grass[x, y, i].Position.y = Land.GetHeight(PosX, PosZ);<br />
Grass[x, y, i].Position.z = PosZ;<br />
Grass[x, y, i].ColorR = 0.9f;<br />
Grass[x, y, i].ColorG = 0.9f;<br />
Grass[x, y, i].ColorB = 0.8f;<br />
Grass[x, y, i].Size = Globals.Vector((float)random.NextDouble() * 0.4f + 0.8f, (float)random.NextDouble() * 0.4f + 0.8f, (float)random.NextDouble() * 0.4f + 0.8f);<br />
<br />
if (MeshRandom > 0.2) <br />
{
Grass[x, y, i].MeshType = 0;<br />
}<br />
else if (MeshRandom > 0.15) <br />
{<br />
Grass[x, y, i].MeshType = 1;<br />
}<br />
else if (MeshRandom > 0.1) <br />
{<br />
Grass[x, y, i].MeshType = 2;<br />
}<br />
else<br />
{<br />
Grass[x, y, i].MeshType = 3;<br />
}<br />
}<br />
}<br />
}<br />
<br />
TranspDistance = OneTileSize * 0.9f;
<br />
Denominator = 1 / ((OneTileSize * OneTileSize) - (TranspDistance * TranspDistance));
Transp2 = TranspDistance * TranspDistance;
}<br />
<br />
public void Render(MTV3D65.TV_3DVECTOR CamPos)
{<br />
GrassMeshes[0].Enable(true);<br />
GrassMeshes[1].Enable(true);<br />
GrassMeshes[2].Enable(true);<br />
GrassMeshes[3].Enable(true);<br />
CamTilePosX = (CamPos.x - OneTileSize / 2) / OneTileSize;<br />
CamTilePosY = (CamPos.z - OneTileSize / 2) / OneTileSize;<br />
<br />
DrawSurround(CamTilePosX, CamTilePosY, CamPos);<br />
<br />
if (CamTilePosX - 1 >= 0 && CamTilePosY - 1 >= 0) <br />
DrawSurround(CamTilePosX - 1, CamTilePosY - 1, CamPos);<br />
<br />
if (CamTilePosY - 1 >= 0) <br />
DrawSurround( CamTilePosX, CamTilePosY - 1, CamPos);<br />
<br />
if (CamTilePosX + 1 < NumberOfTiles && CamTilePosY - 1 >= 0) <br />
DrawSurround( CamTilePosX + 1, CamTilePosY - 1, CamPos);<br />
<br />
if (CamTilePosX - 1 >= 0 && CamTilePosY + 1 < NumberOfTiles) <br />
DrawSurround( CamTilePosX - 1, CamTilePosY + 1, CamPos);<br />
<br />
if (CamTilePosY + 1 < NumberOfTiles) <br />
DrawSurround( CamTilePosX, CamTilePosY + 1, CamPos);<br />
<br />
if (CamTilePosX + 1 < NumberOfTiles) <br />
DrawSurround( CamTilePosX + 1, CamTilePosY, CamPos);<br />
<br />
if (CamTilePosX + 1 < NumberOfTiles && CamTilePosY + 1 < NumberOfTiles) <br />
DrawSurround( CamTilePosX + 1, CamTilePosY + 1, CamPos);<br />
<br />
if (CamTilePosX - 1 >= 0) <br />
DrawSurround( CamTilePosX - 1, CamTilePosY, CamPos);<br />
<br />
GrassMeshes[0].Enable(false);<br />
GrassMeshes[1].Enable(false);<br />
GrassMeshes[2].Enable(false);<br />
GrassMeshes[3].Enable(false);<br />
}<br />
<br />
private void DrawSurround(float x, float y, MTV3D65.TV_3DVECTOR CamPos)<br />
{<br />
for (i = 1; i < Grass[(int)x, (int)y, 0].MeshType; i++) <br />
{<br />
GrassMeshes[Grass[(int)x, (int)y, i].MeshType].SetPosition(Grass[(int)x, (int)y, i].Position.x, Grass[(int)x, (int)y, i].Position.y, Grass[(int)x, (int)y, i].Position.z);<br />
GrassMeshes[Grass[(int)x, (int)y, i].MeshType].SetScale(Grass[(int)x, (int)y, i].Size.x, Grass[(int)x, (int)y, i].Size.y, Grass[(int)x, (int)y, i].Size.z);
DistX = Grass[(int)x, (int)y, i].Position.x - CamPos.x;<br />
DistZ = Grass[(int)x, (int)y, i].Position.z - CamPos.z;<br />
Distance = (DistX * DistX + DistZ * DistZ);<br />
<br />
if (Distance > Transp2) <br />
{<br />
Transparency = 1 - ((Distance - Transp2) * Denominator);<br />
if (Transparency > 0) <br />
{<br />
GrassMeshes[Grass[(int)x, (int)y, i].MeshType].SetColor(Globals.RGBA(Grass[(int)x, (int)y, i].ColorR, Grass[(int)x, (int)y, i].ColorG, Grass[(int)x, (int)y, i].ColorB, Transparency));<br />
GrassMeshes[Grass[(int)x, (int)y, i].MeshType].Render();<br />
}<br />
}<br />
else<br />
{<br />
GrassMeshes[Grass[(int)x, (int)y, i].MeshType].SetColor(Globals.RGBA(Grass[(int)x, (int)y, i].ColorR, Grass[(int)x, (int)y, i].ColorG, Grass[(int)x, (int)y, i].ColorB, 1));<br />
GrassMeshes[Grass[(int)x, (int)y, i].MeshType].Render();<br />
}<br />
<br />
}<br />
}<br />
}<br />
}<br />
And this is the GrassType class:
<br />
using System;<br />
using MTV3D65;<br />
<br />
namespace Template<br />
{<br />
public class GrassType<br />
{<br />
<br />
public int MeshType;<br />
public MTV3D65.TV_3DVECTOR Position;<br />
public MTV3D65.TV_3DVECTOR Size;
public float ColorR;<br />
public float ColorG;<br />
public float ColorB;<br />
<br />
public GrassType()<br />
{<br />
MeshType = 0;<br />
Position = new TV_3DVECTOR();<br />
Size = new TV_3DVECTOR();<br />
ColorR = 0;<br />
ColorG = 0;<br />
ColorR = 0;<br />
}<br />
}<br />
}<br />
I get my Error when i fist try to acces the array:
Grass[x, y, i].Position.x = PosX;
I really dont know why, because i init my array correcly (atleast i think).
Do u guys know what im doing wrong here?
|