GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
Physics.hh
Go to the documentation of this file.
1
2#ifndef PHYSICS_HH
3#define PHYSICS_HH
4
5#include "Effect.hh"
6#include "Equalizer.hh"
7
8struct PForce
9{
10public:
11 virtual Vector Force(Point p) const=0;
12};
13
14class Gravity : public PForce
15{
16public:
17 Gravity(float g) : g(g) { }
18 Vector Force(Point p) const { return Vector(0.0,-g,0.0); }
19private:
20 float g;
21};
22
23struct PPoint
24{
25public:
28 float time;
29 float mass;
30 std::vector<PForce*> vec;
31};
32
33class XForces : public Function<float,float>
34{
35public:
36 XForces(PPoint &pp) : pp(pp) { }
37 float Index(float time) const
38 {
39 int s = pp.vec.size();
40 float val = 0.0;
41 for(int i=0;i<s;i++)
42 {
43 PForce *f = pp.vec[i];
44 Vector v = f->Force(pp.pos);
45 val += v.dx;
46 }
47 val /= pp.mass;
48 return val;
49 }
50private:
51 PPoint &pp;
52};
53
54class YForces : public Function<float,float>
55{
56public:
57 YForces(PPoint &pp) : pp(pp) { }
58 float Index(float time) const
59 {
60 int s = pp.vec.size();
61 float val = 0.0;
62 for(int i=0;i<s;i++)
63 {
64 PForce *f = pp.vec[i];
65 Vector v = f->Force(pp.pos);
66 val += v.dy;
67 }
68 val /= pp.mass;
69 return val;
70 }
71private:
72 PPoint &pp;
73};
74
75class ZForces : public Function<float,float>
76{
77public:
78 ZForces(PPoint &pp) : pp(pp) { }
79 float Index(float time) const
80 {
81 int s = pp.vec.size();
82 float val = 0.0;
83 for(int i=0;i<s;i++)
84 {
85 PForce *f = pp.vec[i];
86 Vector v = f->Force(pp.pos);
87 val += v.dz;
88 }
89 val /= pp.mass;
90 return val;
91 }
92private:
93 PPoint &pp;
94};
95
96class CombinePositions : public Function<float, Point>
97{
98public:
102 Point Index(float time) const
103 {
104 float xx = x.Index(time);
105 float yy = y.Index(time);
106 float zz = z.Index(time);
107 return Point(xx,yy,zz);
108 }
109private:
113};
114
115
116// This is function float->Point
118{
119public:
120 PointMovement(PPoint &p, float time_step, float delta)
121 : p(p), xf(p), yf(p), zf(p), /*time_step(time_step),*/ delta(delta)
122 { }
123 Point Index(float time) const
124 {
125 IntegrationFunction ifx1(xf, delta, p.time, time, p.speed.dx);
126 IntegrationFunction ify1(yf, delta, p.time, time, p.speed.dy);
127 IntegrationFunction ifz1(zf, delta, p.time, time, p.speed.dz);
128
129 IntegrationFunction ifx2(ifx1, delta, p.time, time, p.pos.x);
130 IntegrationFunction ify2(ify1, delta, p.time, time, p.pos.y);
131 IntegrationFunction ifz2(ifz1, delta, p.time, time, p.pos.z);
132 CombinePositions comb(ifx2, ify2, ifz2);
133 return comb.Index(time);
134 }
135 Vector Speed(float time) const
136 {
137 IntegrationFunction ifx1(xf, delta, p.time, time, p.speed.dx);
138 IntegrationFunction ify1(yf, delta, p.time, time, p.speed.dy);
139 IntegrationFunction ifz1(zf, delta, p.time, time, p.speed.dz);
140 CombinePositions comb(ifx1, ify1, ifz1);
141 return Vector(comb.Index(time));
142 }
143 float Length() const { return 100000.0; }
144private:
145 PPoint &p;
146 XForces xf;
147 YForces yf;
148 ZForces zf;
149
150 //float time_step;
151 float delta;
152};
153
155
156class CollisionDistanceToPlane : public Function<Point, float>
157{
158public:
160 : pl(u_p, u_x, u_y) { }
161 float Index(Point p) const
162 {
163 return pl.Dist(p);
164 }
165private:
166 Plane pl;
167};
168
169class Any : public Function<float, int>
170{
171public:
172 int Index(float x) const { return 0; }
173};
174
175template<class T>
176class Zero : public Function<T, float>
177{
178public:
179 float Index(T t) const { return 0.0; }
180};
181
182
183class PairFunc : public Function<float, std::pair<Point,int> >
184{
185public:
186 PairFunc(Function<float, Point> &curve) : curve(curve) { }
187 std::pair<Point, int> Index(float time) const
188 {
189 return std::make_pair(curve.Index(time), 0);
190 }
191private:
193};
194
195template<class A, class B>
196class ProjA : public Function<std::pair<A,B>, A>
197{
198public:
199 A Index(std::pair<A,B> p) const { return p.first; }
200};
201
202template<class A, class B>
203class ProjB : public Function<std::pair<A,B>, B>
204{
205public:
206 B Index(std::pair<A,B> p) const { return p.second; }
207};
208
209template<class T, class A, class B>
210class PairFunc2 : public Function<T, std::pair<A,B> >
211{
212public:
213 PairFunc2(Function<T,A> &f, Function<T,B> &f2) : f(f), f2(f2) { }
214 std::pair<A,B> Index(T p) const { return std::make_pair(f.Index(p), f2.Index(p)); }
215private:
218};
219
220
221#if 0
222class CalculateCollisionPoint
223{
224public:
225 CalculateCollisionPoint(Function<float, Point> &curve,
226 Function<Point, float> &space, float start_time, float end_time)
227 : pairfunc(curve), pb(space, zero, fst, snd, pairfunc, start_time, end_time)
228 { }
229 float get_time() const { return pb.Elem().value; }
230 Point get_point() const { return pb.get_A(); }
231private:
232 PairFunc pairfunc;
233 FloatPullback<std::pair<Point,int>, Point, int> pb;
234 Any any;
235 Zero<int> zero;
236 Fst<Point,int> fst;
237 Snd<Point,int> snd;
238};
239#endif
240
241template<class P1, class P2>
242class PosFunc : public Function<std::pair<float,float>, std::pair<P1,P2> >
243{
244public:
245 PosFunc(Function<float, P1> &f1, Function<float,P2> &f2) : f1(f1),f2(f2) { }
246 std::pair<P1,P2> Index(std::pair<float,float> p) const
247 {
248 return std::make_pair(f1.Index(p.first), f2.Index(p.second));
249 }
250private:
253};
254
255
257{
258public:
259 SurfacePlaneIntersection(Function<Point2d, float> &surface, float y) : surface(surface), y(y) { }
260 bool Inside(Point2d p) const { return fabs(surface.Index(p)-y) < 0.001; }
262 {
263 Gradient g(surface);
264 Vector2d v = g.Index(p);
265 v/=20.0;
266 p+=v;
267 return p;
268 }
270 {
271 if (p.x<tl.x) return false;
272 if (p.x>br.x) return false;
273 if (p.y<tl.y) return false;
274 if (p.y>br.y) return false;
275 return true;
276 }
277 Point2d Loop(Point2d start_pos, Point2d tl, Point2d br, bool &succes)
278 {
279 Point2d p = start_pos;
280 while(InsideBox(p, tl, br) && !Inside(p))
281 {
282 p = Step(p);
283 }
284 succes = InsideBox(p, tl, br);
285 return p;
286 }
287private:
289 float y;
290};
293{
294public:
295 // RxRxR|->t
296 WorldWorldIntersection(Function<Point,float> &f, float t) : f(f), t(t) { }
297 bool Inside(Point p) const
298 {
299 return fabs(f.Index(p)-t) <0.001;
300 }
301 Point Step(Point p) const
302 {
303#if 0
304 Gradient3d g(f);
305 Vector v = g.Index(p);
306 v/=20.0;
307 p+=v;
308 return p;
309#endif
310 float val = f.Index(p);
311 Gradient3d g(f);
312 Vector vv = g.Index(p);
313 Vector v = val*(vv/vv.Dist());
314 v/=20.0;
315 //v*=-1;
316 p+=v;
317 //std::cout << "Point:" << p << std::endl;
318 return p;
319 }
320 bool InsideBox(Point p, Point tl, Point br)
321 {
322 if (p.x<tl.x) return false;
323 if (p.x>br.x) return false;
324 if (p.y<tl.y) return false;
325 if (p.y>br.y) return false;
326 if (p.z<tl.z) return false;
327 if (p.z>br.z) return false;
328 return true;
329 }
330 Point Loop(Point start_pos, Point tl, Point br, bool &b)
331 {
332 Point p = start_pos;
333 //int i=0;
334 while(InsideBox(p, tl, br) && !Inside(p))
335 {
336 p = Step(p);
337 //std::cout << i << " " << p << std::endl;
338 //i++;
339 }
340 b = Inside(p); //InsideBox(p, tl, br);
341 return p;
342 }
343
344private:
346 float t;
347};
348
350
351
352
353class FloatPairToStdPair : public Function<FloatPair, std::pair<float,float> >
354{
355public:
356 std::pair<float,float> Index(FloatPair p) const
357 {
358 return std::make_pair(p.x,p.y);
359 }
360};
361class FloatPointToStdPair : public Function<FloatPoint, std::pair<std::pair<float,float>, float> >
362{
363public:
364 std::pair<std::pair<float,float>, float> Index(FloatPoint p) const
365 {
366 return std::make_pair(std::make_pair(p.p.x,p.p.y),p.p.z);
367 }
368};
369
370#if 0
372{
373public:
374 LineLineIntersection(Point2d p_1, Vector2d v_1, Point2d p_2, Vector2d v_2)
375 : line1(p_1, p_1+v_1), line2(p_2,p_2+v_2), tl(-1000.0,-1000.0), br(1000.0,1000.0), pb(line1, line2, fst, snd, pair) { }
376 Point2d Elem() const
377 {
378 return pb.get_A();
379 }
380private:
381 LineIn2d line1, line2;
382 Distance2dPullback<std::pair<float,float>, Point2d, Point2d> pb;
383 FloatPairToStdPair pair;
384 FloatPair tl, br;
385};
386#endif
387class SurfaceFunction : public Function<std::pair<float,float>, Point>
388{
389public:
390 SurfaceFunction(SurfaceIn3d &surf) : surf(surf) { }
391 Point Index(std::pair<float,float> p) const
392 {
393 return surf.Index(p.first,p.second);
394 }
395private:
396 SurfaceIn3d &surf;
397};
398
399// A pullback of (RxR)x(R).
401{
402public:
403 RenderingEquation(Point ray_p, Vector ray_dir, SurfaceIn3d &surf, Point tl_, Point br_)
404 : r(ray_p, ray_dir),
405 surf_func(surf),
406 tl(tl_), br(br_),
407 pb(surf_func, r, proj_a, proj_b, comb, tl, br)
408 {}
409 Point InternalPos() const { return pb.Elem().get(); }
411 std::pair<float,float> p = pb.get_A();
412 Point2d pp;
413 pp.x = p.first;
414 pp.y = p.second;
415 return pp;
416 }
417 float RayPos() const {
418 float p = pb.get_B();
419 return p;
420 }
421 bool Success() const { return pb.Success(); }
422private:
423 ProjA<std::pair<float,float>, float> proj_a;
424 ProjB<std::pair<float,float>, float> proj_b;
426 Ray r; // float -> Point
427 SurfaceFunction surf_func; // float x float -> Point
428 FloatPoint tl,br;
429 Distance3dPullback<std::pair<std::pair<float,float>, float>, std::pair<float,float>, float> pb; // (RxR)x(R) pullback
430};
431
432#if 0
433class RenderingEquationBitmap : public Bitmap<Point2d>
434{
435public:
436 RenderingEquationBitmap(Point ray_p, Vector ray_x, Vector ray_y, Vector ray_z, SurfaceIn3d &surf, int sx, int sy) : ray_p(ray_p), ray_x(ray_x), ray_y(ray_y), ray_z(ray_z), surf(surf), tl(ray_p), br(ray_p+ray_x+ray_y+ray_z), sx(sx), sy(sy) { }
437 void Collect(CollectVisitor &vis) { }
438 void HeavyPrepare() { }
439
440 void Prepare() { }
441
442 virtual int SizeX() const { return sx; }
443 virtual int SizeY() const { return sy; }
444 virtual Point2d Map(int x, int y) const
445 {
446 float xx = x / float(sx);
447 float yy = y / float(sy);
448 Vector u_x = ray_x * xx;
449 Vector u_y = ray_y * yy;
450 Vector u_z = ray_z;
451 Point p = ray_p + u_x + u_y;
452 RenderingEquation eq(p, u_z, surf, tl, br);
453 if (eq.Success())
454 {
455 Point2d pp = eq.SurfacePos();
456 //std::cout << "SurfacePos:" << pp.x << " " << pp.y << std::endl;
457#if 1
458 float ssx = surf.XSize();
459 float ssy = surf.YSize();
460 while (pp.x < 0.0)
461 {
462 pp.x += ssx;
463 }
464 while (pp.x >= ssx)
465 {
466 pp.x -= ssx;
467 }
468 while (pp.y < 0.0)
469 {
470 pp.y += ssy;
471 }
472 while (pp.y >= ssy)
473 {
474 pp.y -= ssy;
475 }
476#endif
477 return pp;
478
479 }
480 Point2d p2 = { -1.0, -1.0 };
481 return p2;
482 }
483
484private:
485 Point ray_p;
486 Vector ray_x, ray_y, ray_z;
487 SurfaceIn3d &surf;
488 Point tl;
489 Point br;
490 int sx,sy;
491};
492#endif
493
494#if 0
495class TextureBitmap : public Bitmap<Color>
496{
497public:
498 TextureBitmap(Bitmap<Point2d> &coords, SurfaceIn3d &surf) : coords(coords), surface(surf) { }
499 void Collect(CollectVisitor &vis) {coords.Collect(vis); }
500 void HeavyPrepare() { }
501
502 void Prepare() { coords.Prepare(); }
503
504 virtual int SizeX() const { return coords.SizeX(); }
505 virtual int SizeY() const { return coords.SizeY(); }
506 virtual Color Map(int x, int y) const
507 {
508 Point2d p = coords.Map(x,y);
509 unsigned int c = surface.Color(p.x, p.y);
510 return Color(c);
511 }
512
513private:
514 Bitmap<Point2d> &coords;
515 SurfaceIn3d &surface;
516};
517#endif
518
519//
520// Some interesting classes follow
521//
522// p = m*v
523// F = dp/dt = m*a // does not make sense without time range
524
525
526struct Moment
527{
528 float mass;
530};
531
533{
534public:
535 virtual Moment mom(float time) const=0;
536};
537
538class Force
539{
540public:
541 Force(MomentInTime &time_evo, float t1, float t2) : time_evo(time_evo), t1(t1), t2(t2) { }
542 float mass() const { return (time_evo.mom(t2).mass - time_evo.mom(t1).mass)/(t2-t1); }
543// Vector accel() const { return (time_evo.mom(t2).v - time_evo.mom(t1).v)/(t2-t1); }
544 //Vector force() const { return mass()*accel(); }
545 float time1() const { return t1; }
546 float time2() const { return t2; }
547private:
548 MomentInTime &time_evo;
549 float t1,t2;
550};
551
553{
554public:
555 InvForce(Force &f, float mass2) : f(f), mass2(mass2) { }
556 float mass() const { return mass2; }
557// Vector accel() const { return force()/mass(); }
558// Vector force() const { return -f.force(); }
559 float time1() const { return f.time1(); }
560 float time2() const { return f.time2(); }
561private:
562 Force &f;
563 float mass2;
564};
565
566#if 0
567class InvMoment
568{
569public:
570 InvMoment(InvForce &f, Vector v) : f(f), v(v) { }
571 Moment moment1() const
572 {
573 Moment m;
574 m.mass = f.mass;
575 m.v = v;
576 return m;
577 }
578 Moment moment2() const
579 {
580 Moment vv;
581 vv.mass = f.mass();
582 vv.v = v + f.accel()*(f.time2()-f.time1());
583 }
584private:
585 InvForce &f;
586 Vector v;
587};
588#endif
589
590#endif
P func(int c, EveryApi &ev)
Definition test.cpp:59
SM Any(std::vector< char > alphabet)
Definition Language.hh:1006
Point2d SolveWithFailure(Function< Point2d, float > &func, Point2d tl, Point2d br, bool &b)
Definition Physics.cc:12
Vector SplitToComponent(Vector vec, Vector base)
Definition Physics.cc:4
Point2d LineLineIntersection(Point2d l1_p1, Point2d l1_p2, Point2d l2_p1, Point2d l2_p2)
Definition VectorTools.cc:1564
void error(std::string err, std::string fail_reason)
Definition check.cc:18
Definition compiled_tests.cpp:1189
Definition Physics.hh:170
int Index(float x) const
Definition Physics.hh:172
Definition compiled_tests.cpp:1200
Definition GraphI.hh:826
virtual int SizeY() const =0
virtual void Prepare()=0
virtual int SizeX() const =0
virtual C Map(int x, int y) const =0
virtual void Collect(CollectVisitor &vis)=0
Definition GameApi.cc:43596
virtual void HeavyPrepare()=0
CollisionDistanceToPlane(Point u_p, Vector u_x, Vector u_y)
Definition Physics.hh:159
float Index(Point p) const
Definition Physics.hh:161
Definition Physics.hh:97
CombinePositions(Function< float, float > &x, Function< float, float > &y, Function< float, float > &z)
Definition Physics.hh:99
Point Index(float time) const
Definition Physics.hh:102
Definition EffectI.hh:49
Definition Equalizer.hh:1122
Definition Physics.hh:354
std::pair< float, float > Index(FloatPair p) const
Definition Physics.hh:356
Definition Physics.hh:362
std::pair< std::pair< float, float >, float > Index(FloatPoint p) const
Definition Physics.hh:364
Definition Physics.hh:539
float time1() const
Definition Physics.hh:545
float time2() const
Definition Physics.hh:546
float mass() const
Definition Physics.hh:542
Force(MomentInTime &time_evo, float t1, float t2)
Definition Physics.hh:541
Definition EffectI.hh:11
Definition Equalizer.hh:121
Definition Equalizer.hh:102
Gravity(float g)
Definition Physics.hh:17
Vector Force(Point p) const
Definition Physics.hh:18
Definition Equalizer.hh:158
float time1() const
Definition Physics.hh:559
float time2() const
Definition Physics.hh:560
float mass() const
Definition Physics.hh:556
InvForce(Force &f, float mass2)
Definition Physics.hh:555
Definition Physics.hh:533
virtual Moment mom(float time) const =0
std::pair< A, B > Index(T p) const
Definition Physics.hh:214
PairFunc2(Function< T, A > &f, Function< T, B > &f2)
Definition Physics.hh:213
PairFunc(Function< float, Point > &curve)
Definition Physics.hh:186
std::pair< Point, int > Index(float time) const
Definition Physics.hh:187
Definition VectorTools.hh:350
float Length() const
Definition Physics.hh:143
Vector Speed(float time) const
Definition Physics.hh:135
Point Index(float time) const
Definition Physics.hh:123
PointMovement(PPoint &p, float time_step, float delta)
Definition Physics.hh:120
PosFunc(Function< float, P1 > &f1, Function< float, P2 > &f2)
Definition Physics.hh:245
std::pair< P1, P2 > Index(std::pair< float, float > p) const
Definition Physics.hh:246
Definition Physics.hh:197
A Index(std::pair< A, B > p) const
Definition Physics.hh:199
Definition Physics.hh:204
B Index(std::pair< A, B > p) const
Definition Physics.hh:206
Definition Effect.hh:10156
RenderingEquation(Point ray_p, Vector ray_dir, SurfaceIn3d &surf, Point tl_, Point br_)
Definition Physics.hh:403
Point2d SurfacePos() const
Definition Physics.hh:410
bool Success() const
Definition Physics.hh:421
float RayPos() const
Definition Physics.hh:417
Point InternalPos() const
Definition Physics.hh:409
Definition Physics.hh:388
SurfaceFunction(SurfaceIn3d &surf)
Definition Physics.hh:390
Point Index(std::pair< float, float > p) const
Definition Physics.hh:391
Definition Effect.hh:1880
bool InsideBox(Point2d p, Point2d tl, Point2d br)
Definition Physics.hh:269
SurfacePlaneIntersection(Function< Point2d, float > &surface, float y)
Definition Physics.hh:259
Point2d Step(Point2d p) const
Definition Physics.hh:261
Point2d Loop(Point2d start_pos, Point2d tl, Point2d br, bool &succes)
Definition Physics.hh:277
bool Inside(Point2d p) const
Definition Physics.hh:260
Definition VectorTools.hh:125
float Dist() const
Definition VectorTools.cc:141
float dy
Definition VectorTools.hh:268
float dz
Definition VectorTools.hh:268
float dx
Definition VectorTools.hh:268
Point Loop(Point start_pos, Point tl, Point br, bool &b)
Definition Physics.hh:330
bool Inside(Point p) const
Definition Physics.hh:297
bool InsideBox(Point p, Point tl, Point br)
Definition Physics.hh:320
Point Step(Point p) const
Definition Physics.hh:301
WorldWorldIntersection(Function< Point, float > &f, float t)
Definition Physics.hh:296
Definition Physics.hh:34
float Index(float time) const
Definition Physics.hh:37
XForces(PPoint &pp)
Definition Physics.hh:36
Definition Physics.hh:55
YForces(PPoint &pp)
Definition Physics.hh:57
float Index(float time) const
Definition Physics.hh:58
Definition Physics.hh:76
float Index(float time) const
Definition Physics.hh:79
ZForces(PPoint &pp)
Definition Physics.hh:78
Definition Physics.hh:177
float Index(T t) const
Definition Physics.hh:179
double f(const std::string &, double, bool) noexcept
Definition profile_cpp_calls_2.cpp:4
int f2(char c)
Definition test.cpp:51
Definition Equalizer.hh:952
float y
Definition Equalizer.hh:955
float x
Definition Equalizer.hh:955
Definition Equalizer.hh:978
Point p
Definition Equalizer.hh:1005
Definition Physics.hh:527
Vector v
Definition Physics.hh:529
float mass
Definition Physics.hh:528
Definition Physics.hh:9
virtual Vector Force(Point p) const =0
Definition Physics.hh:24
float time
Definition Physics.hh:28
Point pos
Definition Physics.hh:26
float mass
Definition Physics.hh:29
Vector speed
Definition Physics.hh:27
std::vector< PForce * > vec
Definition Physics.hh:30
Definition VectorTools.hh:910
float x
Definition VectorTools.hh:911
float y
Definition VectorTools.hh:911
Definition Parser.cc:60
float x
Definition Parser.cc:60
float y
Definition Parser.cc:60
float z
Definition Parser.cc:60
Definition GameApi.h:42
Definition VectorTools.hh:972