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

Recalculating a normal, knowing a local point, on a mesh which moves

Discussion in 'Scripting' started by djorgri, Jan 5, 2011.

  1. djorgri

    djorgri

    Joined:
    Jan 3, 2011
    Posts:
    17
    Hi everyone !

    I have a problem to update datas of a normal, coming from a raycast hit point.
    Here is the scene:

    i have a door whith a hinge joint.
    The player (FPS gameplay), can grab a point on the door and push it.
    I use a raycast to see where the door was hit, i store:

    - Hit Point Position
    - Hit Point Normal

    The player then applie a force on the point, following the normal.
    But i would like to update the normal (because the door rotate) whithout using a new raycast.
    I still know the local position of the first hit,
    isn't there any method to recalculate its normal ?

    Thank you for reading ! :)
    PS: Excuse me if my english is inacurate...

    Djor
     
  2. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    746
    Maths.... yay... I suck at it but I'd investigate something like this

    (previousDoorRotation - currentDoorRotation) gives you the amount the door has rotated... now you just gotta figure out how to rotate the hit normal you've stored...

    Good luck :)
     
  3. djorgri

    djorgri

    Joined:
    Jan 3, 2011
    Posts:
    17
    Hey Trooper, Thank you for answering !

    You're close ;)
    It is currentAngle - initialAngle etc...
    Now my normal vector follow the door movement !

    Code (csharp):
    1. var quat : Quaternion = Quaternion.Euler(0,objectHandled.transform.eulerAngles.y-initialAngleDoor,0);
    2. var newvect = quat * hitNormal;
    I have a strange behavior now, in some cases, when i push the mouse, it pushe the door, but at some angles, commands just invert...
    I'll check why and give explanation here when i find !

    --------------------------------

    Ok, now, i found why i had this problem, if this can help someone someday :)
    I was using :

    Code (csharp):
    1. objectHandled.rigidbody.AddForceAtPosition(newvect * Input.GetAxis("Mouse Y") * -15, hitPoint);
    instead of the working :

    Code (csharp):
    1. objectHandled.rigidbody.AddForceAtPosition(newvect * Input.GetAxis("Mouse Y") * -15, objectHandled.transform.TransformPoint(hitPoint));
    I was applying my vector (updated in time thanks to trooper !), in a point taken in local space,
    with my new line i translate my local space point to world space point.
    It now works ;) !
     

    Attached Files:

    Last edited: Jan 5, 2011