Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can I get (Ax, Ay) from a known dot product?

Discussion in 'Physics' started by Rocky_Unity, Dec 12, 2021.

  1. Rocky_Unity

    Rocky_Unity

    Joined:
    Oct 13, 2017
    Posts:
    96
    Hello!

    I am wondering if there is a way that I can move vector by a given dot product value.

    For example, if I want a vector to be at the point when its dot product is 0.7, I want to know the new coordinate points.

    I hope that makes sense

    Thank you!
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    I perceive your question as incomplete, but my guess is: for a given vector a give me the vector b such that a*b = 0.7

    If that’s correct, there is a very simple solution which is: b = 0.7 * a / a.magnitude. But this is just the trivial one of an infinite amount of possible solutions.

    but to avoid any further guessing, could you add some more information? What do you actually want to use this for?

    just for completeness sake the definition of the dot product (stolen from https://en.m.wikipedia.org/wiki/Dot_product)
    78F41EC4-CFF6-451B-8BAA-B683D1006D4A.jpeg
     
    Last edited: Dec 12, 2021
    Rocky_Unity likes this.
  3. Rocky_Unity

    Rocky_Unity

    Joined:
    Oct 13, 2017
    Posts:
    96
    Yes, I believe you have interpreted my question perfectly!

    I am wanting to apply Hook's Law to the dot product. Thusly giving a spring like effect to a system of nodes, visually connected by lines draw on the GPU. I know the original A and B vectors, and therefore I do have the magnitudes available to me.

    I believe the magnitude is what will help me solve for the 2nd unknown in the case the two unknowns, bx & by.

    Thank you @tjmaul !
     
    tjmaul likes this.
  4. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    If I interpret your post correctly you have a network of nodes connected through springs. For that, there is no need for the dot product.

    assuming you have two nodes A and B with their respective position vectors a and b, you can get the connecting vector like this

    x = a - b

    If for each spring you have the rest length L and the stiffness coefficient c, the force exerted on the two connected nodes is given by

    f = (x.magnitude - L) * c * x.normalized

    Explanation: it’s the actual distance between the nodes (magnitude) minus the rest length L (length of the spring without any forces) times the stiffness.

    For the other node it’s obviously -f (negative f)

    edit: Added the x.normalized to the assignment of f. Without, you’ll only get the scalar force but you’ll need the vectorial version
     
    Last edited: Dec 14, 2021
  5. Rocky_Unity

    Rocky_Unity

    Joined:
    Oct 13, 2017
    Posts:
    96

    I think this makes sense, I will try to apply it.
    I should post a picture, this



    @tjmaul They are supposed to be behave more grass like. Right now, they behave more.. web like. Each yellow dot is a node, the red is just a line drawn between each node.

    The part that I believe I am struggling with is applying Force, as an iterative time step. I'll keep trying!
     

    Attached Files:

    • 11.gif
      11.gif
      File size:
      119.3 KB
      Views:
      226
  6. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    Ah I see. That’s why you wanted to incorporate the angle :). I think a better mental modal would be a torsion spring. So each node is a torsion spring and the link is infinitely stiff.

    Is this a 2D problem? Is the grass just a visual feature? Is there a reason not to use the provided joints?
     
  7. Rocky_Unity

    Rocky_Unity

    Joined:
    Oct 13, 2017
    Posts:
    96
    Yes! I think that is exactly what I am after. I would especially love for cascading effects, like a force on the top-most node will translate some of the force down to the others... I believe that is what I want, to have the whole grass bend, instead of just the top piece.

    It is indeed a 2D problem, and just a visual feature. I am writing it in hlsl/compute shader, I pass in a circle collider's positional data before each update and do the collisions myself in the shader. I am thrilled with how I figured out to do the circle collisions, but my physics calculations are funky. All I'm doing is multiplying the distance of each node from its original position, by some constant, minus a dampening, times deltaTime. So that aspect is very crude
    I can absolutely use the joints, any data at all really, it's all under my control
     
  8. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    I see. Can you tell a bit more how you draw the blades of grass after calculating the position?

    My way to approach this would actually be just one mass node simulating the tip of the blade of grass and then draw a spline for each grass blade, starting from a static point down from the initial position of the tip and a tangent that goes straight up.

    You then only have to store each nodes initial position (to which it is always drawn to), its current position and velocity. With that you can easily implement a spring system (like I've elaborated above, with rest length 0) and add a damping term ( add a force of -d * velocity, counteracting the current velocity).

    To disturb the grass, you can basically do the same as you're doing right now.

    does that make any sense?
     
  9. Rocky_Unity

    Rocky_Unity

    Joined:
    Oct 13, 2017
    Posts:
    96
    I am using Bresenham's algorithm to draw each pixel of a line between each node.

    I really like your suggestion, it would be a lot more simple. One reason I went with multiple nodes for each grass blade, is so that I can have little animals munch the grass down. Bresenham does have an algorithm for splines though, too.

    I *think* I am already doing the physics as you mentioned. I am storing each node's initial position, and as its dynamic position changes, a velocity, springiness, dampening, and deltaTime variables are applied to it.

    The issue that comes in with that is the fact that I have multiple nodes, and that each node is not currently influencing the other. so it looks.... almost spider webby, as each node tries to oscillate back to its original position, instead of its original vector. I thought that maybe a reverse dot product (solving for Vector B), would be a start to correcting that. Perhaps I need a rethink on how exactly I should design this. This is all very challenging to think about, but it's fun to solve it :)
     
    Last edited: Dec 20, 2021