• Advertisement
Sign in to follow this  

Unity Need help programming car game

This topic is 414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I'm using Unity. I'm making a really sick car game, with dynamic car damage, crazy stunts, and wacky cartoonish graphics. I'm running into problems with math, though. Basically, I'm trying to make a function that inputs the damage done to the car and the Vector3 location of the impact. Then, the code should use this information to distort the mesh around the point of impact by a reasonable amount.

 

It looks like the following in pseudocode:

public void DestortMesh(collision point, amount of damage)
{
    Radius = damage / 10;
    for (each verex of the mesh)
    {
        if (the vertex is within the radius from the collision point)
        {
              distort the vertex
        }
    }
}

Here is the actual code:

    void deform(float radius, ContactPoint contactPoint)
    {
        Debug.Log("Deform called with radius of " + radius);
        Vector3 relativePosition = gameObject.transform.InverseTransformPoint(contactPoint.point);
        for (int x = 0; x < vertices.Count; x ++)
        {
            Debug.Log(Vector3.Distance(vertices[x], relativePosition));
            if (Vector3.Distance(vertices[x], relativePosition) <= radius)
            {
                //time to deform
                vertices[x] += vertices[x] * Random.Range(-.1f,.1f);
            }
            
        }

        mesh.SetVertices(vertices);
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
    }

This looks all fine and dandy, right? but when I look at the distances between each vertex and the collision point (the debug log right before the if statement), they look like this:

 

f9738bbdd33a7aa654b4c85a9fd3dd0d.png

 

basically, the output is weird. The speed of impact seems to affect it slightly, and for every impact, each vertex's distance is within a few 1000ths of each other. This makes it really hard to tell which points are close and which ones are far from the impact point. When trying out this in the game, the damage to the car is sporadic. For the most part, no damage is visible on the car. Then randomly, usually after a high speed impact is made, the car will become completely distorted, with every vertex changed.

 

You can see a video of this happening here:

 

So in the end, this is not the result I was aiming for. Something seems to be off with the Vector3 distance, but in the Unity forums all I'm getting are people saying there couldn't possibly be anything wrong with them. So I'm going to assume that there is something wrong with my code perhaps?

 

If you think you could help me, add me on Discord. Ramaraunt#6934

Edited by Ramaraunt

Share this post


Link to post
Share on other sites
Advertisement

Several things:

 

a) What is the ratio of the size of the bounding box of the mesh (enough "of" for now ;)) and the damage infliction?

 

b) From the video it seems me that the mesh has a too low resolution for deformation. Either use a higher resolution in advance or refine resolution locally just before applying the damage.

 

c) Damage infliction is done by applying a translation in direction of and proportional to the position of the vertex. That means that the intentional same damage factor applied to a vertex farther away from the origin has a higher impact, and that the bumps are not oriented correctly. What you probably want is to support a direction of damage (i.e. the damage is input as a vector with its length denoting the damage amplitude), scale that with one minus the normalized distance from the impact point, and add that to the vertex position.

 

d) Using a translation in range [-1,+1] gives inside and outside bumps; perhaps not what you want.

Share this post


Link to post
Share on other sites

Several things:

 

a) What is the ratio of the size of the bounding box of the mesh (enough "of" for now ;)) and the damage infliction?

 

b) From the video it seems me that the mesh has a too low resolution for deformation. Either use a higher resolution in advance or refine resolution locally just before applying the damage.

 

c) Damage infliction is done by applying a translation in direction of and proportional to the position of the vertex. That means that the intentional same damage factor applied to a vertex farther away from the origin has a higher impact, and that the bumps are not oriented correctly. What you probably want is to support a direction of damage (i.e. the damage is input as a vector with its length denoting the damage amplitude), scale that with one minus the normalized distance from the impact point, and add that to the vertex position.

 

d) Using a translation in range [-1,+1] gives inside and outside bumps; perhaps not what you want.

A: There will be 100 damage maximum before the car is wasted. The bounding box is about 2 by .4 by .7.

B: I want the resolution to be low. Is there a way to make that work?

C: Normalized distance? How do you normalize a float?

D: Yeah that could be changed.

 

 

I'll pm you  a copy of the game.

 

EDIT: I tried this

vertices[x] += Vector3.Normalize(vertices[x] - relativePosition) * Random.Range(-.1f,.1f); 

and it does this

 

d94fff0b72e0cefcde54479e3adb3d26.png

Edited by Ramaraunt

Share this post


Link to post
Share on other sites

BTW: I'm *not* running Unity3D at the moment, so we need to solve the problem on a more theoretical basis.

 

to A: What I asked for is the ratio of the bounding box size to the area of damage. If the radius of the sphere that locates the damage is, say, 1.0, while the bounding is 2 by .4 by .7, then every applied damage has an influence on at least half of the vertices. Hence my question: Is the ratio low enough?

 

to B: Think of the radius being 0.3 when damage is inflicted to the front right corner of the car. The bounding box's size in x direction is 2, and it is covered by 2 faces along from front to back. Only the vertices at the front are located within the sphere of damage. What we would expect is that the region 0.7 to 1.0 along x direction is damaged. However, the mesh's face ranges from 0.0 to 1.0 in that direction, and hence the deformation appears over half of the length of the car although the relatively small damage sphere.

 

to C: "Normalized distance" means that it ranges from 0.0 to 1.0. Since the unnormalized value ranges from 0.0 to radius, dividing by radius is the calculation to be done for normalization.

 

to D: Well, when being done "correctly", then this kind of calculation will be removed totally.

 

 

It seems me worth to clarify first how exactly an inflicted damage should look like...

Edited by haegarr

Share this post


Link to post
Share on other sites

I appreciate the responses! 

 

Here is my fixed up code using what you said

    void deform(float radius, ContactPoint contactPoint)
    {
        Debug.Log("Deform called with radius of " + radius);
        Vector3 center = gameObject.GetComponent<Rigidbody>().centerOfMass;
        Vector3 relativePosition = gameObject.transform.InverseTransformPoint(contactPoint.point);
        for (int x = 0; x < vertices.Count; x ++)
        {
            float normalized = (1 - (Vector3.Distance(vertices[x], relativePosition) / radius));
            //Debug.Log(Vector3.Distance(vertices[x], relativePosition));
            if (normalized > 0)
            {
                //time to deform
                vertices[x] -= vertices[x] * normalized *Random.Range(1, 5);
            }

            Debug.Log(normalized);
            
        }

        mesh.SetVertices(vertices);
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
    }

Here is the debug log output. Its still weird though.

 

http://pastebin.com/dH5fGy3Y

 

Why are all the distances for each point equal to about -3?

 

EDIT: btw, I've got a friend from blenderartists.org who is pumping out some nice models. In light of this, I changed the shaders to be more smooth looking. I like it much better.

 

48dad61308304039fb2876f5b886232b.png

Edited by Ramaraunt

Share this post


Link to post
Share on other sites

So, I tried doing just this:

float normalized = ((Vector3.Distance(vertices[x], relativePosition)));

And the distance between each vertex and the collision point is .98XXXXXX every single time.

Share this post


Link to post
Share on other sites

Solved it! The problem was, the model was originally imported really tiny, and it was scaled up 100 times. So the mesh.vertices values were really tiny.

 

Here is a video of the fixed game!

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Innoc uous
      I'm working on a space game, and I suck at art. I would love to get some help from someone who is more skilled than me. Things I need include modular space ship parts and GUI elements. Nothing too fancy, just functional so I can get a prototype put together. This could potentially become a serious project, but for now this is just a hobby project.
       
      In this video, you can see a few things I already completed
      :2018-02-24 20-08-13.flv2018-02-24 20-08-13.flv
    • By Innoc uous
      If you want to incorporate noise into your shaders, the Turbulance Library has you covered. Using code I gathered from this library, I made a cginc file that contains all you need to easily implement noise into your unity shaders. Who knows how this stuff works, but man, does it work well!
       
      https://pastebin.com/LLCUpJut
       
      Here is an example of what you can create using these noise functions.
       
    • By Nio Martinez
      I'll be buying a new laptop as my workstation for building games, Mostly 3D but not hard core. 
       
      I'm stuck at choosing between these 2 specs below. Does this really matter and if so, can some one tell my how and why it matters. 
      Choice1:
      Intel core i5-8250U (8th gen Kabylake refresh)(6 MB Smart Cache, 1.6 GHz Base with Turbo Boost up to 3.4 GHz) 4 cores 8 threads
      RAM 8 GB DDR4 (2400 MHz)
      GPU 2 GB DDR5 Nvidia MX150 256 bit
      SSD: yes
      Choice2:
      Intel core i7-7500U 2.70GHz Base Processor (4M Cache, up to 3.50 GHz Boost) 2 Cores, 4 Threads
      RAM 4 GB DDR4 (1800 MHz)
      GPU 2 GB DDR5 Nvidia GeForce 940MX 256 bit
      SSD: No
       
    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By devbyskc
      Hi Everyone,
      Like most here, I'm a newbie but have been dabbling with game development for a few years. I am currently working full-time overseas and learning the craft in my spare time. It's been a long but highly rewarding adventure. Much of my time has been spent working through tutorials. In all of them, as well as my own attempts at development, I used the audio files supplied by the tutorial author, or obtained from one of the numerous sites online. I am working solo, and will be for a while, so I don't want to get too wrapped up with any one skill set. Regarding audio, the files I've found and used are good for what I was doing at the time. However I would now like to try my hand at customizing the audio more. My game engine of choice is Unity and it has an audio mixer built in that I have experimented with following their tutorials. I have obtained a great book called Game Audio Development with Unity 5.x that I am working through. Half way through the book it introduces using FMOD to supplement the Unity Audio Mixer. Later in the book, the author introduces Reaper (a very popular DAW) as an external program to compose and mix music to be integrated with Unity. I did some research on DAWs and quickly became overwhelmed. Much of what I found was geared toward professional sound engineers and sound designers. I am in no way trying or even thinking about getting to that level. All I want to be able to do is take a music file, and tweak it some to get the sound I want for my game. I've played with Audacity as well, but it didn't seem to fit the bill. So that is why I am looking at a better quality DAW. Since being solo, I am also under a budget contraint. So of all the DAW software out there, I am considering Reaper or Presonus Studio One due to their pricing. My question is, is investing the time to learn about using a DAW to tweak a sound file worth it? Are there any solo developers currently using a DAW as part of their overall workflow? If so, which one? I've also come across Fabric which is a Unity plug-in that enhances the built-in audio mixer. Would that be a better alternative?
      I know this is long, and maybe I haven't communicated well in trying to be brief. But any advice from the gurus/vets would be greatly appreciated. I've leaned so much and had a lot of fun in the process. BTW, I am also a senior citizen (I cut my programming teeth back using punch cards and Structured Basic when it first came out). If anyone needs more clarification of what I am trying to accomplish please let me know.  Thanks in advance for any assistance/advice.
  • Advertisement