aboutsummaryrefslogtreecommitdiff
path: root/src/pong.c
blob: a30e8926a363a808a49330ace346bd9f4923e80e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* --------------------------- pong  ------------------------------------------ */
/*                                                                              */
/* A virtual bouncing ball.                                                     */
/* Written by Olaf Matthes <olaf.matthes@gmx.de>                                */
/* Based on pong (for Max) version 1.5 written by Richard Dudas.                */
/* Get source at http://www.akustische-kunst.org/puredata/maxlib/               */
/*                                                                              */
/* This program is free software; you can redistribute it and/or                */
/* modify it under the terms of the GNU General Public License                  */
/* as published by the Free Software Foundation; either version 2               */
/* of the License, or (at your option) any later version.                       */
/*                                                                              */
/* This program is distributed in the hope that it will be useful,              */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of               */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
/* GNU General Public License for more details.                                 */
/*                                                                              */
/* You should have received a copy of the GNU General Public License            */
/* along with this program; if not, write to the Free Software                  */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */
/*                                                                              */
/* Based on PureData by Miller Puckette and others.                             */
/*                                                                              */
/* ---------------------------------------------------------------------------- */

#include "m_pd.h"

static char *version = "pong v0.1, ported by Olaf Matthes <olaf.matthes@gmx.de>\n"
                       "           written for Max by Richard Dudas";

typedef struct pong
{
	t_object x_obj;		
	t_outlet *p_bounceout;
	t_outlet *p_handout;
	t_outlet *p_velout;
	t_outlet *p_heightout;
	t_clock  *p_klok;

	t_int    p_ms;  			// ms count
	t_float  p_time;			// current time div by warp
	t_float  p_timegrain;		// timegrain in seconds
	t_float  p_timegrainin;		// timegrain in ms
	t_float  p_warp;			// timewarp in ms
	
	t_float  p_dinit;			// init distance
	t_float  p_vinit;			// init velocity
	t_float  p_ainit;			// base acceleration def -100
	t_float  p_damping;			// realtime multiplicative damping
	t_float  p_dhand;			// virtual hand distance
	t_float  p_force;			// force of hand 1.0 = no force
 
	t_float  p_accel;			// current accel value
	t_float  p_vi;				// current velocity
	t_float  p_di;				// current distance
	t_float  p_dt;				// distance out
	t_float  p_dtprev;			// previous distance out for accel computation
	t_float  p_vt;				// velocity out
	t_int    p_prevchg;			// for logical transition
} t_pong;

/* ---------------------------------------------------------- */
/* ---- this stuff is mainly for the timer, on off etc... --- */
/* ---------------------------------------------------------- */

static void pong_reset(t_pong *x)
{
	x->p_di = x->p_dinit;
	x->p_dt = x->p_dinit;			// added
	x->p_dtprev = x->p_dinit;		// added
	x->p_vi = x->p_vinit;
	x->p_vt = x->p_vinit;			// added
	x->p_ms = 0;
	x->p_time = 0.;
	x->p_ainit = -100.;				// added, but currently disabled
	x->p_accel = -100.;				// reactivated (?)
	x->p_damping = 1.; 				// i.e. no initial damping
	x->p_prevchg = 0;				// added
	
/*	x->p_ms = 0;
	x->p_time = 0.;
	x->p_timegrain = 0.05;  // if ms grain = 50
	x->p_timegrainin = 50;
	
	x->p_vinit = 0.;
	x->p_dinit = 100.;	
	x->p_ainit = -100.;
	x->p_damping = 1.; 	// i.e. no initial damping 
	x->p_dhand = 100.;
	x->p_force = 1.;  // i.e. hand does nothing initially
	
	x->p_accel = -100.;
	x->p_vi = 0.;
	x->p_di = 100.;
	
	x->p_dt = 100.;			// changed from 0 to 100 
	x->p_dtprev = 100.;		// changed from 0 to 100 
	x->p_vt = 0.;
	x->p_prevchg = 0;
*/
}

/* ---------------------------------------------------------- */

static void pong_timein(t_pong *x, t_floatarg n)
{
	int thischg;
	
	x->p_time = n / x->p_warp;
	
	x->p_dt = ((x->p_accel * (x->p_time*x->p_time)) + (x->p_time * x->p_vi) + x->p_di);
	x->p_vt = ((x->p_dt - x->p_dtprev) / x->p_timegrain);
	
	if (x->p_dt < 0.)
	{
		x->p_dt *= -1.;
		
		x->p_di = 0.;
		x->p_vi = x->p_vt * (-1. * x->p_damping);  // -1 will eventually be a damping variable
		//post("vel at bounce %f", x->p_vi); 
		outlet_bang(x->p_bounceout);
		x->p_ms = 0;
		//x->p_dtprev= 0.;
	}
	//else
		x->p_dtprev = x->p_dt;
	
	/* ---------------------------------- virtual hand below ------------ */
	
	if (x->p_dt > x->p_dhand)  // presuming the hand is initially equal to the dinit
		thischg = 1;
	else
		thischg = 0;
		
	if (thischg != x->p_prevchg)
	{
		x->p_ms = 0;
		x->p_vi = x->p_vt;
		x->p_di = x->p_dhand;
		
		if (thischg == 0)
		{
			x->p_accel = -100.;   					// x->p_ainit in lieu of -100.
			outlet_float(x->p_handout, 0);
		}
		else
		{
			x->p_accel = (x->p_force * -100.);     // x->p_ainit in lieu of -100.
			outlet_float(x->p_handout, 1);
		}
	}
	
	x->p_prevchg = thischg;
	outlet_float(x->p_velout, x->p_vt);
	outlet_float(x->p_heightout, x->p_dt);
}

static void pong_onoff(t_pong *x, t_floatarg n)
{
	if (n != 0)
		clock_delay(x->p_klok, 0);
	else
		clock_unset(x->p_klok);
}

/* ---------------------------------------------------------- */

static void pong_bang(t_pong *x)
{
	x->p_ms = 0;
	clock_delay(x->p_klok, 0);
}

static void pong_stop(t_pong *x)
{
	clock_unset(x->p_klok);
}

/* ---------------------------------------------------------- */

static void pong_tick(t_pong *x)
{
	clock_delay(x->p_klok, (t_int)x->p_timegrainin);
	pong_timein(x, x->p_ms);
	//outlet_float(x->p_heightout, (float)x->p_ms);
	x->p_ms = x->p_ms + x->p_timegrainin;
}

/* ---------------------------------------------------------- */

static void pong_tgrain(t_pong *x, t_floatarg n)
{
	x->p_timegrain = n / x->p_warp;
	x->p_timegrainin = n;
	post("timegrain %f", x->p_timegrain);
}

/* ---------------------------------------------------------- */

static void pong_warpin(t_pong *x, t_floatarg n)
{
	x->p_warp = n;
	x->p_timegrain = x->p_timegrainin / x->p_warp;
	post("timewarp %f ms = one sec", x->p_warp);
}

/* ---------------------------------------------------------- */
/* ----- these are to receive and store the init values ----- */
/* ---------------------------------------------------------- */

static void pong_initdist(t_pong *x, t_floatarg n)
{
	x->p_dinit = n;
}

static void pong_initvel(t_pong *x, t_floatarg n)
{
	x->p_vinit = n;
}

static void pong_damp(t_pong *x, t_floatarg n)
{
	x->p_damping = n;
}

/* ---------------------------------------------------------- */

static void pong_baseacc(t_pong *x, t_floatarg n)
{
	//post ("baseaccel currently disabled", 0);
	 x->p_ainit = n; 
	 x->p_accel = x->p_ainit; 
}

/* ---------------------------------------------------------- */

static void pong_hand(t_pong *x, t_floatarg n)
{
	x->p_dhand = n;
}

static void pong_force(t_pong *x, t_floatarg n)
{
	x->p_force = n;
}

/* ---------------------------------------------------------- */


static t_class *pong_class;

static void *pong_new(t_floatarg n)
{
    t_pong *x = (t_pong *)pd_new(pong_class);

	x->p_klok = clock_new(x, (t_method)pong_tick);
	
	inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("dist"));	// distance
	inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("velo"));	// velocity
	inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("damp"));	// damping
	inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("force"));	// hand force 1.0 = no force
	inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("hand"));	// virtual hand (distance)

	
	if (n > 0)
		x->p_warp = n;
	else
		x->p_warp = 1000.;
	
	x->p_ms = 0;
	x->p_time = 0.;
	x->p_timegrain = 0.05;  // if ms grain = 50
	x->p_timegrainin = 50.0;
	
	x->p_vinit = 0.;
	x->p_dinit = 100.;	
	x->p_ainit = -100.;
	x->p_damping = 1.; 	// i.e. no initial damping
	x->p_dhand = 100.;
	x->p_force = 1.;  	// i.e. hand does nothing initially
	
	x->p_accel = -100.;
	x->p_vi = 0.;
	x->p_di = 100.;

	x->p_dt = 100.;			// changed from 0 to 100
	x->p_dtprev = 100.;		// changed from 0 to 100
	x->p_vt = 0.;
	x->p_prevchg = 0;
	
	x->p_bounceout = outlet_new(&x->x_obj, gensym("bang"));
	x->p_handout = outlet_new(&x->x_obj, gensym("float"));
	x->p_velout = outlet_new(&x->x_obj, gensym("float"));
	x->p_heightout = outlet_new(&x->x_obj, gensym("float"));

	return (x);
}

#ifndef MAXLIB
void pong_setup(void)
{
    pong_class = class_new(gensym("pong"), (t_newmethod)pong_new,
    	0, sizeof(t_pong), 0, A_DEFFLOAT, 0);
#else
void maxlib_pong_setup(void)
{
    pong_class = class_new(gensym("maxlib_pong"), (t_newmethod)pong_new,
    	0, sizeof(t_pong), 0, A_DEFFLOAT, 0);
	class_addcreator((t_newmethod)pong_new, gensym("pong"), A_DEFFLOAT, 0);
#endif
		/* method handlers for inlets */
	class_addmethod(pong_class, (t_method)pong_initdist, gensym("dist"), A_FLOAT, 0);
	class_addmethod(pong_class, (t_method)pong_initvel, gensym("velo"), A_FLOAT, 0);
	class_addmethod(pong_class, (t_method)pong_damp, gensym("damp"), A_FLOAT, 0);
	class_addmethod(pong_class, (t_method)pong_force, gensym("force"), A_FLOAT, 0);
	class_addmethod(pong_class, (t_method)pong_hand, gensym("hand"), A_FLOAT, 0);
		/* method handlers for other messages to first inlet */
	class_addmethod(pong_class, (t_method)pong_tgrain, gensym("timegrain"), A_FLOAT, 0);
	class_addmethod(pong_class, (t_method)pong_warpin, gensym("timewarp"), A_FLOAT, 0);
	class_addmethod(pong_class, (t_method)pong_baseacc, gensym("baseaccel"), A_FLOAT, 0);
	class_addmethod(pong_class, (t_method)pong_reset, gensym("reset"), 0);
	class_addmethod(pong_class, (t_method)pong_stop, gensym("stop"), 0);

    class_addfloat(pong_class, pong_onoff);
	class_addbang(pong_class, pong_bang);
#ifndef MAXLIB
    
    post(version);
#else
    class_sethelpsymbol(pong_class, gensym("maxlib/pong-help.pd"));
#endif
}