Jump to content

  • Log In with Google      Sign In   
  • Create Account

collision grid map effects How is it done


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 football94   Members   -  Reputation: 198

Like
0Likes
Like

Posted 10 July 2013 - 10:23 PM

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.

 

 

                                                                                       Thankyou

 

 

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

 

 

 

 

 

 

 

 



Sponsor:

#2 Krohm   Crossbones+   -  Reputation: 3119

Like
0Likes
Like

Posted 12 July 2013 - 11:42 AM

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.



#3 football94   Members   -  Reputation: 198

Like
0Likes
Like

Posted 13 July 2013 - 02:07 AM

 

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; 
 
                //jump   
                if (keyState.IsKeyDown(Keys.L)) 
                { 
                    gravityVelocity = 85;      // Adjust this value to control height of jump.   
                }   
 
 
            } 
            else 
            { 
                // 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; 
            }   
 
             
               
            base.Update(gameTime); 

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; 
 
            LoadVertices(); 
            LoadTextures(); 
        } 
 
        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");  
            LoadHeightData(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]; 
            heightMap.GetData(heightMapColors); 
 
            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++) 
                vertices[i].Normal.Normalize(); 
 
            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); 
            terrainVertexBuffer.SetData(vertices); 
 
            terrainIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly); 
            terrainIndexBuffer.SetData(indices); 
        } 
 
        /* 
         * This draws the terrain
         */ 
        public void DrawTerrain(Effect effect) 
        { 
            effect.CurrentTechnique = effect.Techniques["MultiTextured"]; 
            effect.Parameters["xTexture0"].SetValue(sandTexture); 
            effect.Parameters["xTexture1"].SetValue(grassTexture); 
            effect.Parameters["xTexture2"].SetValue(rockTexture); 
            effect.Parameters["xTexture3"].SetValue(snowTexture); 
 
            Matrix worldMatrix = Matrix.Identity; 
            effect.Parameters["xWorld"].SetValue(worldMatrix); 
            effect.Parameters["xEnableLighting"].SetValue(true); 
            effect.Parameters["xAmbient"].SetValue(0.4f); 
            effect.Parameters["xLightDirection"].SetValue(new Vector3(-0.5f, -1, -0.5f)); 
 
            effect.Begin(); 
            foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
            { 
                pass.Begin(); 
 
                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); 
 
                pass.End(); 
            } 
            effect.End(); 
        } 
       
        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; 
            else 
                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); 
            } 
            else 
            { 
                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

 

 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS