# Calculate ripple normals

Hey
I’m generating wave ripples with this function

``````public Vector3 GetSurfacePosition(Vector3 point) {
Vector3 ripplePosition = (initPosition * -1) * 2;
float offset = ((point.x * point.x) + (point.z * point.z));
offset += ((point.x * ripplePosition.x) + (point.z * ripplePosition.z));
var value = amplitude * Mathf.Sin(timer * speed + offset * frequency);
if(Vector3.Distance(point, initPosition) < rippleDistance)
return new Vector3(0, (value * amplitude), 0);
return Vector3.zero;
}
``````

It works well for getting the positions of the ripples, but now i would like to also get the normals.
I have tried with this function, but it doesn’t seem to return the correct values.

``````public Vector3 GetSurfaceNormal(Vector3 point) {
Vector3 ripplePosition = (initPosition * -1) * 2;
float offset = ((point.x * point.x) + (point.z * point.z));
offset += ((point.x * ripplePosition.x) + (point.z * ripplePosition.z));
float x = -1 * speed * amplitude * Mathf.Cos(-1 * timer * speed + offset * frequency);
float y = 1;
float z = speed * amplitude * Mathf.Cos(-1 * timer * speed + offset * frequency);
if(Vector3.Distance(point, initPosition) < rippleDistance)
return new Vector3(x,y,z).normalized * 2;

return Vector3.zero;
}
``````

Here’s a screenshot of what I have right now.

Does this work, Asger?

``````public Vector3 GetSurfaceNormal(Vector3 point)
{
Vector3 ripplePosition = (initPosition * -1) * 2;
float a = amplitude * amplitude;
float f = frequency;
float x = point.x;
float z = point.z;
float p = timer * speed;
float rx = ripplePosition.x;
float rz = ripplePosition.z;
Vector3 slopeX = new Vector3(1f, a * Mathf.Cos((x*x + z*z + x*rx + z*rz)*f+p) * (f*2*x + f*rx), 0f);
Vector3 slopeZ = new Vector3(0f, a * Mathf.Cos((x*x + z*z + x*rx + z*rz)*f+p) * (f*2*z + f*rz), 1f);

if (Vector3.Distance(point, initPosition) < rippleDistance)
return Vector3.Cross(slopeX, slopeZ).normalized;
else
return Vector3.right;
}
``````