JNR
laCollisionTests.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #include "stdafx.h"
31 #include "jnr.h"
32 
33 laLine2 _dln[10];
34 laCollisionDomain _d[10];
35 
36 laPivot _pivot;
37 laLine2 _ln[10];
38 
39 // TEST for
40 // laCollider::collideLineWithDomain(const laLine2 &line, laCollisionScenario *pScenario,
41 // int nIterations, M_BOOL bResponse)
42 //
43 void stageCollisionTests::_initTest_2()
44 {
45  for(unsigned i=0; i<9; i++) _d[i].setNextDomain(_d + i + 1);
46  laPivot::colliderRange(_d + 0, 10);
47 
48  for(unsigned i=0; i<10; i++)
49  {
50  _dln[i].build_v( laPoint2((i+2)*M_UNIT*1.5, M_UNIT*10),
51  laPoint2(M_UNIT, M_UNIT*(rand()%10/10.0 - 0.5)));
52 
53  _d[i].addLine( _dln[i].origin, _dln[i].end(), laPoint3(0,-M_UNIT,0), NULL);
54  _d[i].updateBounds();
55  }
56 
57  _ln[0].build_v( laPoint2(M_UNIT*2, M_UNIT*2), laPoint2(M_UNIT, M_UNIT*0.5) );
58 }
59 
60 void stageCollisionTests::_doTest_2(laRenderer *pr, laInputManager *pi, laTimer *pt)
61 {
62  pr->styleLine(1);
63  pr->styleSet( laColor(255,0,0) );
64  _ln[0].draw( pr, laPoint2() );
65 
66  if(pi->key(KEY_LEFT)) _ln[0].origin.x( _ln[0].origin.x() - 0.5 * pt->delta() );
67  if(pi->key(KEY_RIGHT)) _ln[0].origin.x( _ln[0].origin.x() + 0.5 * pt->delta() );
68  if(pi->key(KEY_UP)) _ln[0].origin.y( _ln[0].origin.y() - 0.5 * pt->delta() );
69  if(pi->key(KEY_DOWN)) _ln[0].origin.y( _ln[0].origin.y() + 0.5 * pt->delta() );
70 
71  static double a = 0;
72  if(pi->key(KEY_1)) a += 2 * pt->delta();
73  if(pi->key(KEY_2)) a -= 2 * pt->delta();
74  _ln[0].vector.x( sin(a) * M_UNIT * 2 );
75  _ln[0].vector.y( cos(a) * M_UNIT * 2 );
76 
77  laCollisionScenario col;
78  col.pFirstDomain = _d;
79  col.nDomains = 10;
80  col.ptPosition = laPoint3();
81  col.ptVector = laPoint3(0, M_UNIT, 0);
82 
83  laCollider::collideLineWithDomain(_ln[0], &col, 3, M_FALSE);
84 
85  if(col.bCollision)
86  {
87  laLine2 v(col.ptCausing, col.ptCausing + col.ptVector);
88  pr->styleLine(1);
89  pr->styleSet( laColor(0,150,0) );
90  v.draw( pr, laPoint2() );
91 
92  laLine2 vm(col.ptCausing, col.ptCausing + col.ptModifiedVector);
93  pr->styleLine(2);
94  pr->styleSet( laColor(0,255,0) );
95  vm.draw( pr, laPoint2() );
96  }
97 }
98 
99 // TEST for
100 // M_BOOL laLine2::intersection(const laLine2 &ln, double *k, double *k_ln),
101 // laCollider::intersection()
102 //
103 void stageCollisionTests::_initTest_1()
104 {
105  for(unsigned i=0; i<9; i++) _d[i].setNextDomain(_d + i + 1);
106  laPivot::colliderRange(_d + 0, 1);
107 
108  for(unsigned i=0; i<10; i++)
109  _dln[i].build_v( laPoint2((i+2)*M_UNIT*1.5, M_UNIT*10),
110  laPoint2(M_UNIT, M_UNIT*(rand()%10/10.0 - 0.5)));
111 
112  _ln[0].build_v( laPoint2(M_UNIT*2, M_UNIT*2), laPoint2(M_UNIT, M_UNIT*0.5) );
113 }
114 
115 void stageCollisionTests::_doTest_1(laRenderer *pr, laInputManager *pi, laTimer *pt)
116 {
117  pr->styleLine(1);
118  pr->styleSet( laColor(255,0,0) );
119 
120  for(unsigned i=0; i<10; i++)
121  {
122  pr->styleSet( laColor(255,0,0) );
123  _dln[i].draw( pr, laPoint2() );
124 
125  double k, k_ln;
126  if(_dln[i].intersection( _ln[0], &k, &k_ln))
127  {
128  pr->font("plain")->ctlSize(M_UNIT*0.2);
129  pr->font("plain")->draw( _dln[i].origin, "True");
130  pr->modeTexture(M_FALSE);
131  pr->styleSet( laColor(255,0,0) );
132  }
133 
134  laPoint2 ptIntersect = _dln[i].at(k);
135  if( (k>=0) && (k<=1) )
136  {
137  if( (k_ln>=0) && (k_ln<=1) ) pr->styleSet( laColor(0,255,0) );
138 
139  ptIntersect.draw(pr);
140  }
141  }
142 
143  _ln[0].draw( pr, laPoint2() );
144 
145  if(pi->key(KEY_LEFT)) _ln[0].origin.x( _ln[0].origin.x() - 0.5 * pt->delta() );
146  if(pi->key(KEY_RIGHT)) _ln[0].origin.x( _ln[0].origin.x() + 0.5 * pt->delta() );
147  if(pi->key(KEY_UP)) _ln[0].origin.y( _ln[0].origin.y() - 0.5 * pt->delta() );
148  if(pi->key(KEY_DOWN)) _ln[0].origin.y( _ln[0].origin.y() + 0.5 * pt->delta() );
149 
150  static double a = 0;
151  if(pi->key(KEY_1)) a += 2 * pt->delta();
152  if(pi->key(KEY_2)) a -= 2 * pt->delta();
153  _ln[0].vector.x( sin(a) * M_UNIT * 2 );
154  _ln[0].vector.y( cos(a) * M_UNIT * 2 );
155 }
156 
157 
158 // Stage executing the active test
159 //
160 stageCollisionTests::stageCollisionTests(void) {}
161 
162 void stageCollisionTests::onInit()
163 {
164  ::laSystemIntegrator::getEnvironment()->setTitle("Test Mode");
165 
166  _initTest_2();
167 }
168 
169 void stageCollisionTests::onFini() { }
170 void stageCollisionTests::onActivate() {}
171 void stageCollisionTests::onDeactivate() { }
172 
173 void stageCollisionTests::onFrame(laRenderer *pr, laInputManager *pi, laTimer *pt)
174 {
175  // Setup camera
176  //
177  double w = ::laSystemIntegrator::getSettings()->graphics_resolution_w;
178  double h = ::laSystemIntegrator::getSettings()->graphics_resolution_h;
179 
180  if(pi->key(KEY_ESCAPE)) ::laSystemIntegrator::getEnvironment()->terminate();
181  pr->styleBackground( laColor() );
182  pr->modeInterface();
183  pr->modeTexture(M_FALSE);
184  pr->modeLight(M_FALSE);
185  pr->modeBlend(M_FALSE);
186  pr->transScale( laPoint3( w/(M_UNIT * 20), h/( M_UNIT * 15), 1 ) );
187 
188  // Run test
189  //
190  _doTest_2(pr, pi, pt);
191 }
laFont * font(char *strName)
Get a font renderer.
Definition: laRenderer.h:192
void draw(class laRenderer *r, laPoint2 &pos)
Draw the line ( primarily for debug purposes )
Definition: laLine2.cpp:49
virtual void modeTexture(M_BOOL bOn)=0
Enable/disable texturing.
#define M_UNIT
Unit of 1 meter.
void build_v(const laPoint2 &a, const laPoint2 &v)
Build a line from an end-point and a vector.
Definition: laLine2.h:57
2D Point
Definition: laPoint_novec.h:41
void at(double k, laPoint2 *ppt) const
Get point at 1/k lenght (returns in user-specified pointer)
Definition: laLine2.h:81
laPoint2 vector
Vector ( not normalized, so k = [0;1] traces the whole line segment )
Definition: laLine2.h:44
2D line segment
Definition: laLine2.h:40
virtual void modeBlend(M_BOOL bOn)=0
Enable/disable alpha blending (opacity)
Collision Domain.
Dynamic Object Pivot.
Definition: laPivot.h:44
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
laPoint2 origin
Point of origin.
Definition: laLine2.h:43
virtual void modeInterface()=0
Switch to interface rendering (2D projection mode)
virtual void modeLight(M_BOOL bOn)=0
Enable/disable lighting.