void GenerateChunk(int startx) { Node n = scene.CreateChild(); n.SetPosition2D(startx, 0); var g = n.CreateComponent <CustomGeometry>(); g.SetMaterial(chunkmat); g.BeginGeometry(0, PrimitiveType.TRIANGLE_LIST); var s = n.CreateComponent <CustomGeometry>(); s.SetMaterial(surfMat); s.BeginGeometry(0, PrimitiveType.TRIANGLE_LIST); List <Vector2> surface = new List <Vector2>() { new Vector2(0, (float)noise.Evaluate((startx) * noiseScaleX, 0) * noiseScaleY ) }; lastSurfaceExtrusion += Vector3.Left * chunksize; float incr = 0.5f; for (float i = 0; i < chunksize - float.Epsilon * 2; i += incr) { float iend = i + incr; float tlY = SampleSurface(startx + i); float trY = SampleSurface(startx + iend); float blY = tlY + chunkheight; float brY = trY + chunkheight; Vector3 bl = new Vector3(i, blY, 0); Vector3 tl = new Vector3(i, tlY, 0); Vector3 br = new Vector3(iend, brY, 0); Vector3 tr = new Vector3(iend, trY, 0); //phys surface.Add(new Vector2(tr)); //decor CreateDecor(tr + Vector3.Right * startx, tl - tr); //surface visual Vector2 startV = Vector2.UnitX * (i / chunksize) * surfaceVisualRepeatPerChunk; Vector2 endV = Vector2.UnitX * (iend / chunksize * surfaceVisualRepeatPerChunk); //bl s.DefineVertex(lastSurfaceExtrusion); s.DefineTexCoord(startV); //tl s.DefineVertex(tl); s.DefineTexCoord(startV - Vector2.UnitY); //tr s.DefineVertex(tr); s.DefineTexCoord(-Vector2.UnitY + endV); //bl s.DefineVertex(lastSurfaceExtrusion); s.DefineTexCoord(startV); //tr s.DefineVertex(tr); s.DefineTexCoord(-Vector2.UnitY + endV); //br lastSurfaceExtrusion = tr + Quaternion.FromAxisAngle(Vector3.Back, 90) * Vector3.NormalizeFast(tr - tl); s.DefineVertex(lastSurfaceExtrusion); s.DefineTexCoord(endV); //ground //bl g.DefineVertex(bl); g.DefineTexCoord(new Vector2(bl / chunksize)); //tl g.DefineVertex(tl); g.DefineTexCoord(new Vector2(tl / chunksize)); //tr g.DefineVertex(tr); g.DefineTexCoord(new Vector2(tr / chunksize)); //bl g.DefineVertex(bl); g.DefineTexCoord(new Vector2(bl / chunksize)); //tr g.DefineVertex(tr); g.DefineTexCoord(new Vector2(tr / chunksize)); //br g.DefineVertex(br); g.DefineTexCoord(new Vector2(br / chunksize)); } s.Commit(); g.Commit(); CollisionChain2D col = n.CreateComponent <CollisionChain2D>(); col.SetLoop(false); col.SetFriction(10); col.SetVertexCount((uint)surface.Count + 1); chunks.Add(col); //Vector3 smoother = Quaternion.FromRotationTo(Vector3.Left, new Vector3(-1,-.3f,0)) * new Vector3(surface[0] - surface[1]); //col.SetVertex(0,surface[0] + new Vector2(smoother)); Vector2 smoother = new Vector2(-incr * .5f, (float)noise.Evaluate((startx - incr * .5f) * noiseScaleX, 0) * noiseScaleY - 0.005f); col.SetVertex(0, smoother); uint c2 = 0; foreach (Vector2 surfpoint in surface) { col.SetVertex(++c2, new Vector2(surfpoint.X, surfpoint.Y)); } n.CreateComponent <RigidBody2D>().SetBodyType(BodyType2D.BT_STATIC); }
void GenerateChunk(int startx) { // We create a node and position where the chunk starts Node node = _scene.CreateChild(); node.SetPosition2D(startx, 0); // We create components to render the geometries of the surface and the ground var groundComponent = node.CreateComponent <CustomGeometry>(); groundComponent.SetMaterial(_chunkmat); groundComponent.BeginGeometry(0, PrimitiveType.TRIANGLE_LIST); var surfaceComponent = node.CreateComponent <CustomGeometry>(); surfaceComponent.SetMaterial(_surfMat); surfaceComponent.BeginGeometry(0, PrimitiveType.TRIANGLE_LIST); // We initialize and add a single entry to the surface points list List <Vector2> surface = new List <Vector2>() { new Vector2(0, (float)_noise.Evaluate(startx * NoiseScaleX, 0) * NoiseScaleY ) }; // We translate the last surface extrusion point so it's local relative to the chunk we're creating _lastSurfaceExtrusion += Vector3.Left * Chunksize; // We //TODO continue float incr = SurfaceSegmentSize; for (float i = 0; i < Chunksize - float.Epsilon * 2; i += incr) { float iend = i + incr; float tlY = SampleSurface(startx + i); float trY = SampleSurface(startx + iend); float blY = tlY + Chunkheight; float brY = trY + Chunkheight; Vector3 bl = new Vector3(i, blY, -10); Vector3 tl = new Vector3(i, tlY, -10); Vector3 br = new Vector3(iend, brY, -10); Vector3 tr = new Vector3(iend, trY, -10); //phys surface.Add(new Vector2(tr)); //decor CreateDecor(tr + Vector3.Right * startx, tl - tr); //surface visual Vector2 startV = Vector2.UnitX * (i / Chunksize) * SurfaceRepeatPerChunk; Vector2 endV = Vector2.UnitX * (iend / Chunksize * SurfaceRepeatPerChunk); //bl surfaceComponent.DefineVertex(_lastSurfaceExtrusion); surfaceComponent.DefineTexCoord(startV); //tl surfaceComponent.DefineVertex(tl); surfaceComponent.DefineTexCoord(startV - Vector2.UnitY); //tr surfaceComponent.DefineVertex(tr); surfaceComponent.DefineTexCoord(-Vector2.UnitY + endV); //bl surfaceComponent.DefineVertex(_lastSurfaceExtrusion); surfaceComponent.DefineTexCoord(startV); //tr surfaceComponent.DefineVertex(tr); surfaceComponent.DefineTexCoord(-Vector2.UnitY + endV); //br _lastSurfaceExtrusion = tr + Quaternion.FromAxisAngle(Vector3.Back, 90) * Vector3.NormalizeFast(tr - tl); surfaceComponent.DefineVertex(_lastSurfaceExtrusion); surfaceComponent.DefineTexCoord(endV); //ground //bl groundComponent.DefineVertex(bl); groundComponent.DefineTexCoord(new Vector2(bl / Chunksize)); //tl groundComponent.DefineVertex(tl); groundComponent.DefineTexCoord(new Vector2(tl / Chunksize)); //tr groundComponent.DefineVertex(tr); groundComponent.DefineTexCoord(new Vector2(tr / Chunksize)); //bl groundComponent.DefineVertex(bl); groundComponent.DefineTexCoord(new Vector2(bl / Chunksize)); //tr groundComponent.DefineVertex(tr); groundComponent.DefineTexCoord(new Vector2(tr / Chunksize)); //br groundComponent.DefineVertex(br); groundComponent.DefineTexCoord(new Vector2(br / Chunksize)); } surfaceComponent.Commit(); groundComponent.Commit(); CollisionChain2D col = node.CreateComponent <CollisionChain2D>(); col.SetLoop(false); col.SetFriction(10); col.SetVertexCount((uint)surface.Count + 1); _chunks.Add(col); //Vector3 smoother = Quaternion.FromRotationTo(Vector3.Left, new Vector3(-1,-.3f,0)) * new Vector3(surface[0] - surface[1]); //col.SetVertex(0,surface[0] + new Vector2(smoother)); Vector2 smoother = new Vector2(-incr * .5f, (float)_noise.Evaluate((startx - incr * .5f) * NoiseScaleX, 0) * NoiseScaleY - 0.005f); col.SetVertex(0, smoother); uint c2 = 0; foreach (Vector2 surfpoint in surface) { col.SetVertex(++c2, new Vector2(surfpoint.X, surfpoint.Y)); } node.CreateComponent <RigidBody2D>().SetBodyType(BodyType2D.BT_STATIC); }
void GenerateChunk(int startx) { // We create a node and position where the chunk starts Node node = _scene.CreateChild(); node.SetPosition2D(startx, 0); // We create components to render the geometries of the surface and the ground var groundComponent = node.CreateComponent <CustomGeometry>(); groundComponent.SetMaterial(_chunkMaterial); groundComponent.BeginGeometry(0, PrimitiveType.TRIANGLE_LIST); var surfaceComponent = node.CreateComponent <CustomGeometry>(); surfaceComponent.SetMaterial(_surfaceMaterial); surfaceComponent.BeginGeometry(0, PrimitiveType.TRIANGLE_LIST); // We initialize and add a single entry to the surface collider points list List <Vector2> surfacePoints = new List <Vector2>() { new Vector2(0, (float)_noise.Evaluate(startx * NoiseScaleX, 0) * NoiseScaleY ) }; // We translate the last surface extrusion point so it's local relative to the chunk we're creating _lastSurfaceExtrusion += Vector3.Left * Chunksize; // We loop all the segments in this chunk float incr = SurfaceSegmentSize; for (float x = 0; x < Chunksize - float.Epsilon * 8; x += incr) { // We store vars for the position for the current segment end point x position and for the y position of the 4 points float xEnd = x + incr; float tlY = SampleSurface(startx + x); float trY = SampleSurface(startx + xEnd); float blY = tlY + Chunkheight; float brY = trY + Chunkheight; // We create vectors that represent Vector3 bl = new Vector3(x, blY, -10); Vector3 tl = new Vector3(x, tlY, -10); Vector3 br = new Vector3(xEnd, brY, -10); Vector3 tr = new Vector3(xEnd, trY, -10); // We add the top right point to the surface points list (remember we added the first point in the list init) surfacePoints.Add(new Vector2(tr)); // We call the CreateDecor function passing the global position of the current segment end point and the segment angle CreateDecor(tr + Vector3.Right * startx, tl - tr); // We create the geometry of the surface (UV os oriented according to the surface) Vector2 startV = Vector2.UnitX * (x / Chunksize) * SurfaceRepeatPerChunk; Vector2 endV = Vector2.UnitX * (xEnd / Chunksize * SurfaceRepeatPerChunk); //bl surfaceComponent.DefineVertex(_lastSurfaceExtrusion); surfaceComponent.DefineTexCoord(startV); //tl surfaceComponent.DefineVertex(tl); surfaceComponent.DefineTexCoord(startV - Vector2.UnitY); //tr surfaceComponent.DefineVertex(tr); surfaceComponent.DefineTexCoord(-Vector2.UnitY + endV); //bl surfaceComponent.DefineVertex(_lastSurfaceExtrusion); surfaceComponent.DefineTexCoord(startV); //tr surfaceComponent.DefineVertex(tr); surfaceComponent.DefineTexCoord(-Vector2.UnitY + endV); //br - We store the last point to use for continuity _lastSurfaceExtrusion = tr + Quaternion.FromAxisAngle(Vector3.Back, 90) * Vector3.NormalizeFast(tr - tl); surfaceComponent.DefineVertex(_lastSurfaceExtrusion); surfaceComponent.DefineTexCoord(endV); // We create the geometry of the ground (UV is in world coordinates) //bl groundComponent.DefineVertex(bl); groundComponent.DefineTexCoord(new Vector2(bl / Chunksize)); //tl groundComponent.DefineVertex(tl); groundComponent.DefineTexCoord(new Vector2(tl / Chunksize)); //tr groundComponent.DefineVertex(tr); groundComponent.DefineTexCoord(new Vector2(tr / Chunksize)); //bl groundComponent.DefineVertex(bl); groundComponent.DefineTexCoord(new Vector2(bl / Chunksize)); //tr groundComponent.DefineVertex(tr); groundComponent.DefineTexCoord(new Vector2(tr / Chunksize)); //br groundComponent.DefineVertex(br); groundComponent.DefineTexCoord(new Vector2(br / Chunksize)); } // We commit the geometry data we just created surfaceComponent.Commit(); groundComponent.Commit(); // We create the collider component for the chunk surface CollisionChain2D surfaceCollider = node.CreateComponent <CollisionChain2D>(); surfaceCollider.SetLoop(false); surfaceCollider.SetFriction(1); surfaceCollider.SetVertexCount((uint)surfacePoints.Count + 1); _chunks.Add(surfaceCollider); // We add a small overlapping segment with a bit of negative offset in y so the wheel passes smoothly over chunk seams Vector2 smoother = new Vector2(-incr * .5f, (float)_noise.Evaluate((startx - incr * .5f) * NoiseScaleX, 0) * NoiseScaleY - .005f); surfaceCollider.SetVertex(0, smoother); // Finally, we set the vertex of the surface collider for (int c = 0; c < surfacePoints.Count; c++) { Vector2 surfacePoint = surfacePoints[c]; surfaceCollider.SetVertex((uint)c + 1, surfacePoint); } // A collider must have a rigid body to interact with other bodies, even if static which is the case node.CreateComponent <RigidBody2D>().SetBodyType(BodyType2D.BT_STATIC); }