Commit 549cce45e261fa60e50858841468bd7affe0f003

Authored by David Steele
1 parent 0baa4c10

Adding dali letters to 2d phys demo

Added "hanging" letters that can pivot about some point
Now, the balls interact with the letters, and fire off
with a huge impulse periodically to bring some life to
the demo!

Change-Id: Ieecacc3b27e4e55174cb86d1075c9b1739ae0bc3
examples/bullet-physics/physics-demo-controller.cpp
... ... @@ -303,8 +303,9 @@ public:
303 303 Vector3 localPivot;
304 304 Vector3 rayPhysicsOrigin;
305 305 Vector3 rayPhysicsEnd;
  306 + Dali::Any nullFilter;
306 307 mPhysicsAdaptor.BuildPickingRay(origin, direction, rayPhysicsOrigin, rayPhysicsEnd);
307   - auto body = scopedAccessor->HitTest(rayPhysicsOrigin, rayPhysicsEnd, localPivot, mOldPickingDistance);
  308 + auto body = scopedAccessor->HitTest(rayPhysicsOrigin, rayPhysicsEnd, nullFilter, localPivot, mOldPickingDistance);
308 309 if(!body.Empty())
309 310 {
310 311 mPickedBody = body.Get<btRigidBody*>();
... ...
examples/chipmunk-physics/README.md
1 1 # Chipmunk Physics Example
2 2  
3 3 This is an example showing how to use Chipmunk2D physics library to create and control physics objects in DALi.
4   -It creates a set of balls which act under gravity
  4 +It creates a set of balls which act under gravity, and the letters of the DALi logo that are each "suspended" from
  5 +a fixed pivot. Every 7 seconds, a random number of the balls are given a high impulse to bring the whole demo back
  6 +to life.
5 7  
6 8 ![](./chipmunk.gif)
7 9  
... ... @@ -9,3 +11,4 @@ It creates a set of balls which act under gravity
9 11 "qe" keys rotate the last touched actor in Z axis
10 12 "p" key resets the position/forces on the last touched actor to the origin
11 13 Space key toggles the integration state.
  14 +"m" key toggles the debug state
... ...
examples/chipmunk-physics/dletter.blend 0 → 100755
No preview for this file type
examples/chipmunk-physics/letter-a.h 0 → 100755
  1 +double letter_a[] = {
  2 +0.080334, -0.445639,
  3 +-0.050940, -0.447993,
  4 +-0.175007, -0.428650,
  5 +-0.275208, -0.394296,
  6 +-0.349133, -0.348910,
  7 +-0.399168, -0.296059,
  8 +-0.429146, -0.239705,
  9 +-0.444190, -0.184144,
  10 +-0.449309, -0.133031,
  11 +-0.447807, -0.088260,
  12 +-0.441110, -0.049854,
  13 +-0.429090, -0.016322,
  14 +-0.411267, 0.013977,
  15 +-0.387297, 0.042290,
  16 +-0.356968, 0.069448,
  17 +-0.320038, 0.095734,
  18 +-0.276016, 0.120705,
  19 +-0.224141, 0.143171,
  20 +-0.163786, 0.161366,
  21 +-0.096207, 0.173662,
  22 +-0.025351, 0.178901,
  23 +0.042247, 0.176440,
  24 +0.099742, 0.166799,
  25 +0.143348, 0.152616,
  26 +0.172828, 0.138790,
  27 +0.191163, 0.131669,
  28 +0.202876, 0.135037,
  29 +0.213151, 0.148009,
  30 +0.227734, 0.165069,
  31 +0.251264, 0.178894,
  32 +0.284487, 0.185042,
  33 +0.323773, 0.182775,
  34 +0.361894, 0.174502,
  35 +0.392529, 0.162594,
  36 +0.412959, 0.147477,
  37 +0.424129, 0.127583,
  38 +0.429248, 0.100184,
  39 +0.431109, 0.063005,
  40 +0.431556, 0.014540,
  41 +0.431537, -0.045757,
  42 +0.431509, -0.116220,
  43 +0.431712, -0.192109,
  44 +0.432178, -0.265634,
  45 +0.432602, -0.328082,
  46 +0.432049, -0.374457,
  47 +0.428895, -0.404558,
  48 +0.420987, -0.422551,
  49 +0.406962, -0.433552,
  50 +0.387266, -0.440988,
  51 +0.364196, -0.446436,
  52 +0.341061, -0.450080,
  53 +0.320084, -0.451858,
  54 +0.301841, -0.451758,
  55 +0.285388, -0.449827,
  56 +0.269461, -0.446152,
  57 +0.253532, -0.440856,
  58 +0.237876, -0.434095,
  59 +0.222830, -0.426206,
  60 +0.206674, -0.418119,
  61 +0.184985, -0.411483,
  62 +};
... ...
examples/chipmunk-physics/letter-d.h 0 → 100755
  1 +double letter_d[] = {
  2 +0.131136, -0.445716,
  3 +0.210390, -0.423237,
  4 +0.262577, -0.399065,
  5 +0.303066, -0.375918,
  6 +0.337334, -0.354157,
  7 +0.351012, -0.340831,
  8 +0.375016, -0.322534,
  9 +0.396490, -0.293488,
  10 +0.416732, -0.270408,
  11 +0.434497, -0.237234,
  12 +0.452636, -0.199979,
  13 +0.462341, -0.155056,
  14 +0.466712, -0.072655,
  15 +0.468917, 0.030611,
  16 +0.469557, 0.141277,
  17 +0.468970, 0.241971,
  18 +0.466592, 0.321481,
  19 +0.463973, 0.370117,
  20 +0.449290, 0.412602,
  21 +0.430514, 0.435231,
  22 +0.405080, 0.449557,
  23 +0.375657, 0.458505,
  24 +0.345982, 0.463514,
  25 +0.318714, 0.465354,
  26 +0.294957, 0.464321,
  27 +0.274400, 0.460291,
  28 +0.256365, 0.453172,
  29 +0.240564, 0.443229,
  30 +0.227141, 0.431093,
  31 +0.216465, 0.417345,
  32 +0.208675, 0.401603,
  33 +0.203567, 0.382294,
  34 +0.200628, 0.356920,
  35 +0.199298, 0.324164,
  36 +0.199170, 0.285514,
  37 +0.200000, 0.245338,
  38 +0.201478, 0.209592,
  39 +0.202679, 0.182716,
  40 +0.201921, 0.166840,
  41 +0.196805, 0.161833,
  42 +0.184565, 0.165839,
  43 +0.162353, 0.175720,
  44 +0.127279, 0.187103,
  45 +0.077452, 0.195250,
  46 +0.014636, 0.197297,
  47 +-0.055010, 0.192870,
  48 +-0.121485, 0.183899,
  49 +-0.176149, 0.172829,
  50 +-0.216299, 0.161038,
  51 +-0.245481, 0.148720,
  52 +-0.271343, 0.135014,
  53 +-0.299749, 0.118339,
  54 +-0.333036, 0.096502,
  55 +-0.370072, 0.066624,
  56 +-0.407117, 0.024452,
  57 +-0.438623, -0.036298,
  58 +-0.457155, -0.123895,
  59 +-0.443017, -0.239761,
  60 +-0.333914, -0.370416,
  61 +-0.238509, -0.415552,
  62 +-0.143103, -0.447210,
  63 +-0.015264, -0.458859,
  64 +};
... ...
examples/chipmunk-physics/letter-i.h 0 → 100755
  1 +double letter_i[] = {
  2 +0.064721, 0.397047,
  3 +0.084359, 0.392005,
  4 +0.106181, 0.384591,
  5 +0.125922, 0.375744,
  6 +0.142219, 0.366343,
  7 +0.154761, 0.356927,
  8 +0.163984, 0.347651,
  9 +0.170804, 0.338367,
  10 +0.175946, 0.328809,
  11 +0.179775, 0.318644,
  12 +0.182339, 0.307542,
  13 +0.177591, 0.258003,
  14 +0.148165, 0.224480,
  15 +0.111786, 0.209128,
  16 +0.092304, 0.197781,
  17 +0.091241, 0.194700,
  18 +0.110808, 0.181851,
  19 +0.144725, 0.163438,
  20 +0.168692, 0.140330,
  21 +0.174015, 0.027886,
  22 +0.174833, -0.060661,
  23 +0.175642, -0.162698,
  24 +0.176388, -0.255385,
  25 +0.176999, -0.322609,
  26 +0.177447, -0.361523,
  27 +0.176470, -0.404438,
  28 +0.156498, -0.440912,
  29 +0.128113, -0.461851,
  30 +0.079571, -0.474461,
  31 +0.032029, -0.476380,
  32 +-0.006020, -0.470540,
  33 +-0.051020, -0.453217,
  34 +-0.078452, -0.436259,
  35 +-0.088338, -0.425531,
  36 +-0.099007, -0.405238,
  37 +-0.104707, -0.324290,
  38 +-0.104555, -0.251683,
  39 +-0.104249, -0.160428,
  40 +-0.103978, -0.069679,
  41 +-0.103846, 0.002851,
  42 +-0.103784, 0.050585,
  43 +-0.103546, 0.078313,
  44 +-0.101804, 0.106063,
  45 +-0.086488, 0.143888,
  46 +-0.047079, 0.166131,
  47 +-0.007868, 0.197018,
  48 +-0.056347, 0.222110,
  49 +-0.084005, 0.244999,
  50 +-0.094406, 0.286331,
  51 +-0.090557, 0.322635,
  52 +-0.086423, 0.332826,
  53 +-0.081092, 0.342019,
  54 +-0.074727, 0.350617,
  55 +-0.067183, 0.359181,
  56 +-0.057354, 0.368154,
  57 +-0.043032, 0.377802,
  58 +-0.021385, 0.387927,
  59 +0.009406, 0.396920,
  60 +0.051950, 0.399662,
  61 +
  62 +};
... ...
examples/chipmunk-physics/letter-l.h 0 → 100755
  1 +double letter_l[] = {
  2 +-0.105263, -0.207440,
  3 +-0.108182, 0.173937,
  4 +-0.109280, 0.299948,
  5 +-0.109283, 0.340490,
  6 +-0.105907, 0.372182,
  7 +-0.104059, 0.380107,
  8 +-0.091231, 0.409253,
  9 +-0.069112, 0.431754,
  10 +-0.039130, 0.446449,
  11 +-0.005093, 0.454614,
  12 +0.029018, 0.457917,
  13 +0.060424, 0.457140,
  14 +0.087614, 0.452122,
  15 +0.110104, 0.442246,
  16 +0.127851, 0.427662,
  17 +0.141076, 0.409636,
  18 +0.150263, 0.390488,
  19 +0.156066, 0.372701,
  20 +0.159670, 0.354752,
  21 +0.160219, 0.303937,
  22 +0.160569, 0.152952,
  23 +0.160810, 0.034914,
  24 +0.160790, -0.095764,
  25 +0.160217, -0.214607,
  26 +0.158889, -0.304397,
  27 +0.156737, -0.360039,
  28 +0.150208, -0.401271,
  29 +0.124974, -0.427020,
  30 +0.078909, -0.447270,
  31 +0.052876, -0.452784,
  32 +0.038706, -0.454157,
  33 +-0.024785, -0.446256,
  34 +-0.075312, -0.415388,
  35 +-0.091657, -0.396699,
  36 +-0.103874, -0.371529,
  37 +};
... ...
examples/chipmunk-physics/physics-demo-controller.cpp
... ... @@ -14,8 +14,8 @@
14 14 * limitations under the License.
15 15 */
16 16  
17   -#include <dali-toolkit/dali-toolkit.h>
18 17 #include <dali-physics/dali-physics.h>
  18 +#include <dali-toolkit/dali-toolkit.h>
19 19  
20 20 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
21 21 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
... ... @@ -23,9 +23,14 @@
23 23 #include <dali/devel-api/events/hit-test-algorithm.h>
24 24 #include <dali/integration-api/debug.h>
25 25  
  26 +#include <chipmunk/chipmunk.h>
26 27 #include <iostream>
27 28 #include <string>
28   -#include <chipmunk/chipmunk.h>
  29 +#include "letter-a.h"
  30 +#include "letter-d.h"
  31 +#include "letter-i.h"
  32 +#include "letter-l.h"
  33 +#include "split-letter-d.h"
29 34  
30 35 using namespace Dali;
31 36 using namespace Dali::Toolkit::Physics;
... ... @@ -34,8 +39,7 @@ using namespace Dali::Toolkit::Physics;
34 39 Debug::Filter* gPhysicsDemo = Debug::Filter::New(Debug::Concise, false, "LOG_PHYSICS_EXAMPLE");
35 40 #endif
36 41  
37   -#define GRABBABLE_MASK_BIT (1u << 31)
38   -cpShapeFilter NOT_GRABBABLE_FILTER = {CP_NO_GROUP, ~GRABBABLE_MASK_BIT, ~GRABBABLE_MASK_BIT};
  42 +const bool DEBUG_STATE{false};
39 43  
40 44 namespace KeyModifier
41 45 {
... ... @@ -53,10 +57,69 @@ enum Key
53 57 };
54 58 }
55 59  
56   -const std::string BRICK_WALL = DEMO_IMAGE_DIR "/brick-wall.jpg";
57   -const std::string BALL_IMAGE = DEMO_IMAGE_DIR "/blocks-ball.png";
  60 +const std::string LETTER_IMAGES[4] = {
  61 + DEMO_IMAGE_DIR "/dali-logo-d.png",
  62 + DEMO_IMAGE_DIR "/dali-logo-a.png",
  63 + DEMO_IMAGE_DIR "/dali-logo-l.png",
  64 + DEMO_IMAGE_DIR "/dali-logo-i.png"};
  65 +
  66 +const std::string BRICK_WALL = DEMO_IMAGE_DIR "/brick-wall.jpg";
  67 +const std::string BALL_IMAGE = DEMO_IMAGE_DIR "/blocks-ball.png";
  68 +
  69 +#if defined(_ARCH_ARM_)
  70 +#define DEMO_ICON_DIR "/usr/share/icons"
  71 +#else
  72 +#define DEMO_ICON_DIR DEMO_IMAGE_DIR
  73 +#endif
  74 +
  75 +const std::string BALL_IMAGES[] = {DEMO_IMAGE_DIR "/blocks-ball.png",
  76 + DEMO_ICON_DIR "/dali-tests.png",
  77 + DEMO_ICON_DIR "/dali-examples.png",
  78 + DEMO_ICON_DIR "/com.samsung.dali-demo.png"};
  79 +
  80 +const std::string LOGO = DEMO_IMAGE_DIR "/Logo-for-demo.png";
58 81 const std::string BRICK_URIS[4] = {
59   - DEMO_IMAGE_DIR "/blocks-brick-1.png", DEMO_IMAGE_DIR "/blocks-brick-2.png", DEMO_IMAGE_DIR "/blocks-brick-3.png", DEMO_IMAGE_DIR "/blocks-brick-4.png"};
  82 + DEMO_IMAGE_DIR "/blocks-brick-1.png",
  83 + DEMO_IMAGE_DIR "/blocks-brick-2.png",
  84 + DEMO_IMAGE_DIR "/blocks-brick-3.png",
  85 + DEMO_IMAGE_DIR "/blocks-brick-4.png"};
  86 +
  87 +using Verts = double*;
  88 +
  89 +const Verts LETTER_VERTICES[] = {&letter_d0[0], &letter_d1[0], &letter_a[0], &letter_l[0], &letter_i[0]};
  90 +//const Verts LETTER_VERTS[4] = {&letter_d[0], &letter_a[0], &letter_l[0], &letter_i[0]};
  91 +const size_t NUMBER_OF_VERTICES[] = {
  92 + sizeof(letter_d0) / (2 * sizeof(double)),
  93 + sizeof(letter_d1) / (2 * sizeof(double)),
  94 + sizeof(letter_a) / (2 * sizeof(double)),
  95 + sizeof(letter_l) / (2 * sizeof(double)),
  96 + sizeof(letter_i) / (2 * sizeof(double))};
  97 +
  98 +// Indexed by letter - index into VERTICES / NUMBER_OF_VERTICES arrays
  99 +const std::vector<int> LETTER_SHAPE_INDEXES[]{{0, 1}, {2}, {3}, {4}};
  100 +
  101 +// Groups that can collide with each other:
  102 +const cpGroup BALL_GROUP{1 << 0};
  103 +const cpGroup LETTER_GROUP_1{1 << 1};
  104 +const cpGroup LETTER_GROUP_2{1 << 2};
  105 +const cpGroup LETTER_GROUP_3{1 << 3};
  106 +const cpGroup LETTER_GROUP_4{1 << 4};
  107 +const cpGroup BOUNDS_GROUP{1 << 5};
  108 +
  109 +const cpBitmask COLLISION_MASK{0x3F};
  110 +
  111 +const cpBitmask BALL_COLLIDES_WITH{BALL_GROUP | LETTER_GROUP_1 | LETTER_GROUP_2 | LETTER_GROUP_3 | LETTER_GROUP_4 | BOUNDS_GROUP};
  112 +const cpBitmask LETTER_1_COLLIDES_WITH{BALL_GROUP | LETTER_GROUP_2 | BOUNDS_GROUP};
  113 +const cpBitmask LETTER_2_COLLIDES_WITH{BALL_GROUP | LETTER_GROUP_1 | LETTER_GROUP_3};
  114 +const cpBitmask LETTER_3_COLLIDES_WITH{BALL_GROUP | LETTER_GROUP_2 | LETTER_GROUP_4};
  115 +const cpBitmask LETTER_4_COLLIDES_WITH{BALL_GROUP | LETTER_GROUP_3 | BOUNDS_GROUP};
  116 +
  117 +static cpFloat SpringForce(cpConstraint* spring, cpFloat distance)
  118 +{
  119 + cpFloat clamp = 20.0f;
  120 + return cpfclamp(cpDampedSpringGetRestLength(spring) - distance, -clamp, clamp) *
  121 + cpDampedSpringGetStiffness(spring);
  122 +}
60 123  
61 124 /**
62 125 * @brief The physics demo using Chipmunk2D APIs.
... ... @@ -92,61 +155,189 @@ public:
92 155 0.0f));
93 156  
94 157 mPhysicsAdaptor = PhysicsAdaptor::New(mPhysicsTransform, windowSize);
95   - mPhysicsRoot = mPhysicsAdaptor.GetRootActor();
  158 + mPhysicsRoot = mPhysicsAdaptor.GetRootActor();
96 159 mPhysicsRoot.TouchedSignal().Connect(this, &PhysicsDemoController::OnTouched);
97 160  
98 161 mWindow.Add(mPhysicsRoot);
  162 + mPopcornTimer = Timer::New(7000);
  163 + mPopcornTimer.TickSignal().Connect(this, &PhysicsDemoController::OnPopcornTick);
  164 + mPopcornTimer.Start();
99 165  
100   - auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
101   - cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
  166 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  167 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
102 168  
103 169 CreateBounds(space, windowSize);
  170 +
104 171 // Ball area = 2*PI*26^2 ~= 6.28*26*26 ~= 5400
105   - // Fill quarter of the screen...
  172 + // Fill top quarter of the screen...
106 173 int numBalls = 10 + windowSize.GetWidth() * windowSize.GetHeight() / 20000;
107 174 for(int i = 0; i < numBalls; ++i)
108 175 {
109   - CreateBall(space);
  176 + mBalls.push_back(CreateBall(space));
110 177 }
  178 + //AddSprings(space);
  179 + //CreateLogo(space);
  180 + CreateLetters(space);
111 181  
112 182 // For funky mouse drag
113 183 mMouseBody = cpBodyNewKinematic(); // Mouse actor is a kinematic body that is not integrated
114 184  
115 185 // Process any async queued methods next frame
116 186 mPhysicsAdaptor.CreateSyncPoint();
  187 +
  188 + if(DEBUG_STATE)
  189 + {
  190 + mPhysicsDebugLayer = mPhysicsAdaptor.CreateDebugLayer(mWindow);
  191 + mPhysicsAdaptor.SetDebugState(PhysicsAdaptor::DebugState::ON);
  192 + }
117 193 }
118 194  
119   - void CreateBall(cpSpace* space)
  195 + PhysicsActor CreateBall(cpSpace* space)
120 196 {
121 197 const float BALL_MASS = 10.0f;
122   - const float BALL_RADIUS = 26.0f;
  198 + const float BALL_RADIUS = 13.0f;
123 199 const float BALL_ELASTICITY = 0.5f;
124 200 const float BALL_FRICTION = 0.5f;
125 201  
126   - auto ball = Toolkit::ImageView::New(BALL_IMAGE);
127   -
128   - cpBody* body = cpSpaceAddBody(space, cpBodyNew(BALL_MASS, cpMomentForCircle(BALL_MASS, 0.0f, BALL_RADIUS, cpvzero)));
  202 + auto ball = Toolkit::ImageView::New(BALL_IMAGES[rand() % 4]);
  203 + ball[Actor::Property::NAME] = "Ball";
  204 + ball[Actor::Property::SIZE] = Vector2(26, 26); // Halve the image size
  205 + cpBody* body = cpSpaceAddBody(space, cpBodyNew(BALL_MASS, cpMomentForCircle(BALL_MASS, 0.0f, BALL_RADIUS, cpvzero)));
129 206  
130 207 cpShape* shape = cpSpaceAddShape(space, cpCircleShapeNew(body, BALL_RADIUS, cpvzero));
131 208 cpShapeSetElasticity(shape, BALL_ELASTICITY);
132 209 cpShapeSetFriction(shape, BALL_FRICTION);
133   -
  210 + //cpShapeSetFilter(shape, cpShapeFilterNew(BALL_GROUP, BALL_COLLIDES_WITH, COLLISION_MASK));
134 211 ball.RegisterProperty("uBrightness", 0.0f);
135 212  
136 213 PhysicsActor physicsBall = mPhysicsAdaptor.AddActorBody(ball, body);
137 214  
138   - Window::WindowSize windowSize = mWindow.GetSize();
  215 + Window::WindowSize windowSize = mWindow.GetSize();
139 216  
140   - const float fw = 0.5f*(windowSize.GetWidth() - BALL_RADIUS);
141   - const float fh = 0.5f*(windowSize.GetHeight() - BALL_RADIUS);
  217 + const float fw = 0.5f * (windowSize.GetWidth() - BALL_RADIUS);
  218 + const float fh = 0.5f * (windowSize.GetHeight() - BALL_RADIUS);
142 219  
143 220 // Example of setting physics property on update thread
144   - physicsBall.AsyncSetPhysicsPosition(Vector3(Random::Range(-fw, fw), Random::Range(-fh, fh), 0.0f));
  221 + physicsBall.AsyncSetPhysicsPosition(Vector3(Random::Range(-fw, fw), Random::Range(-fh, -fh * 0.5), 0.0f));
145 222  
146 223 // Example of queuing a chipmunk method to run on the update thread
147   - mPhysicsAdaptor.Queue([body](){
  224 + mPhysicsAdaptor.Queue([body]() {
148 225 cpBodySetVelocity(body, cpv(Random::Range(-100.0, 100.0), Random::Range(-100.0, 100.0)));
149 226 });
  227 + return physicsBall;
  228 + }
  229 +
  230 + void CreateLogo(cpSpace* space)
  231 + {
  232 + const float MASS = 20.0f;
  233 + auto logo = Toolkit::ImageView::New(LOGO);
  234 + Vector2 logoSize{368, 208};
  235 + logo[Actor::Property::SIZE] = logoSize; // Double in size
  236 +
  237 + cpBody* logoBody = cpSpaceAddBody(space, cpBodyNew(MASS, cpMomentForBox(MASS, logoSize.width, logoSize.height)));
  238 + cpShape* logoShape = cpSpaceAddShape(space, cpBoxShapeNew(logoBody, logoSize.width, logoSize.height, 0.0));
  239 + PhysicsActor logoActor = mPhysicsAdaptor.AddActorBody(logo, logoBody);
  240 +
  241 + cpShapeSetFriction(logoShape, 0.9);
  242 + cpShapeSetElasticity(logoShape, 0.0);
  243 + Window::WindowSize windowSize = mWindow.GetSize();
  244 + Vector3 daliPos(0, -windowSize.GetHeight() / 2 + logoSize.height * 1.3, 0);
  245 + Vector3 physPos = mPhysicsAdaptor.TranslateToPhysicsSpace(daliPos);
  246 + cpBodySetPosition(logoBody, cpv(physPos.x, physPos.y));
  247 +
  248 + cpBody* staticBody = cpSpaceGetStaticBody(space);
  249 + cpConstraint* spring = NewSpring(staticBody, logoBody, cpv(0, 0), cpv(0, logoSize.height / 2));
  250 + cpSpaceAddConstraint(space, spring);
  251 + }
  252 +
  253 + void CreateLetters(cpSpace* space)
  254 + {
  255 + const float LETTER_MASS = 10.0f;
  256 + const float RADIUS = 85.0f;
  257 + const float ELASTICITY = 0.0f;
  258 + const float FRICTION = 0.9f;
  259 +
  260 + static const cpShapeFilter FILTERS[4] = {
  261 + cpShapeFilterNew(LETTER_GROUP_1, LETTER_1_COLLIDES_WITH, COLLISION_MASK),
  262 + cpShapeFilterNew(LETTER_GROUP_2, LETTER_2_COLLIDES_WITH, COLLISION_MASK),
  263 + cpShapeFilterNew(LETTER_GROUP_3, LETTER_3_COLLIDES_WITH, COLLISION_MASK),
  264 + cpShapeFilterNew(LETTER_GROUP_4, LETTER_4_COLLIDES_WITH, COLLISION_MASK)};
  265 +
  266 + static const std::string NAME[4] = {"d", "a", "l", "i"};
  267 + for(int index = 0; index < 4; ++index)
  268 + {
  269 + auto letter = Toolkit::ImageView::New(LETTER_IMAGES[index]);
  270 + letter[Actor::Property::NAME] = NAME[index];
  271 +
  272 + cpBody* body = cpSpaceAddBody(space, cpBodyNew(LETTER_MASS, cpMomentForCircle(LETTER_MASS, 0.0f, RADIUS, cpvzero)));
  273 +
  274 + for(size_t letterShapeIndex = 0; letterShapeIndex < LETTER_SHAPE_INDEXES[index].size(); ++letterShapeIndex)
  275 + {
  276 + size_t shapeIndex = LETTER_SHAPE_INDEXES[index][letterShapeIndex];
  277 +
  278 + std::vector<cpVect> scaledVerts;
  279 + size_t numberOfElements = NUMBER_OF_VERTICES[shapeIndex];
  280 + scaledVerts.resize(numberOfElements);
  281 + for(size_t i = 0; i < numberOfElements; ++i)
  282 + {
  283 + double x = LETTER_VERTICES[shapeIndex][i * 2 + 0];
  284 + double y = LETTER_VERTICES[shapeIndex][i * 2 + 1];
  285 + scaledVerts[i] = cpv(x * 122.0f, y * 171.0f); // Verts are normalized to +-0.5
  286 + }
  287 + cpFloat bevel = 1.0;
  288 +
  289 + cpShape* shape = cpSpaceAddShape(space, cpPolyShapeNew(body, numberOfElements, &scaledVerts[0], cpTransformIdentity, bevel));
  290 + cpShapeSetElasticity(shape, ELASTICITY);
  291 + cpShapeSetFriction(shape, FRICTION);
  292 + cpShapeSetFilter(shape, FILTERS[index]);
  293 + }
  294 +
  295 + PhysicsActor physicsLetter = mPhysicsAdaptor.AddActorBody(letter, body);
  296 +
  297 + Window::WindowSize windowSize = mWindow.GetSize();
  298 +
  299 + // Image is 326x171; center of letter is guessed; each image contains only 1 image.
  300 + // Position the letters into the window
  301 +
  302 + float cellW = (windowSize.GetWidth() - 170) / 4;
  303 + float cellC = -windowSize.GetWidth() * 0.5f + cellW * (0.5f + index);
  304 + float x = 85 + cellC; // - 61.0f;
  305 + Vector3 physPos = mPhysicsAdaptor.TranslateToPhysicsSpace(Vector3(x, 0, 0.0f));
  306 +
  307 + // Have to set position before setting constraint
  308 + cpBodySetPosition(body, cpv(physPos.x, physPos.y));
  309 +
  310 + // Add a fixed pivot at top of shape
  311 + cpBody* staticBody = cpSpaceGetStaticBody(space);
  312 +
  313 + Vector3 localPivot(x, -70.0f, 0.0f);
  314 + Vector3 pivotPhys = mPhysicsAdaptor.TranslateToPhysicsSpace(localPivot);
  315 + cpSpaceAddConstraint(space, cpPivotJointNew(staticBody, body, cpv(pivotPhys.x, pivotPhys.y)));
  316 + }
  317 + }
  318 +
  319 + cpConstraint* NewSpring(cpBody* body1, cpBody* body2, cpVect anchor1, cpVect anchor2)
  320 + {
  321 + const cpFloat STIFFNESS{100.0f};
  322 + const cpFloat DAMPING{0.5f};
  323 + cpConstraint* spring = cpDampedSpringNew(body1, body2, anchor1, anchor2, 0.0f, STIFFNESS, DAMPING);
  324 + cpDampedSpringSetSpringForceFunc(spring, SpringForce);
  325 + return spring;
  326 + }
  327 +
  328 + void AddSprings(cpSpace* space)
  329 + {
  330 + int N = mBalls.size();
  331 + int randValue = 3 + rand() % (N / 4); // Some number of pairs
  332 + for(int i = 0; i < randValue; ++i)
  333 + {
  334 + int randIndex = rand() % N;
  335 + cpBody* body1 = mBalls[randIndex].GetBody().Get<cpBody*>();
  336 + cpBody* body2 = mBalls[(randIndex + 1) % N].GetBody().Get<cpBody*>();
  337 +
  338 + cpConstraint* spring = NewSpring(body1, body2, cpv(25, 0), cpv(-25, 0));
  339 + cpSpaceAddConstraint(space, spring);
  340 + }
150 341 }
151 342  
152 343 void CreateBounds(cpSpace* space, Window::WindowSize size)
... ... @@ -180,7 +371,7 @@ public:
180 371 cpShapeSetElasticity(shape, 1.0f);
181 372 cpShapeSetFriction(shape, 1.0f);
182 373  
183   - cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
  374 + cpShapeSetFilter(shape, cpShapeFilterNew(BOUNDS_GROUP, COLLISION_MASK, COLLISION_MASK));
184 375 return shape;
185 376 }
186 377  
... ... @@ -209,12 +400,30 @@ public:
209 400  
210 401 void OnWindowResize(Window window, Window::WindowSize newSize)
211 402 {
212   - auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
213   - cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
  403 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  404 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
214 405  
215 406 CreateBounds(space, newSize);
216 407 }
217 408  
  409 + bool OnPopcornTick()
  410 + {
  411 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  412 +
  413 + // fire off N random balls upwards with a high impulse
  414 + int N = mBalls.size();
  415 + int randValue = 10 + rand() % (N / 2);
  416 +
  417 + for(int i = 0; i < randValue; ++i)
  418 + {
  419 + int randIndex = rand() % N;
  420 + cpBody* body = mBalls[randIndex].GetBody().Get<cpBody*>();
  421 + cpBodyActivate(body);
  422 + cpBodyApplyImpulseAtLocalPoint(body, cpv(rand() % 200 - 100, -10000), cpv(0, 25));
  423 + }
  424 + return true;
  425 + }
  426 +
218 427 bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch)
219 428 {
220 429 static enum {
... ... @@ -225,7 +434,7 @@ public:
225 434 auto renderTask = mWindow.GetRenderTaskList().GetTask(0);
226 435 auto screenCoords = touch.GetScreenPosition(0);
227 436 // In this demo, physics space is equivalent to screen space with y inverted
228   - auto windowSize = mWindow.GetSize();
  437 + auto windowSize = mWindow.GetSize();
229 438 Vector3 rayPhysicsOrigin(screenCoords.x, windowSize.GetHeight() - screenCoords.y, 0.0f);
230 439  
231 440 switch(state)
... ... @@ -236,17 +445,24 @@ public:
236 445 {
237 446 state = MovePivot;
238 447  
239   - auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
240   - cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
  448 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  449 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
241 450  
242 451 Vector3 localPivot;
243 452 float pickingDistance;
244 453  
245   - auto body = scopedAccessor->HitTest(rayPhysicsOrigin, rayPhysicsOrigin, localPivot, pickingDistance);
  454 + cpShapeFilter ballFilter{CP_NO_GROUP, 1u << 31, 1u << 31};
  455 + auto body = scopedAccessor->HitTest(rayPhysicsOrigin, rayPhysicsOrigin, ballFilter, localPivot, pickingDistance);
  456 + if(body.Empty())
  457 + {
  458 + cpShapeFilter letterFilter{CP_NO_GROUP, COLLISION_MASK, COLLISION_MASK};
  459 + body = scopedAccessor->HitTest(rayPhysicsOrigin, rayPhysicsOrigin, letterFilter, localPivot, pickingDistance);
  460 + }
246 461 if(!body.Empty())
247 462 {
248   - mPickedBody = body.Get<cpBody*>();
  463 + mPickedBody = body.Get<cpBody*>();
249 464 mSelectedActor = mPhysicsAdaptor.GetPhysicsActor(mPickedBody);
  465 + std::cout << "PhysicsActor: " << mPhysicsAdaptor.GetRootActor().FindChildById(mSelectedActor.GetId()).GetProperty<std::string>(Actor::Property::NAME) << std::endl;
250 466  
251 467 mPickedSavedState = cpBodyIsSleeping(mPickedBody);
252 468 cpBodyActivate(mPickedBody);
... ... @@ -273,8 +489,8 @@ public:
273 489 {
274 490 if(mPickedConstraint)
275 491 {
276   - auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
277   - cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
  492 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  493 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
278 494  
279 495 if(mPickedSavedState)
280 496 {
... ... @@ -296,7 +512,6 @@ public:
296 512 }
297 513 }
298 514  
299   -
300 515 Stage::GetCurrent().KeepRendering(30.0f);
301 516  
302 517 return true;
... ... @@ -305,6 +520,7 @@ public:
305 520 void OnKeyEv(const Dali::KeyEvent& event)
306 521 {
307 522 static bool integrateState{true};
  523 + static bool debugState{DEBUG_STATE};
308 524  
309 525 if(event.GetState() == KeyEvent::DOWN)
310 526 {
... ... @@ -336,11 +552,17 @@ public:
336 552 }
337 553 else if(!event.GetKeyString().compare(" "))
338 554 {
339   - integrateState = true^integrateState;
340   - mPhysicsAdaptor.SetIntegrationState(integrateState?
341   - PhysicsAdaptor::IntegrationState::ON:
342   - PhysicsAdaptor::IntegrationState::OFF);
343   -
  555 + integrateState = true ^ integrateState;
  556 + mPhysicsAdaptor.SetIntegrationState(integrateState ? PhysicsAdaptor::IntegrationState::ON : PhysicsAdaptor::IntegrationState::OFF);
  557 + }
  558 + else if(!event.GetKeyString().compare("m"))
  559 + {
  560 + debugState = true ^ debugState;
  561 + if(debugState && !mPhysicsDebugLayer)
  562 + {
  563 + mPhysicsDebugLayer = mPhysicsAdaptor.CreateDebugLayer(mWindow);
  564 + }
  565 + mPhysicsAdaptor.SetDebugState(debugState ? PhysicsAdaptor::DebugState::ON : PhysicsAdaptor::DebugState::OFF);
344 566 }
345 567 else if(!event.GetKeyString().compare("w"))
346 568 {
... ... @@ -391,10 +613,13 @@ public:
391 613 // Rotate anti-clockwise
392 614 if(mSelectedActor)
393 615 {
394   - Quaternion quaternion = mSelectedActor.GetActorRotation();
395   - quaternion *= Quaternion(Degree(-15.0f), Vector3::ZAXIS);
396   - mSelectedActor.AsyncSetPhysicsRotation(quaternion);
397   - cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  616 + // A negative angle should rotate anti-clockwise, which it does,
  617 + // BUT, we mirror in Y axis, so actually, it LOOKS like it rotates clockwise.
  618 + // So, we have to invert angle.
  619 +
  620 + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  621 + float angle = cpBodyGetAngle(body);
  622 + mPhysicsAdaptor.Queue([body, angle]() { cpBodySetAngle(body, angle + Math::PI / 12.0f); });
398 623 mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); });
399 624 mPhysicsAdaptor.CreateSyncPoint();
400 625 }
... ... @@ -404,9 +629,9 @@ public:
404 629 // Rotate clockwise using native physics APIs
405 630 if(mSelectedActor)
406 631 {
407   - cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
408   - float angle = cpBodyGetAngle(body);
409   - mPhysicsAdaptor.Queue([body, angle]() { cpBodySetAngle(body, angle-Math::PI/12.0f); });
  632 + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  633 + float angle = cpBodyGetAngle(body);
  634 + mPhysicsAdaptor.Queue([body, angle]() { cpBodySetAngle(body, angle - Math::PI / 12.0f); });
410 635 mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); });
411 636 mPhysicsAdaptor.CreateSyncPoint();
412 637 }
... ... @@ -445,14 +670,19 @@ private:
445 670 Application& mApplication;
446 671 Window mWindow;
447 672  
448   - PhysicsAdaptor mPhysicsAdaptor;
449   - PhysicsActor mSelectedActor;
450   - Matrix mPhysicsTransform;
451   - Actor mPhysicsRoot;
452   - cpBody* mMouseBody{nullptr};
453   - cpBody* mPickedBody{nullptr};
454   - cpConstraint* mPickedConstraint{nullptr};
455   - int mPickedSavedState = -1; /// 0 : Active, 1 : Sleeping
  673 + PhysicsAdaptor mPhysicsAdaptor;
  674 + PhysicsActor mSelectedActor;
  675 + std::vector<PhysicsActor> mBalls;
  676 + Matrix mPhysicsTransform;
  677 + Actor mPhysicsRoot;
  678 + Layer mPhysicsDebugLayer;
  679 + cpBody* mMouseBody{nullptr};
  680 + cpBody* mPickedBody{nullptr};
  681 + cpConstraint* mPickedConstraint{nullptr};
  682 + int mPickedSavedState = -1; /// 0 : Active, 1 : Sleeping
  683 + Timer mPopcornTimer;
  684 +
  685 + PhysicsAdaptor::DebugState mDebugState{PhysicsAdaptor::DebugState::OFF};
456 686  
457 687 cpShape* mLeftBound{nullptr};
458 688 cpShape* mRightBound{nullptr};
... ...
examples/chipmunk-physics/split-letter-d.h 0 → 100644
  1 +double letter_d0[] = {
  2 +0.131136, -0.445716,
  3 +0.210390, -0.423237,
  4 +0.262577, -0.399065,
  5 +0.303066, -0.375918,
  6 +0.337334, -0.354157,
  7 +0.351012, -0.340831,
  8 +0.375016, -0.322534,
  9 +0.396490, -0.293488,
  10 +0.416732, -0.270408,
  11 +0.434497, -0.237234,
  12 +0.201921, 0.161833,
  13 +0.184565, 0.165839,
  14 +0.162353, 0.175720,
  15 +0.127279, 0.187103,
  16 +0.077452, 0.195250,
  17 +0.014636, 0.197297,
  18 +-0.055010, 0.192870,
  19 +-0.121485, 0.183899,
  20 +-0.176149, 0.172829,
  21 +-0.216299, 0.161038,
  22 +-0.245481, 0.148720,
  23 +-0.271343, 0.135014,
  24 +-0.299749, 0.118339,
  25 +-0.333036, 0.096502,
  26 +-0.370072, 0.066624,
  27 +-0.407117, 0.024452,
  28 +-0.438623, -0.036298,
  29 +-0.457155, -0.123895,
  30 +-0.443017, -0.239761,
  31 +-0.333914, -0.370416,
  32 +-0.238509, -0.415552,
  33 +-0.143103, -0.447210,
  34 +-0.015264, -0.458859,
  35 +};
  36 +double letter_d1[] = {
  37 +0.201921, 0.166840,
  38 +0.202679, 0.182716,
  39 +0.201478, 0.209592,
  40 +0.200000, 0.245338,
  41 +0.199170, 0.285514,
  42 +0.199298, 0.324164,
  43 +0.200628, 0.356920,
  44 +0.203567, 0.382294,
  45 +0.208675, 0.401603,
  46 +0.216465, 0.417345,
  47 +0.227141, 0.431093,
  48 +0.240564, 0.443229,
  49 +0.256365, 0.453172,
  50 +0.274400, 0.460291,
  51 +0.294957, 0.464321,
  52 +0.318714, 0.465354,
  53 +0.345982, 0.463514,
  54 +0.375657, 0.458505,
  55 +0.405080, 0.449557,
  56 +0.430514, 0.435231,
  57 +0.449290, 0.412602,
  58 +0.463973, 0.370117,
  59 +0.466592, 0.321481,
  60 +0.468970, 0.241971,
  61 +0.469557, 0.141277,
  62 +0.468917, 0.030611,
  63 +0.466712, -0.072655,
  64 +0.462341, -0.155056,
  65 +0.452636, -0.199979,
  66 +0.434497, -0.237234,
  67 +0.201921, 0.161833,
  68 +};
... ...
resources/images/dali-logo-a.png 0 → 100644

19.2 KB

resources/images/dali-logo-d.png 0 → 100644

13.6 KB

resources/images/dali-logo-i.png 0 → 100644

14.9 KB

resources/images/dali-logo-l.png 0 → 100644

14.6 KB