Procedurally Generate Terrain

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
LsJack
Posts: 4
Joined: Tue May 14, 2024 4:15 pm

Procedurally Generate Terrain

Post by LsJack »

hey guys,

i'm trying to create a procedural terrain generator, but im encountering some difficulties regarding collision and camera positioning [the idea is to be isometric].

the main idea is to always keep 9 chunks around the player, I'm searching on the internet, but it's difficult to find something that works, this is the code I got so far:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="0.251 0.502 0.502 1" AmbientLightColor="1 0.502 1 1" FrameRateStyle="1" CameraPosition="4 0.26 0.5" CameraRotation="-0.5 0 -7.9289" LightPosition="1 1 1" ClipNear="0.11" MouseVisible="255" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[// Fill 2D map with random height values

for(int x=1; x<=3; x++){
  for(int y=1; y<=3; y++){
    Terrain.Position.X = x*12;
    Terrain.Position.Y = y*12;

    Map[x,y] = Terrain;
    createModel(Map[x,y]);
  }
}]]>
      </Expression>
    </ZExpression>
    <SpawnModel Model="Player"/>
    <RefreshContent Component="TerrainMesh"/>
    <Variable Name="grounded"/>
  </OnLoaded>
  <OnUpdate>
    <ZExpression/>
  </OnUpdate>
  <OnRender>
    <UseMaterial Material="TextMaterial"/>
    <RenderText Name="fpsInfo" X="-0.99" Y="0.95" Scale="0.4" Align="1" TextExpression="&quot;FPS:&quot; + IntToStr(App.FpsCounter);"/>
    <RenderText Name="playerPos" X="-0.99" Y="0.85" Scale="0.4" Align="1">
      <TextExpression>
<![CDATA["P[X:" + IntToStr(Player.Velocity.x) +
" Y:" + IntToStr(Player.Velocity.y) +
" Z:" + IntToStr(Player.Velocity.z) + "]";]]>
      </TextExpression>
    </RenderText>
    <RenderText Name="terrainPos" X="-0.99" Y="0.75" Scale="0.4" Align="1">
      <TextExpression>
<![CDATA["T[X:" + IntToStr(Terrain.Velocity.X) +
" Y:" + IntToStr(Terrain.Velocity.y) +
" Z:" + IntToStr(Terrain.Velocity.z) + "]";]]>
      </TextExpression>
    </RenderText>
    <RenderText Name="cameraPos" X="-0.99" Y="0.65" Scale="0.4" Align="1">
      <TextExpression>
<![CDATA["C[X:" + IntToStr(App.CameraPosition.X) +
" Y:" + IntToStr(App.CameraPosition.y) +
" Z:" + IntToStr(App.CameraPosition.z) + "]";]]>
      </TextExpression>
    </RenderText>
  </OnRender>
  <Content>
    <Array Name="Axis" SizeDim1="2"/>
    <Array Name="Map" Type="3" Dimensions="1" SizeDim1="16" SizeDim2="16">
      <Values>
<![CDATA[789C63601805A360148C54000004000001]]>
      </Values>
    </Array>
    <Group Comment="Fontes">
      <Children>
        <Font Name="ZXFont" Bitmap="ZXBitmap" FirstChar="48" CharPixelWidth="8" CharPixelHeight="8"/>
        <Bitmap Name="ZXBitmap" Width="88" Height="8" Filter="1">
          <Producers>
            <BitmapFromFile DataWidth="88" DataHeight="8">
              <BitmapFile>
<![CDATA[789CED93C11A802008837DFF97B61BF5D9F68378ACDDC23926AC397FDC180FB87A1C8D17529D386DF44D457675A44290D96DEA04686E6EC0DFEADEB0076B72365C05F6BEDC0265E617FDC0A7CB2DCB2E03812BA9A06C0AA12D9A774D2BF9DF9AA7942ACE3FA535020C38FCBFC0A49C3F14E5D3608F9C071789363FF5235D7D1C1759E63F16]]>
              </BitmapFile>
            </BitmapFromFile>
          </Producers>
        </Bitmap>
        <Material Name="ZXMaterial" Shading="1" Light="0" ZBuffer="0" Font="ZXFont"/>
        <Material Name="TextMaterial"/>
      </Children>
    </Group>
    <Group Comment="Player">
      <Children>
        <Model Name="Player" Position="4 0.26 4">
          <Definitions>
            <Variable Name="facing"/>
            <Variable Name="facing_x"/>
          </Definitions>
          <OnSpawn>
            <ZExpression Comment="setup">
              <Expression>
<![CDATA[if (!debug) {
  centerMouse();
}
facing_x=-.2;   //horizontsal
app.cameraRotation.x=-.2;
grounded=1;]]>
              </Expression>
            </ZExpression>
          </OnSpawn>
          <OnUpdate>
            <ZExpression>
              <Expression>
<![CDATA[currentModel.velocity.x*=.85;  // friction
currentModel.velocity.y*=.85;

 //mouse controls
facing-=clamp(app.mousePosition.x*.2,-0.03,0.03);
facing_x-=clamp(app.mousePosition.y*.2,-0.01,0.01);
if (!debug) {
  centerMouse();
}

  //camera
app.cameraPosition.x=currentModel.position.x;
app.cameraPosition.y=currentModel.position.y;
app.cameraPosition.z=currentModel.position.z;

facing_x=clamp(facing_x,-.5,-0.01); //look limit
app.cameraRotation.z = 0-facing+.25;
app.cameraRotation.x = facing_x;

 //gravity?
if (currentModel.position.z > .5) {
  currentModel.velocity.z -= .2;
} else {
  currentModel.position.z = .5;
  currentModel.velocity.z = 0;
  grounded = 1;
}]]>
              </Expression>
            </ZExpression>
            <KeyPress Comment="UP" Keys="^w">
              <OnPressed>
                <ZExpression>
                  <Expression>
<![CDATA[currentModel.velocity.X+=cos(facing*6.28);
currentModel.velocity.Y+=sin(facing*6.28);]]>
                  </Expression>
                </ZExpression>
              </OnPressed>
            </KeyPress>
            <KeyPress Comment="DOWN" Keys="_s">
              <OnPressed>
                <ZExpression>
                  <Expression>
<![CDATA[currentModel.velocity.X-=cos(facing*6.28);
currentModel.velocity.Y-=sin(facing*6.28);]]>
                  </Expression>
                </ZExpression>
              </OnPressed>
            </KeyPress>
            <KeyPress Comment="SPACE" Keys=" }">
              <OnPressed>
                <ZExpression>
                  <Expression>
<![CDATA[if (grounded==1) {
  currentModel.velocity.z=25;
  grounded=0;
}]]>
                  </Expression>
                </ZExpression>
              </OnPressed>
            </KeyPress>
            <KeyPress Comment="LEFT" Keys="a&lt;">
              <OnPressed>
                <ZExpression>
                  <Expression>
<![CDATA[currentModel.velocity.X+=cos(facing*6.28+pi*.5);
currentModel.velocity.Y+=sin(facing*6.28+pi*.5);]]>
                  </Expression>
                </ZExpression>
              </OnPressed>
            </KeyPress>
            <KeyPress Comment="RIGHT" Keys="d&gt;">
              <OnPressed>
                <ZExpression>
                  <Expression>
<![CDATA[currentModel.velocity.X+=cos(facing*6.28-pi*.5);
currentModel.velocity.Y+=sin(facing*6.28-pi*.5);]]>
                  </Expression>
                </ZExpression>
              </OnPressed>
            </KeyPress>
          </OnUpdate>
          <OnRender>
            <UseMaterial Material="PlayerMaterial"/>
            <RenderMesh Mesh="PlayerMesh"/>
          </OnRender>
        </Model>
        <Mesh Name="PlayerMesh">
          <Producers>
            <MeshBox XCount="15" YCount="1" Grid2DOnly="255"/>
            <MeshExpression Scale="0.125 0.125 0.125" AutoNormals="0">
              <Expression>
<![CDATA[//

float a = V.X*PI;

V.X = sin(a);
V.Z = cos(a);

N.X = V.X;
N.Y = 0;
N.Z = V.Z;

if(V.Y == 0)
{
  V = 0;
}]]>
              </Expression>
            </MeshExpression>
          </Producers>
        </Mesh>
        <Material Name="PlayerMaterial" Color="1 0 0 1" DrawBackFace="255"/>
      </Children>
    </Group>
    <Group Comment="Terrain">
      <Children>
        <Model Name="Terrain">
          <Definitions>
            <Variable Name="TerrainPosition" Type="1"/>
            <Mesh Name="TerrainMesh">
              <Producers>
                <MeshBox Scale="6 6 0" XCount="16" YCount="16"/>
                <MeshExpression Scale="1 1 0.3" Expression="V.Z = noise3(V.X, V.Y-TerrainPosition, 0)*2;"/>
              </Producers>
            </Mesh>
          </Definitions>
          <OnSpawn>
            <ZExpression Expression="TerrainPosition = CurrentModel.Position.Y;"/>
          </OnSpawn>
          <OnRender>
            <UseMaterial Material="TerrainMaterial"/>
            <RenderMesh Mesh="TerrainMesh"/>
          </OnRender>
        </Model>
        <Material Name="TerrainMaterial" Shading="1" Color="0.502 0.502 0.502 1"/>
      </Children>
    </Group>
  </Content>
</ZApplication>
User avatar
VilleK
Site Admin
Posts: 2281
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Procedurally Generate Terrain

Post by VilleK »

Hi,

There are some example on this forum such as this thread: viewtopic.php?p=10532&hilit=terrain#p10532
Post Reply