• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

collision grid map effects How is it done

2 posts in this topic

HI Guys


Ive been teaching myself game programming for a few months and have received a lot of help and good advice from many different sources (books, forums,etc,...)and during that  time

Ive learned a lot of things(collision detection, effects, etc...) but with the subject of effects Ive decided to

go back to basics and ask just one question, how is it done. from day one there was one  technique that I wanted to learn and thats how to implement a particles impact effect  ( water splashes, grass debris, etc,...) on a terrain, for example a car emitting particles as it moves over a certain texture on the terrain or a character emitting a certain sound effect  with each footstep taken.  the method Ive tried so far was to shoot a ray from the model into the terrain returning the texture coordinates but this turned out to be very slow, but another helpful source told me to ditch the ray altogether, focus on the X and Z coordinates,  ignore the Y and return the position of the model over that particular spot on the grid map or heightmap and this is what Im working on now. but Im still just curious about what advice and pointers(no pun intended) you guys can give me.






The link  below is a video of the effect Im trying to emulate, an open source project called rigs of rods











Share this post

Link to post
Share on other sites

For this level of sophistication, I'd just put a special sensor nearby water. When the sensor is hit, particles are spawn.

Rays do work and are still used. For complex AAA stuff render-texture was used. It's now stream-out or compute shaders but if you want my opinion don't focus on that. Just make it so your collision system pulls out a callback and start with this.


To render the splashes at around 0:07 you will need to look at alpha blending (and possibly polygon offset but I don't think that's the case).


It is never too early to start using a physics library.


Share this post

Link to post
Share on other sites

Thanks for replying Krohm Ive been at it for a while and basically what I have so far is a method I

implemented that finds the height of the terrain(code below)  that my mobile car model is sitting on, but Im

not quite sure where to go from here or what further method calculations are needed to know

which textured triangle my 3D model is sitting on or colliding with.


                                                                                  Thanks again




collision update code(using c#/xna)

 protected override void Update(GameTime gameTime) 
            KeyboardState keyState = Keyboard.GetState(); 
            if (keyState.IsKeyDown(Keys.Escape)) 
                this.Exit(); // Exit with ESC key 
            camera.update(gameTime, effect); // update view 
              treshold = 12.0f; 
              terrainHeight = terrain.GetExactHeightAt(hummer.Position.X, -hummer.Position.Z); 
            if (hummer.Position.Y < terrainHeight + treshold) 
                newPos = hummer.Position; 
                newPos.Y = terrainHeight + treshold; 
                hummer.Position = newPos; 
                gravityVelocity = 0.0f; 
                if (keyState.IsKeyDown(Keys.L)) 
                    gravityVelocity = 85;      // Adjust this value to control height of jump.   
                // Not on ground so must be falling.   
                // Add in effect of gravity on current velocity.   
                float g = -1000.8f;  // m/sec^2   
                gravityVelocity += g * (float)gameTime.ElapsedGameTime.TotalSeconds; 
                // Add effect of velocity to position.   
                newPos = hummer.Position; 
                newPos.Y += gravityVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds; 
                hummer.Position = newPos; 

terrain code along with the find height method

using System; 
using System.Collections.Generic; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Net; 
using Microsoft.Xna.Framework.Storage; 
namespace ComputergrafixxxProject 
    public class Terrain 
         * Struct to hold vertex information with pos,normal,texcoord and texweight
        public struct VertexMultitextured 
            public Vector3 Position; 
            public Vector3 Normal; 
            public Vector4 TextureCoordinate; 
            public Vector4 TexWeights; 
            public static int SizeInBytes = (3 + 3 + 4 + 4) * sizeof(float); // vec3 + vec3 + vec4 +vec4 
            public static VertexElement[] VertexElements = new VertexElement[] 
                // stream, offset, type, method, usage and usage index 
                new VertexElement( 0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0 ), 
                new VertexElement( 0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0 ), 
                new VertexElement( 0, sizeof(float) * 6, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0 ), 
                new VertexElement( 0, sizeof(float) * 10, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1 ), 
        Game1 GameClass; 
        GraphicsDevice device; 
        int terrainWidth; 
        int terrainLength; 
        public float[,] heightData; 
        VertexBuffer terrainVertexBuffer; 
        IndexBuffer terrainIndexBuffer; 
        VertexDeclaration terrainVertexDeclaration; 
        Texture2D grassTexture; 
        Texture2D sandTexture; 
        Texture2D rockTexture; 
        Texture2D snowTexture; 
        public Terrain(Game1 reference,GraphicsDevice device) 
            this.GameClass = reference; 
            this.device = device; 
        private void LoadTextures() 
            grassTexture = GameClass.Content.Load<Texture2D>("grass"); 
            sandTexture = GameClass.Content.Load<Texture2D>("sand"); 
            rockTexture = GameClass.Content.Load<Texture2D>("rock"); 
            snowTexture = GameClass.Content.Load<Texture2D>("snow"); 
        private void LoadVertices() 
            Texture2D heightMap = GameClass.Content.Load<Texture2D>("heightmap");  
            VertexMultitextured[] terrainVertices = SetUpTerrainVertices(); 
            int[] terrainIndices = SetUpTerrainIndices(); 
            terrainVertices = CalculateNormals(terrainVertices, terrainIndices); 
            CopyToTerrainBuffers(terrainVertices, terrainIndices); 
            terrainVertexDeclaration = new VertexDeclaration(device, VertexMultitextured.VertexElements); 
        private void LoadHeightData(Texture2D heightMap) 
            float minimumHeight = float.MaxValue; 
            float maximumHeight = float.MinValue; 
            terrainWidth = heightMap.Width; 
            terrainLength = heightMap.Height; 
            // get rgb values for each pixel of hightmap 
            Color[] heightMapColors = new Color[terrainWidth * terrainLength]; 
            heightData = new float[terrainWidth, terrainLength]; 
            for (int x = 0; x < terrainWidth; x++) 
                for (int y = 0; y < terrainLength; y++) 
                    heightData[x, y] = heightMapColors[x + (y * terrainWidth)].R; // read r value 
                    // determine minimum and maximum height value in terrain 
                    if (heightData[x, y] < minimumHeight) minimumHeight = heightData[x, y]; 
                    if (heightData[x, y] > maximumHeight) maximumHeight = heightData[x, y]; 
            // get height values in a range between 0 and 30 
            for (int x = 0; x < terrainWidth; x++) 
                for (int y = 0; y < terrainLength; y++) 
                    heightData[x, y] = (heightData[x, y] - minimumHeight) / (maximumHeight - minimumHeight) * 30.0f; 
         * Define Vertices
        private VertexMultitextured[] SetUpTerrainVertices() 
            VertexMultitextured[] terrainVertices = new VertexMultitextured[terrainWidth * terrainLength]; 
            for (int x = 0; x < terrainWidth; x++) 
                for (int y = 0; y < terrainLength; y++) 
                    // generate a vertex for each pixel of the heightmap 
                    // a terrain is generated in x,-y direction 
                    terrainVertices[x + (y * terrainWidth)].Position = new Vector3(x, heightData[x, y], -y); 
                    terrainVertices[x + (y * terrainWidth)].TextureCoordinate.X = (float)x / 30.0f; // /30 to stretch texture so it looks realistic 
                    terrainVertices[x + (y * terrainWidth)].TextureCoordinate.Y = (float)y / 30.0f; 
                     * A vertex with height 12 should have texweight 1. 
                     * The weight should become 0 for heights 6 and 18, which are 12-6 and 12+6. In other words: all heights that 
                     * are within 6 meters from 12 meter high should have a weight factor for the grass texture. This explains the
                     * ‘abs(height-12)/6’: it will be 0 for height = 12, and become 1 as height approaches 6 or 18. But we need
                     * the opposite: at height 12 we need weight=1, and at heights 6 and 12 we need weight 0. So we subtract our
                     * line above from 1 and get ‘1- abs(height-12)/6’. This will become smaller than 0 for height lower than 6
                     * and larger than 18, so we clamp this value between 0 and 1.
                     * Although this is a step in the good direction, it isn’t perfect yet. For example: as their snow and
                     * rock weights are 0.2, the pixels corresponding to height 25 will get 20% of their color from the snow
                     * texture, and 20% from the rock texture. The remaining 60% will remain black, so they will look very dark.
                     * To solve this, we must make sure that for every vertex, the sum of all weights is exactly 1.
                     * To do this, for each vertex we’ll make the sum of all weights, and divide all weights by this sum. In case
                     * of the previous example, the sum would be 0.2 + 0.2 = 0.4. Next, 0.2 divided by 0.4 gives 0.5 for both the
                     * new snow and rock weights. And of course, 0.5 + 0.5 equals 1. This is what is shown in the right part 
                     * of the image above. You’ll notice that for each height, the summed weight value is 1.
                    // X = Sand, Y = Grass, Z = Stone and W = Snow 
                    terrainVertices[x + (y * terrainWidth)].TexWeights.X = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 0) / 8.0f, 0, 1); 
                    terrainVertices[x + (y * terrainWidth)].TexWeights.Y = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 12) / 6.0f, 0, 1); 
                    terrainVertices[x + (y * terrainWidth)].TexWeights.Z = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 20) / 6.0f, 0, 1); 
                    terrainVertices[x + (y * terrainWidth)].TexWeights.W = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 30) / 6.0f, 0, 1); 
                    float total = terrainVertices[x + y * terrainWidth].TexWeights.X; 
                    total += terrainVertices[x + y * terrainWidth].TexWeights.Y; 
                    total += terrainVertices[x + y * terrainWidth].TexWeights.Z; 
                    total += terrainVertices[x + y * terrainWidth].TexWeights.W; 
                    terrainVertices[x + y * terrainWidth].TexWeights.X /= total; 
                    terrainVertices[x + y * terrainWidth].TexWeights.Y /= total; 
                    terrainVertices[x + y * terrainWidth].TexWeights.Z /= total; 
                    terrainVertices[x + y * terrainWidth].TexWeights.W /= total; 
            return terrainVertices; 
         * Set indices clockwise to build faces
        private int[] SetUpTerrainIndices() 
             * new int[(terrainWidth - 1) * (terrainLength - 1) * 6]
             * Example, plane consisting of 4x3 points: first row is build out of 3 quads, second one also.
             *                                        row   col    3 points are needed for each triangle, 6 for one quad
             * so for a 4x3 points plane one can say (4-1)*(3-1) * 6
            int[] indices = new int[(terrainWidth - 1) * (terrainLength - 1) * 6]; 
            int counter = 0; 
            for (int y = 0; y < terrainLength - 1; y++) 
                for (int x = 0; x < terrainWidth - 1; x++) 
                    int lowerLeft = x + y * terrainWidth; 
                    int lowerRight = (x + 1) + y * terrainWidth; 
                    int topLeft = x + (y + 1) * terrainWidth; 
                    int topRight = (x + 1) + (y + 1) * terrainWidth; 
                    // order clockwise 
                    indices[counter++] = topLeft; 
                    indices[counter++] = lowerRight; 
                    indices[counter++] = lowerLeft; 
                    indices[counter++] = topLeft; 
                    indices[counter++] = topRight; 
                    indices[counter++] = lowerRight; 
            return indices; 
         * Calculate Normals on Terrain for realistic shadows
        private VertexMultitextured[] CalculateNormals(VertexMultitextured[] vertices, int[] indices) 
            // initialise normals with 0 0 0 
            for (int i = 0; i < vertices.Length; i++) 
                vertices[i].Normal = new Vector3(0, 0, 0); 
            for (int i = 0; i < indices.Length / 3; i++) 
                // Calculate Triangle 
                int index1 = indices[i * 3]; 
                int index2 = indices[i * 3 + 1]; 
                int index3 = indices[i * 3 + 2]; 
                // Calculate normal on triangle 
                Vector3 side1 = vertices[index1].Position - vertices[index3].Position; 
                Vector3 side2 = vertices[index1].Position - vertices[index2].Position; 
                Vector3 normal = Vector3.Cross(side1, side2); 
                // apply normal on all three vertices of triangle 
                vertices[index1].Normal += normal; 
                vertices[index2].Normal += normal; 
                vertices[index3].Normal += normal; 
            // normalize all normals 
            for (int i = 0; i < vertices.Length; i++) 
            return vertices; 
         * Copy vertices and indices onto grafikcard buffer to save performance (so this data has to be transferred ONLY ONCE)
        private void CopyToTerrainBuffers(VertexMultitextured[] vertices, int[] indices) 
            terrainVertexBuffer = new VertexBuffer(device, vertices.Length * VertexMultitextured.SizeInBytes, BufferUsage.WriteOnly); 
            terrainIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly); 
         * This draws the terrain
        public void DrawTerrain(Effect effect) 
            effect.CurrentTechnique = effect.Techniques["MultiTextured"]; 
            Matrix worldMatrix = Matrix.Identity; 
            effect.Parameters["xLightDirection"].SetValue(new Vector3(-0.5f, -1, -0.5f)); 
            foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
                device.Vertices[0].SetSource(terrainVertexBuffer, 0, VertexMultitextured.SizeInBytes); // tell it that this data is in graka buffer 
                device.Indices = terrainIndexBuffer; 
                device.VertexDeclaration = terrainVertexDeclaration; 
                int noVertices = terrainVertexBuffer.SizeInBytes / VertexMultitextured.SizeInBytes; 
                int noTriangles = terrainIndexBuffer.SizeInBytes / sizeof(int) / 3; 
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, noVertices, 0, noTriangles); 
        public float GetClippedHeightAt(float x, float z) 
            bool invalid = x < 0; 
            invalid |= z < 0; 
            invalid |= x > heightData.GetLength(0) - 1; 
            invalid |= z > heightData.GetLength(1) - 1; 
            if (invalid) 
                return 10; 
                return heightData[(int)x, (int)z]; 
        public float GetExactHeightAt(float xCoord, float zCoord) 
            bool invalid = xCoord < 0; 
            invalid |= zCoord < 0; 
            invalid |= xCoord > heightData.GetLength(0) - 1; 
            invalid |= zCoord > heightData.GetLength(1) - 1; 
            if (invalid) 
                return 10; 
            int xLower = (int)xCoord; 
            int xHigher = xLower + 1; 
            float xRelative = (xCoord - xLower) / ((float)xHigher - (float)xLower); 
            int zLower = (int)zCoord; 
            int zHigher = zLower + 1; 
            float zRelative = (zCoord - zLower) / ((float)zHigher - (float)zLower); 
            float heightLxLz = heightData[xLower, zLower]; 
            float heightLxHz = heightData[xLower, zHigher]; 
            float heightHxLz = heightData[xHigher, zLower]; 
            float heightHxHz = heightData[xHigher, zHigher]; 
            bool cameraAboveLowerTriangle = (xRelative + zRelative < 1); 
            float finalHeight; 
            if (cameraAboveLowerTriangle) 
                finalHeight = heightLxLz; 
                finalHeight += zRelative * (heightLxHz - heightLxLz); 
                finalHeight += xRelative * (heightHxLz - heightLxLz); 
                finalHeight = heightHxHz; 
                finalHeight += (1.0f - zRelative) * (heightHxLz - heightHxHz); 
                finalHeight += (1.0f - xRelative) * (heightLxHz - heightHxHz); 
            return finalHeight; 
as another sample of the effect I would like to emulate is another youtube link of a AAA game(ultimate ninja storm)where the characters are, with every footstep emitting particles based on their location on the terrain




Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0