• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
alkisbkn

Normal of sine

13 posts in this topic

Hello,

I have been developing a simple ocean wave "heightfield" composed out of 6 sine waves. Now, I need to find the normal of each vertex, and I am stuck basically. How I calculate my waves:
[source lang="cpp"]
float2 dir = float2(1.0f, 0.0f);
float dir_vertex = dot(dir, wave_vertex.xz);

float wave1 = 25.0f * pow(sin( (2 * PI / 800) * dir_vertex - time * 2 * PI / 45.0f - 105.0f),2);

// .... and so on for the others ...

vertex.y = wave1 + wave2 + wave3 + wave4 + wave5 + wave6;
[/source]
(I know looks ugly, but it works)

Is there a computationally cheap way to find the normal (this is for an iOS game).

Thanks a bunch!
0

Share this post


Link to post
Share on other sites
If my maths does not fail me, the slope of the normal = the derivative, and the derivative of sin(x) is cos(x).
1

Share this post


Link to post
Share on other sites
Since you seem to have your surface expressed as y = F(x,z), you can compute two tangent vectors as (1,Fx(x,z),0) and (0,Fz(x,z),1), where Fx and Fz are the derivatives of F with respect to x and z respectively. You can then compute the cross product of these two vectors to get a normal vector. And then you probably need to normalize the resulting vector.

Post again if you try to do that for a while and don't succeed.
1

Share this post


Link to post
Share on other sites
Hi and thanks!

It does make sense now. However, I don't think I am getting the correct results. How I calculate my wave, tangent, bitangent and normal:
[source lang="c++"]
float2 dir = float2(1.0f, 0.0f);
float dir_vertex = dot(dir, wave_vertex.xz);

float wave1 = 25.0f * pow(sin( (2 * PI / 800) * dir_vertex - time * 2 * PI / 45.0f - 105.0f),2);

float3 tan1 = float3(1.0, 2 * cos( (2 * PI / 800) * dir_vertex - time * 2 * PI / 45.0f - 105.0f), 0.0);

float3 bitan1 = float3(0.0, 2 * cos( (2 * PI / 800) * dir_vertex - time * 2 * PI / 45.0f - 105.0f), 1.0);

float3 normal1 = normalize(cross(bitan1, tan1));
[/source]
Maybe my derivation is wrong. A power exponent goes in front of the base number when we derive, correct? Also the constant = 1, thus the amplitude is removed from the tan1 and bitan1 equations. I have attached a screenshot of the current output (for 2 perpendicular waves). To my understanding, when the wave is at full amplitude, the normal should be (0,1,0) (green colour), however it doesn't seem so in the screenshot.

Moreover, to get the final vertex normal, I do:
[CODE]normal = normalize(normal1 + normal2);[/CODE]
0

Share this post


Link to post
Share on other sites
The way you compute wave1(x,z) uses dir_vertex, which itself is a function of (x,z). The way you compute tan1 and bitan1 neglect this fact! Notice that alvaro hinted at computing the derivates dF(x,z)/dx and dF(x,z)/dz.

In fact, the formula you use is something like
wave1(x,z) := ( sin( f[sub]s[/sub] * ( dir[sub]x[/sub] * x + dir[sub]z[/sub] * z ) + f[sub]t[/sub] * t + p ) )[sup]2[/sup]
where f[sub]s[/sub] denoting a spatial frequency, f[sub]t[/sub] denoting a temporal frequency, and p denoting a phase.

For the purpose of spatial derivative the term f[sub]t[/sub] * t + p =: c[sub]t[/sub] is a constant, and f[sub]x[/sub] := f[sub]s[/sub] * dir[sub]x[/sub], f[sub]z[/sub] := f[sub]s[/sub] * dir[sub]z[/sub] can be used for simplicity. Then IIRC the derivatives are
tan1 := d wave(x,z) / d x = ( sin( f[sub]x[/sub] * x + f[sub]z[/sub] * z + c[sub]t[/sub] ) )[sup]2 [/sup]/ d x = 2 sin( f[sub]x[/sub] * x + f[sub]z[/sub] * z + c[sub]t[/sub] ) * cos( f[sub]x[/sub] * x + f[sub]z[/sub] * z + c[sub]t[/sub] ) * f[sub]x[/sub]
and
bitan1 := d wave(x,z) / d z = ( sin( f[sub]x[/sub] * x + f[sub]z[/sub] * z + c[sub]t[/sub] ) )[sup]2[/sup] / d z = 2 sin( f[sub]x[/sub] * x + f[sub]z[/sub] * z + c[sub]t[/sub] ) * cos( f[sub]x[/sub] * x + f[sub]z[/sub] * z + c[sub]t[/sub] ) * f[sub]z[/sub]
due to the [url="http://en.wikipedia.org/wiki/Chain_rule"]chain rule[/url]. Please check this twice. Edited by haegarr
1

Share this post


Link to post
Share on other sites
Hi haegarr,

I am now testing using 1 wave only and by implementing the equation you mentioned before. The problem is that the normal seems to always be (0,1,0) using this implementation:
[source lang="cpp"]
float2 dir = float2(1.0, 0.0);
float dir_vertex = dot(dir, wave_vertex.xz);

float fs = 2 * PI / 800.0f;
float t = time * 2 * PI / 45.0f;

float wave1 = 30.0f * pow(sin(fs * dir_vertex + t), 2);

float fx = dir.x * fs;
float fz = dir.y * fs;

float3 tan1 = float3(1.0,
2 * sin(fx * wave_vertex.x + fz * wave_vertex.z + t) *
cos (fx * wave_vertex.x + fz * wave_vertex.z + t) * fx,
0.0);

float3 bitan1 = float3(0.0,
2 * sin(fx * wave_vertex.x + fz * wave_vertex.z + t) *
cos (fx * wave_vertex.x + fz * wave_vertex.z + t) * fz,
1.0);

float3 normal1 = normalize(cross(bitan1, tan1));
[/source]
resulting in a green fragment color.
[img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] Edited by alkisbkn
0

Share this post


Link to post
Share on other sites
Well, using the correspondence
2 * sin(k) * cos(k) = sin(2k)
simplifies the implementation. In your given case there is fz = 0.0, giving
tan1 = [ 1, sin( 2 * fx * wave_vertex.x + 2 * t ), 0 ]
bitan1 = [ 0, 0, 1 ]

Here tan1 shouldn't be constant over x, should it? Hmmm. Perhaps using a graph plotter with that sine function gives an answer...
0

Share this post


Link to post
Share on other sites
This appears to be tricky afterall. Shouldn't the vertex and direction z be affecting the tan1 as well?

Edit:
I changed the wave function to something more simple for now, I basically removed the power to 2.
So, the derivative of:

[code]
float wave1 = 30.0f * sin(fs * dir_vertex + t);
[/code]

should now be:
[code]
float tan1y = cos(fs * dir.x * wave_vertex.x)
float bitan1y = cos(fs * dir.y * wave_vertex.z);
[/code]
(is t in the derivative equation? I think not, as it's constant)

However this too doesn't give me a correct result. Edited by alkisbkn
0

Share this post


Link to post
Share on other sites
Okay, it seems that the correct derivative of "wave1" equation is the following:
[code]
float3 tan1 = float3(1.0, 30.0 * fs * cos(fs * waveVertex.x + t), 0.0);
float3 bitan1 = float3(0.0, 30.0 * fs * cos(fs * waveVertex.x + t), 1.0);
[/code]
for tan and bitan. Then I do
[code]
normal = normalize(cross(bitan1, tan1));
[/code]
For 1 wave it seems to be correct, however how should I go about adding many wave's normals together? Just summing them and normalizing the result?
0

Share this post


Link to post
Share on other sites
Normals don't add up, but derivatives do. So compute the derivatives, add them together and then use the cross product to compute the normal.
0

Share this post


Link to post
Share on other sites
Thanks everyone, I think it is working correctly now. Below I am showing how I am doing it:
[code]
float height = 50.0 * sin(fs * waveVertex.x + t * _Time.y) +
125.0 * sin(fs * waveVertex.z + t * _Time.y + PI/4.0) +
135.0 * sin(-fs * waveVertex.x/4.0 - t * _Time.y * 0.75 + PI/2.0) +
165.0 * sin(-fs * waveVertex.z/8.0 - t * _Time.y * 0.5 + PI);
height *= 0.25;

float derivative = 150.0 * fs * cos(fs * waveVertex.x + t * _Time.y) +
125.0 * fs * cos(fs * waveVertex.z + t * _Time.y + PI/4.0) +
135.0 * -fs * cos(-fs * waveVertex.x/4.0 + t * _Time.y * 0.75 + PI/2.0) +
165.0 * -fs * cos(-fs * waveVertex.z/8.0 + t * _Time.y * 0.5 + PI);


float3 tan1 = float3(1.0, derivative, 0.0);
float3 bitan1 = float3(0.0, derivative, 1.0);

normal = normalize(cross(bitan1, tan1));
[/code]
And this gives me the result as you see in the screenshot. Edited by alkisbkn
0

Share this post


Link to post
Share on other sites
If you have a height field y(x,z) and compute the derivatives
t(x,z) := d y(x,z) / d x
b(x,z) := d y(x,z) / d z
then t denotes the rate of change of y when stepping in direction of x, and analogously b denotes the rate of change of y when stepping in direction of z. Written as vectors, you'll choose the unit length 1 for the step in direction of x or z, resp., and t or b for the corresponding change of y, i.e.
[b]T[/b](x,z) := [ 1; t(x,z); 0 ]
[b]B[/b](x,z) := [ 0; b(x,z); 1 ]

The cross product of these 2 vectors gives you the normal. However, you have 2 possibilities [b]T[/b] x [b]B[/b] and [b]B[/b] x [b]T[/b], but only one is valid for your purpose (depending on your choice of co-ordinate system). Above you've chosen
[b]N[/b](x,z) :=[b] B[/b](x,z) x [b]T[/b](x,z) = [ -t(x,y); 1; -b(x,z) ]
It has to be normalized, of course.

So what does this mean w.r.t. some picked slopes?
a) For a flat height field you'll get
y(x,z) = c ==> t(x,z) = 0; b(x,z) = 0 ==> [b]N[/b](x,z) = [ 0; 1; 0 ]
b) For a wave in x direction only you'll get at a position with slope of 45° (i.e. increasing)
t(x,z) = 1; b(x,z) = 0 ==> N(x,z) = [ -1; 1; 0 ]
c) For a wave in z direction only you'll get at a position with slope of -45° (i.e. decreasing)
t(x,z) = 0; b(x,z) = -1 ==> N(x,z) = [ 0; 1; 1 ]

This seems me okay.


Now, there are some mistakes in your pre-previous post:

1. Notice that normals are not invariant to non-uniform scaling.

E.g. scaling the height by a factor of 0.25 is non-uniform, because it has an effect on y only but not on x and z. If you scale y(x,z) by s and compute [b]N[/b] as above, you'll see that
[b]N[/b]'(x,z) = [ -t(x,y) * s; 1; -b(x,z) * s ] != [b]N[/b](x,z) * s

2. The 1st sin() is scaled by 50, while the 1st cos() is scaled by 150.

3. The argument in the 3rd sin() has a frequency of -fs/4, but the factor of the 3rd cos() shows -fs only.

4. The argument in the 4th sin() has a frequency of -fs/8, but the factor of the 4th cos() shows -fs only.

5. The wave function depends on both x and z position, but you compute only one derivative and use it for both directions. Edited by haegarr
0

Share this post


Link to post
Share on other sites
haegarr,

thank you for your help! I had 2 mistakes in my current code (the points from 2 - 5, I had corrected them before):
I was calculating the normal as:
normal = normalize(float3(derivativeX, 1, derivativeZ));
where it should have been with minus sign, as you said in your post.

The second mistake, was the scaling of the final accumulated height. I didn't expect it to have such a big impact!

So, thank you for your help, here is a screenshot of how the water looks now [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0