Archived

This topic is now archived and is closed to further replies.

Quake 2 water/slime/lava effect

This topic is 5586 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

I'm not sure if this is the right forum to put this in but I'll ask anyways because it deals with sin. Anyways, I would like to add the same swirling effect for water like you see in quake 2 (nothing more). So what I did was look in the actual quake 2 source files. Problem is, I don't quite understand how it works. This is what I found in the quake 2 source code:
    
typedef struct glpoly_s
{
	struct	glpoly_s	*next;
	struct	glpoly_s	*chain;
	int		numverts;
	int		flags;			// for SURF_UNDERWATER (not needed anymore?)

	float	verts[4][VERTEXSIZE];	// variable sized (xyz s1t1 s2t2)

} glpoly_t;


=================
WaterWarpPolyVerts

Mangles the x and y coordinates in a copy of the poly
so that any drawing routine can be water warped
=================

glpoly_t *WaterWarpPolyVerts (glpoly_t *p)
{
	int		i;
	float	*v, *nv;
	static byte	buffer[1024];
	glpoly_t *out;

	out = (glpoly_t *)buffer;

	out->numverts = p->numverts;
	v = p->verts[0];
	nv = out->verts[0];
	for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE, nv+=VERTEXSIZE)
	{
		nv[0] = v[0] + 4*sin(v[1]*0.05+r_newrefdef.time)*sin(v[2]*0.05+r_newrefdef.time);
		nv[1] = v[1] + 4*sin(v[0]*0.05+r_newrefdef.time)*sin(v[2]*0.05+r_newrefdef.time);

		nv[2] = v[2];
		nv[3] = v[3];
		nv[4] = v[4];
		nv[5] = v[5];
		nv[6] = v[6];
	}

	return out;
}
    
As you can see, this function takes a polygon and alters the vertices for the swirling effect. Because I'm not a math buff, the two lines with the sin function is what I'm confused on. Especially the r_newrefdef.time variable. From what I can tell, this appears to be the elapsed time but I'm not to sure on that. I couldn't find in the source code were it got it's value from to find out for sure. So are there any math orientated people willing to explain how those couple of lines work? Or perhaps someone has a good link they could refer me to. [edited by - MagTDK on August 28, 2002 7:56:47 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
draw or graph a sin function.

It ocillates between 0 and 1. Sin will only ever spit out those values. Notice the 4, this will be the max possible wave crest, if both of the sin results are 1. So you are ocillating the position of the vertex between -4 and 4 with a majority of the time being between them (because two decimals multiplied make a smaller decimal, multiplied by the 4.

Share this post


Link to post
Share on other sites