Add PhysicsEffects to Extras. The build is only tested on Windows and Android.

The Android/NEON optimized version of Physics Effects is thanks to Graham Rhodes and Anthony Hamilton, See Issue 587
This commit is contained in:
erwin.coumans
2012-03-05 04:59:58 +00:00
parent 6cf8dfc202
commit a93a661b94
462 changed files with 86626 additions and 0 deletions

View File

@@ -0,0 +1,202 @@
/*
Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.
All rights reserved.
Physics Effects is open software; you can redistribute it and/or
modify it under the terms of the BSD License.
Physics Effects 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 BSD License for more details.
A copy of the BSD License is distributed with
Physics Effects under the filename: physics_effects_license.txt
*/
#define BarrelVtxCount 57
#define BarrelIdxCount 180
static float BarrelVtx[] = {
0.0f,-0.5f,0.0f, 0.0f,-1.0f,0.0f,
0.282362f,-0.5f,-0.205148f, 0.0f,-1.0f,0.0f,
0.349018f,-0.5f,0.0f, 0.0f,-1.0f,0.0f,
0.107853f,-0.5f,-0.331936f, 0.0f,-1.0f,0.0f,
-0.107853f,-0.5f,-0.331936f, 0.0f,-1.0f,0.0f,
0.107853f,-0.5f,-0.331936f, 0.0f,-1.0f,0.0f,
-0.282362f,-0.5f,-0.205148f, 0.0f,-1.0f,0.0f,
-0.349018f,-0.5f,0.0f, 0.0f,-1.0f,0.0f,
-0.282362f,-0.5f,0.205148f, 0.0f,-1.0f,0.0f,
-0.107853f,-0.5f,0.331936f, 0.0f,-1.0f,0.0f,
0.107853f,-0.5f,0.331936f, 0.0f,-1.0f,0.0f,
0.282362f,-0.5f,0.205148f, 0.0f,-1.0f,0.0f,
0.0f,0.5f,0.0f, 0.0f,1.0f,0.0f,
0.349018f,0.5f,0.0f, 0.0f,1.0f,0.0f,
0.282362f,0.5f,-0.205148f, 0.0f,1.0f,0.0f,
0.107853f,0.5f,-0.331936f, 0.0f,1.0f,0.0f,
0.107853f,0.5f,-0.331936f, 0.0f,1.0f,0.0f,
-0.107853f,0.5f,-0.331936f, 0.0f,1.0f,0.0f,
-0.282362f,0.5f,-0.205148f, 0.0f,1.0f,0.0f,
-0.349018f,0.5f,0.0f, 0.0f,1.0f,0.0f,
-0.282362f,0.5f,0.205148f, 0.0f,1.0f,0.0f,
-0.107853f,0.5f,0.331936f, 0.0f,1.0f,0.0f,
0.107853f,0.5f,0.331936f, 0.0f,1.0f,0.0f,
0.282362f,0.5f,0.205148f, 0.0f,1.0f,0.0f,
0.349018f,-0.5f,0.0f, 0.957307f,-0.289072f,0.0f,
0.404509f,0.0f,-0.293893f, 0.809017f,0.0f,-0.587785f,
0.5f,0.0f,0.0f, 1.0f,0.0f,0.0f,
0.282362f,-0.5f,-0.205148f, 0.774478f,-0.289072f,-0.562691f,
0.154508f,0.0f,-0.475528f, 0.309017f,0.0f,-0.951057f,
0.107853f,-0.5f,-0.331936f, 0.295824f,-0.289072f,-0.910453f,
0.107853f,-0.5f,-0.331936f, 0.295824f,-0.289072f,-0.910453f,
-0.154509f,0.0f,-0.475528f, -0.309017f,0.0f,-0.951057f,
0.154508f,0.0f,-0.475528f, 0.309017f,0.0f,-0.951057f,
-0.107853f,-0.5f,-0.331936f, -0.295824f,-0.289072f,-0.910453f,
-0.404509f,0.0f,-0.293893f, -0.809017f,0.0f,-0.587785f,
-0.282362f,-0.5f,-0.205148f, -0.774478f,-0.289072f,-0.562691f,
-0.5f,0.0f,0.0f, -1.0f,0.0f,0.0f,
-0.349018f,-0.5f,0.0f, -0.957307f,-0.289072f,0.0f,
-0.404508f,0.0f,0.293893f, -0.809017f,0.0f,0.587785f,
-0.282362f,-0.5f,0.205148f, -0.774478f,-0.289072f,0.562691f,
-0.154509f,0.0f,0.475528f, -0.309017f,0.0f,0.951056f,
-0.107853f,-0.5f,0.331936f, -0.295824f,-0.289072f,0.910453f,
0.154509f,0.0f,0.475528f, 0.309017f,0.0f,0.951056f,
0.107853f,-0.5f,0.331936f, 0.295824f,-0.289072f,0.910453f,
0.404509f,0.0f,0.293892f, 0.809017f,0.0f,0.587785f,
0.282362f,-0.5f,0.205148f, 0.774478f,-0.289072f,0.562691f,
0.282362f,0.5f,-0.205148f, 0.774478f,0.289072f,-0.562691f,
0.349018f,0.5f,0.0f, 0.957307f,0.289072f,0.0f,
0.107853f,0.5f,-0.331936f, 0.295824f,0.289072f,-0.910453f,
-0.107853f,0.5f,-0.331936f, -0.295824f,0.289072f,-0.910453f,
0.107853f,0.5f,-0.331936f, 0.295824f,0.289072f,-0.910453f,
-0.282362f,0.5f,-0.205148f, -0.774478f,0.289072f,-0.562691f,
-0.349018f,0.5f,0.0f, -0.957307f,0.289072f,0.0f,
-0.282362f,0.5f,0.205148f, -0.774478f,0.289072f,0.562691f,
-0.107853f,0.5f,0.331936f, -0.295824f,0.289072f,0.910453f,
0.107853f,0.5f,0.331936f, 0.295824f,0.289072f,0.910453f,
0.282362f,0.5f,0.205148f, 0.774478f,0.289072f,0.562691f,
};
static float BarrelTex[] = {
0.0f,0.5f,
0.0f,0.85f,
0.0f,0.75f,
0.0f,0.95f,
0.0f,0.05f,
0.0f,-0.05f,
0.0f,0.15f,
0.0f,0.25f,
0.0f,0.35f,
0.0f,0.45f,
0.0f,0.55f,
0.0f,0.65f,
1.0f,0.5f,
1.0f,0.75f,
1.0f,0.85f,
1.0f,0.95f,
1.0f,-0.05f,
1.0f,0.05f,
1.0f,0.15f,
1.0f,0.25f,
1.0f,0.35f,
1.0f,0.45f,
1.0f,0.55f,
1.0f,0.65f,
0.0f,0.75f,
0.5f,0.85f,
0.5f,0.75f,
0.0f,0.85f,
0.5f,0.95f,
0.0f,0.95f,
0.0f,-0.05f,
0.5f,0.05f,
0.5f,-0.05f,
0.0f,0.05f,
0.5f,0.15f,
0.0f,0.15f,
0.5f,0.25f,
0.0f,0.25f,
0.5f,0.35f,
0.0f,0.35f,
0.5f,0.45f,
0.0f,0.45f,
0.5f,0.55f,
0.0f,0.55f,
0.5f,0.65f,
0.0f,0.65f,
1.0f,0.85f,
1.0f,0.75f,
1.0f,0.95f,
1.0f,0.05f,
1.0f,-0.05f,
1.0f,0.15f,
1.0f,0.25f,
1.0f,0.35f,
1.0f,0.45f,
1.0f,0.55f,
1.0f,0.65f,
};
static unsigned short BarrelIdx[] = {
0,1,2,
0,3,1,
0,4,5,
0,6,4,
0,7,6,
0,8,7,
0,9,8,
0,10,9,
0,11,10,
0,2,11,
12,13,14,
12,14,15,
12,16,17,
12,17,18,
12,18,19,
12,19,20,
12,20,21,
12,21,22,
12,22,23,
12,23,13,
24,25,26,
24,27,25,
27,28,25,
27,29,28,
30,31,32,
30,33,31,
33,34,31,
33,35,34,
35,36,34,
35,37,36,
37,38,36,
37,39,38,
39,40,38,
39,41,40,
41,42,40,
41,43,42,
43,44,42,
43,45,44,
45,26,44,
45,24,26,
26,46,47,
26,25,46,
25,48,46,
25,28,48,
32,49,50,
32,31,49,
31,51,49,
31,34,51,
34,52,51,
34,36,52,
36,53,52,
36,38,53,
38,54,53,
38,40,54,
40,55,54,
40,42,55,
42,56,55,
42,44,56,
44,47,56,
44,26,47,
};

View File

@@ -0,0 +1,318 @@
/*
Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.
All rights reserved.
Physics Effects is open software; you can redistribute it and/or
modify it under the terms of the BSD License.
Physics Effects 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 BSD License for more details.
A copy of the BSD License is distributed with
Physics Effects under the filename: physics_effects_license.txt
*/
#define LargeMeshVtxCount 81
#define LargeMeshIdxCount 384
static float LargeMeshVtx[] = {
-25.0f,-2.19407f,18.5625f, 0.232976f,0.967754f,0.0957829f,
-25.0f,0.0644502f,24.75f, 0.188094f,0.871343f,-0.45319f,
-18.75f,-4.50287f,18.5625f, -0.0716098f,0.995449f,0.0628767f,
-18.75f,-0.39999f,24.75f, -0.0602794f,0.895848f,-0.440253f,
-12.5f,-1.2166f,18.5625f, -0.269469f,0.961403f,0.0555879f,
-12.5f,-0.942431f,24.75f, -0.0997841f,0.993258f,0.0589987f,
-6.25f,1.93598f,18.5625f, -0.283933f,0.956351f,-0.0691025f,
-6.25f,-0.445954f,24.75f, -0.180725f,0.942766f,0.280233f,
0.0f,2.76983f,18.5625f, 0.171645f,0.984908f,0.0222501f,
0.0f,2.76642f,24.75f, 0.0784993f,0.995428f,-0.0544117f,
6.25f,-2.53736f,18.5625f, 0.248298f,0.965028f,0.0840741f,
6.25f,-0.915645f,24.75f, 0.164311f,0.945856f,-0.279926f,
12.5f,-1.60054f,18.5625f, -0.180435f,0.983586f,0.00115547f,
12.5f,0.831925f,24.75f, -0.0692495f,0.964821f,-0.253623f,
18.75f,-0.800282f,18.5625f, 0.10755f,0.951538f,0.288112f,
18.75f,-1.47284f,24.75f, 0.232178f,0.972664f,0.00429361f,
25.0f,-2.78379f,18.5625f, 0.1833f,0.937477f,0.295867f,
25.0f,-0.967508f,24.75f, -0.0773477f,0.956641f,-0.280812f,
-25.0f,-0.352038f,12.375f, -0.192222f,0.972435f,0.131987f,
-18.75f,0.681228f,12.375f, -0.107779f,0.962569f,0.248686f,
-12.5f,2.72065f,12.375f, 0.0520846f,0.993034f,0.105692f,
-6.25f,-1.78776f,12.375f, -0.0133223f,0.99674f,-0.0795754f,
0.0f,1.57707f,12.375f, -0.129025f,0.969662f,-0.207627f,
6.25f,0.424356f,12.375f, 0.180995f,0.97074f,0.157815f,
12.5f,0.283234f,12.375f, -0.16285f,0.973539f,0.160316f,
18.75f,2.57015f,12.375f, -0.165454f,0.978339f,0.124408f,
25.0f,3.07363f,12.375f, -0.00454559f,0.988151f,0.153417f,
-25.0f,-0.893751f,6.1875f, -0.0836251f,0.990084f,-0.112876f,
-18.75f,0.930932f,6.1875f, 0.00269413f,0.971838f,-0.235633f,
-12.5f,-0.684529f,6.1875f, -0.010319f,0.927048f,-0.3748f,
-6.25f,-0.661383f,6.1875f, 0.012265f,0.999875f,-0.00994222f,
0.0f,0.385718f,6.1875f, -0.15435f,0.984937f,-0.0779465f,
6.25f,0.589796f,6.1875f, 0.0241717f,0.999582f,-0.0158589f,
12.5f,0.47454f,6.1875f, -0.017184f,0.997953f,-0.0616073f,
18.75f,1.59939f,6.1875f, -0.0902706f,0.957309f,-0.274609f,
25.0f,1.13434f,6.1875f, 0.0346006f,0.957441f,-0.286547f,
-25.0f,0.74548f,0.0f, 0.329903f,0.930812f,0.157334f,
-18.75f,-2.47437f,0.0f, 0.247844f,0.968142f,0.0357041f,
-12.5f,-4.16309f,1.81975e-007f, -0.0623898f,0.997626f,-0.0291436f,
-6.25f,-0.700022f,0.0f, -0.217067f,0.974987f,-0.0477624f,
0.0f,-0.0957388f,0.0f, -0.117693f,0.990712f,-0.068098f,
6.25f,0.614077f,0.0f, -0.0713214f,0.997305f,0.01722f,
12.5f,0.299109f,0.0f, 0.218606f,0.968641f,0.118094f,
18.75f,-1.71753f,0.0f, 0.0339493f,0.985612f,-0.165579f,
25.0f,-1.12638f,0.0f, -0.0547573f,0.989177f,-0.136126f,
-25.0f,-0.403319f,-6.1875f, -0.1263f,0.989211f,-0.074223f,
-18.75f,-0.0730335f,-6.1875f, 0.0173428f,0.965382f,0.260261f,
-12.5f,-0.450125f,-6.1875f, 0.0658811f,0.875f,0.479619f,
-6.25f,-0.180204f,-6.1875f, 0.0116584f,0.981445f,0.19139f,
0.0f,-0.632679f,-6.1875f, -0.0557983f,0.993041f,-0.103716f,
6.25f,0.308409f,-6.1875f, -0.191898f,0.96766f,-0.163737f,
12.5f,2.5011f,-6.1875f, 0.0758397f,0.996403f,-0.0378189f,
18.75f,-1.69303f,-6.1875f, 0.16761f,0.974057f,0.15205f,
25.0f,-0.141349f,-6.1875f, -0.204846f,0.978471f,0.0251341f,
-25.0f,-1.10316f,-12.375f, -0.0350644f,0.993748f,-0.106001f,
-18.75f,1.06711f,-12.375f, -0.309806f,0.919244f,-0.242922f,
-12.5f,3.61544f,-12.375f, -0.0506281f,0.998702f,-0.00561657f,
-6.25f,1.6504f,-12.375f, 0.322294f,0.937465f,0.131477f,
0.0f,-1.60595f,-12.375f, 0.086016f,0.995916f,0.0274459f,
6.25f,-0.849859f,-12.375f, -0.0332811f,0.997087f,0.0686255f,
12.5f,-0.942201f,-12.375f, -0.153559f,0.982185f,-0.108312f,
18.75f,0.494873f,-12.375f, 0.0753195f,0.981472f,0.176181f,
25.0f,-0.333828f,-12.375f, 0.035057f,0.987194f,0.155624f,
-25.0f,2.1862f,-18.5625f, 0.612899f,0.782306f,0.111138f,
-18.75f,-3.38259f,-18.5625f, 0.0194078f,0.992789f,-0.118297f,
-12.5f,-0.880916f,-18.5625f, -0.282251f,0.949875f,-0.13443f,
-6.25f,0.818158f,-18.5625f, 0.1476f,0.981788f,-0.119612f,
0.0f,-1.48019f,-18.5625f, -0.044551f,0.996013f,-0.0772824f,
6.25f,1.47123f,-18.5625f, -0.117036f,0.97986f,0.161792f,
12.5f,0.164076f,-18.5625f, -0.0340355f,0.978993f,0.201034f,
18.75f,2.14568f,-18.5625f, -0.0439269f,0.994859f,0.0912435f,
25.0f,0.626506f,-18.5625f, 0.208015f,0.968592f,0.136229f,
-25.0f,0.296889f,-24.75f, 0.46973f,0.844327f,-0.257809f,
-18.75f,-3.18022f,-24.75f, -0.0226758f,0.998794f,-0.0435401f,
-12.5f,1.8927f,-24.75f, -0.202772f,0.919836f,0.335836f,
-6.25f,1.46387f,-24.75f, 0.224904f,0.955746f,0.189652f,
0.0f,-2.52726f,-24.75f, -0.0435882f,0.993572f,-0.10447f,
6.25f,1.30397f,-24.75f, -0.28646f,0.956525f,-0.0547707f,
12.5f,1.85812f,-24.75f, 0.0521949f,0.979796f,0.193067f,
18.75f,1.56331f,-24.75f, -0.0270784f,0.999629f,-0.00293115f,
25.0f,1.05366f,-24.75f, 0.161f,0.986887f,-0.0115473f,
};
static float LargeMeshTex[] = {
0.125f,0.0f,
0.0f,0.0f,
0.125f,0.125f,
0.0f,0.125f,
0.125f,0.25f,
0.0f,0.25f,
0.125f,0.375f,
0.0f,0.375f,
0.125f,0.5f,
0.0f,0.5f,
0.125f,0.625f,
0.0f,0.625f,
0.125f,0.75f,
0.0f,0.75f,
0.125f,0.875f,
0.0f,0.875f,
0.125f,1.0f,
0.0f,1.0f,
0.25f,0.0f,
0.25f,0.125f,
0.25f,0.25f,
0.25f,0.375f,
0.25f,0.5f,
0.25f,0.625f,
0.25f,0.75f,
0.25f,0.875f,
0.25f,1.0f,
0.375f,0.0f,
0.375f,0.125f,
0.375f,0.25f,
0.375f,0.375f,
0.375f,0.5f,
0.375f,0.625f,
0.375f,0.75f,
0.375f,0.875f,
0.375f,1.0f,
0.5f,0.0f,
0.5f,0.125f,
0.5f,0.25f,
0.5f,0.375f,
0.5f,0.5f,
0.5f,0.625f,
0.5f,0.75f,
0.5f,0.875f,
0.5f,1.0f,
0.625f,0.0f,
0.625f,0.125f,
0.625f,0.25f,
0.625f,0.375f,
0.625f,0.5f,
0.625f,0.625f,
0.625f,0.75f,
0.625f,0.875f,
0.625f,1.0f,
0.75f,0.0f,
0.75f,0.125f,
0.75f,0.25f,
0.75f,0.375f,
0.75f,0.5f,
0.75f,0.625f,
0.75f,0.75f,
0.75f,0.875f,
0.75f,1.0f,
0.875f,0.0f,
0.875f,0.125f,
0.875f,0.25f,
0.875f,0.375f,
0.875f,0.5f,
0.875f,0.625f,
0.875f,0.75f,
0.875f,0.875f,
0.875f,1.0f,
1.0f,0.0f,
1.0f,0.125f,
1.0f,0.25f,
1.0f,0.375f,
1.0f,0.5f,
1.0f,0.625f,
1.0f,0.75f,
1.0f,0.875f,
1.0f,1.0f,
};
static unsigned short LargeMeshIdx[] = {
0,1,2,
3,2,1,
2,3,4,
5,4,3,
4,5,6,
7,6,5,
6,7,8,
9,8,7,
8,9,10,
11,10,9,
10,11,12,
13,12,11,
12,13,14,
15,14,13,
14,15,16,
17,16,15,
18,0,19,
2,19,0,
19,2,20,
4,20,2,
20,4,21,
6,21,4,
21,6,22,
8,22,6,
22,8,23,
10,23,8,
23,10,24,
12,24,10,
24,12,25,
14,25,12,
25,14,26,
16,26,14,
27,18,28,
19,28,18,
28,19,29,
20,29,19,
29,20,30,
21,30,20,
30,21,31,
22,31,21,
31,22,32,
23,32,22,
32,23,33,
24,33,23,
33,24,34,
25,34,24,
34,25,35,
26,35,25,
36,27,37,
28,37,27,
37,28,38,
29,38,28,
38,29,39,
30,39,29,
39,30,40,
31,40,30,
40,31,41,
32,41,31,
41,32,42,
33,42,32,
42,33,43,
34,43,33,
43,34,44,
35,44,34,
45,36,46,
37,46,36,
46,37,47,
38,47,37,
47,38,48,
39,48,38,
48,39,49,
40,49,39,
49,40,50,
41,50,40,
50,41,51,
42,51,41,
51,42,52,
43,52,42,
52,43,53,
44,53,43,
54,45,55,
46,55,45,
55,46,56,
47,56,46,
56,47,57,
48,57,47,
57,48,58,
49,58,48,
58,49,59,
50,59,49,
59,50,60,
51,60,50,
60,51,61,
52,61,51,
61,52,62,
53,62,52,
63,54,64,
55,64,54,
64,55,65,
56,65,55,
65,56,66,
57,66,56,
66,57,67,
58,67,57,
67,58,68,
59,68,58,
68,59,69,
60,69,59,
69,60,70,
61,70,60,
70,61,71,
62,71,61,
72,63,73,
64,73,63,
73,64,74,
65,74,64,
74,65,75,
66,75,65,
75,66,76,
67,76,66,
76,67,77,
68,77,67,
77,68,78,
69,78,68,
78,69,79,
70,79,69,
79,70,80,
71,80,70,
};

View File

@@ -0,0 +1,448 @@
/*
Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.
All rights reserved.
Physics Effects is open software; you can redistribute it and/or
modify it under the terms of the BSD License.
Physics Effects 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 BSD License for more details.
A copy of the BSD License is distributed with
Physics Effects under the filename: physics_effects_license.txt
*/
#include "../common/common.h"
#include "../common/ctrl_func.h"
#include "../common/render_func.h"
#include "../common/perf_func.h"
#include "physics_func.h"
#include "barrel.h"
#include "landscape.h"
#ifdef _WIN32
#include <gl/gl.h>
#include <gl/glu.h>
#endif
// ARA begin insert new code
#ifdef __ANDROID__
#include <EGL/egl.h>
#include <GLES/gl.h>
#endif // __ANDROID__
// ARA end
#define SAMPLE_NAME "api_physics_effects/1_simple"
static bool s_isRunning = true;
int sceneId = 0;
bool simulating = false;
int landscapeMeshId;
int convexMeshId;
void render(void)
{
render_begin();
for(int i=0;i<physics_get_num_rigidbodies();i++) {
const PfxRigidState &state = physics_get_state(i);
const PfxCollidable &coll = physics_get_collidable(i);
PfxTransform3 rbT(state.getOrientation(), state.getPosition());
PfxShapeIterator itrShape(coll);
for(int j=0;j<coll.getNumShapes();j++,++itrShape) {
const PfxShape &shape = *itrShape;
PfxTransform3 offsetT = shape.getOffsetTransform();
PfxTransform3 worldT = rbT * offsetT;
switch(shape.getType()) {
case kPfxShapeSphere:
render_sphere(
worldT,
PfxVector3(1,1,1),
PfxFloatInVec(shape.getSphere().m_radius));
break;
case kPfxShapeBox:
render_box(
worldT,
PfxVector3(1,1,1),
shape.getBox().m_half);
break;
case kPfxShapeCapsule:
render_capsule(
worldT,
PfxVector3(1,1,1),
PfxFloatInVec(shape.getCapsule().m_radius),
PfxFloatInVec(shape.getCapsule().m_halfLen));
break;
case kPfxShapeCylinder:
render_cylinder(
worldT,
PfxVector3(1,1,1),
PfxFloatInVec(shape.getCylinder().m_radius),
PfxFloatInVec(shape.getCylinder().m_halfLen));
break;
case kPfxShapeConvexMesh:
render_mesh(
worldT,
PfxVector3(1,1,1),
convexMeshId);
break;
case kPfxShapeLargeTriMesh:
render_mesh(
worldT,
PfxVector3(1,1,1),
landscapeMeshId);
break;
default:
break;
}
}
}
render_end();
}
int init(void)
{
perf_init();
ctrl_init();
render_init();
physics_init();
landscapeMeshId = render_init_mesh(
LargeMeshVtx,sizeof(float)*6,
LargeMeshVtx+3,sizeof(float)*6,
LargeMeshIdx,sizeof(unsigned short)*3,
LargeMeshVtxCount,LargeMeshIdxCount/3);
convexMeshId = render_init_mesh(
BarrelVtx,sizeof(float)*6,
BarrelVtx+3,sizeof(float)*6,
BarrelIdx,sizeof(unsigned short)*3,
BarrelVtxCount,BarrelIdxCount/3);
return 0;
}
static int shutdown(void)
{
ctrl_release();
render_release();
physics_release();
perf_release();
return 0;
}
void update(void)
{
float angX,angY,r;
render_get_view_angle(angX,angY,r);
ctrl_update();
if(ctrl_button_pressed(BTN_UP)) {
angX -= 0.05f;
if(angX < -1.4f) angX = -1.4f;
if(angX > -0.01f) angX = -0.01f;
}
if(ctrl_button_pressed(BTN_DOWN)) {
angX += 0.05f;
if(angX < -1.4f) angX = -1.4f;
if(angX > -0.01f) angX = -0.01f;
}
if(ctrl_button_pressed(BTN_LEFT)) {
angY -= 0.05f;
}
if(ctrl_button_pressed(BTN_RIGHT)) {
angY += 0.05f;
}
if(ctrl_button_pressed(BTN_ZOOM_OUT)) {
r *= 1.1f;
if(r > 500.0f) r = 500.0f;
}
if(ctrl_button_pressed(BTN_ZOOM_IN)) {
r *= 0.9f;
if(r < 1.0f) r = 1.0f;
}
if(ctrl_button_pressed(BTN_SCENE_RESET) == BTN_STAT_DOWN) {
physics_create_scene(sceneId);
}
if(ctrl_button_pressed(BTN_SCENE_NEXT) == BTN_STAT_DOWN) {
physics_create_scene(++sceneId);
}
if(ctrl_button_pressed(BTN_SIMULATION) == BTN_STAT_DOWN) {
simulating = !simulating;
}
if(ctrl_button_pressed(BTN_STEP) == BTN_STAT_DOWN) {
simulating = true;
}
else if(ctrl_button_pressed(BTN_STEP) == BTN_STAT_UP || ctrl_button_pressed(BTN_STEP) == BTN_STAT_KEEP) {
simulating = false;
}
render_set_view_angle(angX,angY,r);
}
#ifndef _WIN32
// ARA begin insert new code
#ifdef __ANDROID__
///////////////////////////////////////////////////////////////////////////////
// sceneChange
//
/// This function is used to change the physics scene on Android devices
///////////////////////////////////////////////////////////////////////////////
void sceneChange()
{
physics_create_scene(sceneId++);
}
#else // __ANDROID__
// ARA end
///////////////////////////////////////////////////////////////////////////////
// Main
int main(void)
{
init();
physics_create_scene(sceneId);
printf("## %s: INIT SUCCEEDED ##\n", SAMPLE_NAME);
while (s_isRunning) {
update();
if(simulating) physics_simulate();
render();
perf_sync();
}
shutdown();
printf("## %s: FINISHED ##\n", SAMPLE_NAME);
return 0;
}
// ARA begin insert new code
#endif // __ANDROID__
// ARA end
#else // _WIN32
///////////////////////////////////////////////////////////////////////////////
// WinMain
extern HDC hDC;
extern HGLRC hRC;
HWND hWnd;
HINSTANCE hInstance;
void releaseWindow()
{
if(hRC) {
wglMakeCurrent(0,0);
wglDeleteContext(hRC);
}
if(hDC) ReleaseDC(hWnd,hDC);
if(hWnd) DestroyWindow(hWnd);
UnregisterClass(SAMPLE_NAME,hInstance);
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg) {
case WM_SYSCOMMAND:
{
switch (wParam) {
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_SIZE:
render_resize(LOWORD(lParam),HIWORD(lParam));
return 0;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
bool createWindow(char* title, int width, int height)
{
WNDCLASS wc;
RECT rect;
rect.left=0;
rect.right=width;
rect.top=0;
rect.bottom=height;
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = SAMPLE_NAME;
if(!RegisterClass(&wc)) {
return false;
}
AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
if(!(hWnd=CreateWindowEx(WS_EX_APPWINDOW|WS_EX_WINDOWEDGE,SAMPLE_NAME,title,
WS_OVERLAPPEDWINDOW|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,
0,0,rect.right-rect.left,rect.bottom-rect.top,
NULL,NULL,hInstance,NULL))) {
releaseWindow();
return false;
}
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0,
0, 0,
0, 0,
0, 0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
if(!(hDC=GetDC(hWnd)))
{
releaseWindow();
OutputDebugString("");
return FALSE;
}
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 ){
OutputDebugString("ChoosePixelFormat Failed....");
return FALSE;
}
if (SetPixelFormat(hDC, pixelformat, &pfd) == FALSE){
OutputDebugString("SetPixelFormat Failed....");
return FALSE;
}
if (!(hRC=wglCreateContext(hDC))){
OutputDebugString("Creating HGLRC Failed....");
return FALSE;
}
// Set Vsync
//BOOL (WINAPI *wglSwapIntervalEXT)(int) = NULL;
//if(strstr((char*)glGetString( GL_EXTENSIONS ),"WGL_EXT_swap_control")== 0) {
//}
//else {
//wglSwapIntervalEXT = (BOOL (WINAPI*)(int))wglGetProcAddress("wglSwapIntervalEXT");
//if(wglSwapIntervalEXT) wglSwapIntervalEXT(1);
//}
wglMakeCurrent(hDC,hRC);
ShowWindow(hWnd,SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
render_resize(width, height);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
if(!createWindow(SAMPLE_NAME,DISPLAY_WIDTH,DISPLAY_HEIGHT)) {
MessageBox(NULL,"Can't create gl window.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
init();
physics_create_scene(sceneId);
SCE_PFX_PRINTF("## %s: INIT SUCCEEDED ##\n", SAMPLE_NAME);
MSG msg;
while(s_isRunning) {
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
if(msg.message==WM_QUIT) {
s_isRunning = false;
}
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else {
update();
if(simulating) physics_simulate();
render();
perf_sync();
}
}
shutdown();
SCE_PFX_PRINTF("## %s: FINISHED ##\n", SAMPLE_NAME);
releaseWindow();
return (msg.wParam);
}
#endif

View File

@@ -0,0 +1,857 @@
/*
Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.
All rights reserved.
Physics Effects is open software; you can redistribute it and/or
modify it under the terms of the BSD License.
Physics Effects 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 BSD License for more details.
A copy of the BSD License is distributed with
Physics Effects under the filename: physics_effects_license.txt
*/
#include "physics_func.h"
#include "../common/perf_func.h"
///////////////////////////////////////////////////////////////////////////////
// Simulation Data
#define NUM_RIGIDBODIES 1000
#define NUM_JOINTS 500
#define NUM_CONTACTS 4000
const float timeStep = 0.016f;
const float separateBias = 0.1f;
int iteration = 5;
//J ???????
//E World size
PfxVector3 worldCenter(0.0f);
PfxVector3 worldExtent(500.0f);
//J ??
//E Rigid body
PfxRigidState states[NUM_RIGIDBODIES];
PfxRigidBody bodies[NUM_RIGIDBODIES];
PfxCollidable collidables[NUM_RIGIDBODIES];
PfxSolverBody solverBodies[NUM_RIGIDBODIES];
int numRigidBodies = 0;
//J ?????????????????
//E Large mesh for representing a landscape
#include "landscape.h"
PfxLargeTriMesh gLargeMesh;
//J ?????
//E Convex Mesh
#include "barrel.h"
PfxConvexMesh gConvex;
//J ????
//E Proxies
PfxBroadphaseProxy proxies[NUM_RIGIDBODIES];
//J ?????
//E Joint
PfxConstraintPair jointPairs[NUM_JOINTS];
PfxJoint joints[NUM_JOINTS];
int numJoints = 0;
//J ??
//E Pairs
unsigned int numPairs;
PfxBroadphasePair pairs[NUM_CONTACTS];
//J ?????
//E Contacts
PfxContactManifold contacts[NUM_CONTACTS];
int numContacts;
//J ??????
//E Temporary buffers
#define POOL_BYTES (5*1024*1024)
unsigned char SCE_PFX_ALIGNED(128) poolBuff[POOL_BYTES];
//J ????????????????
//E Stack allocator for temporary buffers
PfxHeapManager pool(poolBuff,POOL_BYTES);
// ARA begin insert new code
//E task manager for parallel demo
#define NUM_THREADS 2
PfxTaskManager *taskManager = NULL;
//E need enough bytes for NUM_THREADS PfxTaskArg objects, with the space rounded up to fill a 16-bit aligned space
#define TASK_MANAGER_POOL_BYTES 1024
unsigned char SCE_PFX_ALIGNED(16) taskPoolBuff[TASK_MANAGER_POOL_BYTES];
// ARA end
///////////////////////////////////////////////////////////////////////////////
// Simulation Function
int frame = 0;
void broadphase()
{
//J ?????????????????
//E Find the axis along which all rigid bodies are most widely positioned
int axis = 0;
{
PfxVector3 s(0.0f),s2(0.0f);
for(int i=0;i<numRigidBodies;i++) {
PfxVector3 c = states[i].getPosition();
s += c;
s2 += mulPerElem(c,c);
}
PfxVector3 v = s2 - mulPerElem(s,s) / (float)numRigidBodies;
if(v[1] > v[0]) axis = 1;
if(v[2] > v[axis]) axis = 2;
}
//J ???????????????
//E Create broadpahse proxies
{
for(int i=0;i<numRigidBodies;i++) {
pfxUpdateBroadphaseProxy(proxies[i],states[i],collidables[i],worldCenter,worldExtent,axis);
}
int workBytes = sizeof(PfxBroadphaseProxy) * numRigidBodies;
void *workBuff = pool.allocate(workBytes);
pfxParallelSort(proxies,numRigidBodies,workBuff,workBytes);
pool.deallocate(workBuff);
}
//J ??????
//E Find overlapped pairs
{
PfxFindPairsParam param;
param.workBytes = pfxGetWorkBytesOfFindPairs(NUM_CONTACTS);
param.workBuff = pool.allocate(param.workBytes);
param.pairBytes = pfxGetPairBytesOfFindPairs(NUM_CONTACTS);
param.pairBuff = pool.allocate(param.pairBytes);
param.proxies = proxies;
param.numProxies = numRigidBodies;
param.maxPairs = NUM_CONTACTS;
param.axis = axis;
PfxFindPairsResult result;
int ret = pfxFindPairs(param,result);
if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxFindPairs failed %d\n",ret);
memcpy(pairs,result.pairs,sizeof(PfxBroadphasePair)*result.numPairs);
numPairs = result.numPairs;
pool.deallocate(param.pairBuff);
pool.deallocate(param.workBuff);
}
//J ??????????????
//E Add new contacts and initialize
for(PfxUInt32 i=0;i<numPairs;i++) {
pfxSetContactId(pairs[i],i);
PfxContactManifold &contact = contacts[i];
contact.reset(pfxGetObjectIdA(pairs[i]),pfxGetObjectIdB(pairs[i]));
}
numContacts = numPairs;
}
void collision()
{
//J ????
//E Detect collisions
{
PfxDetectCollisionParam param;
param.contactPairs = pairs;
param.numContactPairs = numPairs;
param.offsetContactManifolds = contacts;
param.offsetRigidStates = states;
param.offsetCollidables = collidables;
param.numRigidBodies = numRigidBodies;
// ARA begin insert new code
#ifdef USE_PTHREADS
int ret = pfxDetectCollision(param, taskManager);
#else
// ARA end
int ret = pfxDetectCollision(param);
// ARA begin insert new code
#endif
// ARA end
if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxDetectCollision failed %d\n",ret);
}
}
void constraintSolver()
{
PfxPerfCounter pc;
pc.countBegin("setup solver bodies");
{
PfxSetupSolverBodiesParam param;
param.states = states;
param.bodies = bodies;
param.solverBodies = solverBodies;
param.numRigidBodies = numRigidBodies;
// ARA begin insert new code
#ifdef USE_PTHREADS
int ret = pfxSetupSolverBodies(param, taskManager);
#else
// ARA end
int ret = pfxSetupSolverBodies(param);
// ARA begin insert new code
#endif
// ARA end
if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxSetupSolverBodies failed %d\n",ret);
}
pc.countEnd();
pc.countBegin("setup contact constraints");
{
PfxSetupContactConstraintsParam param;
param.contactPairs = pairs;
param.numContactPairs = numPairs;
param.offsetContactManifolds = contacts;
param.offsetRigidStates = states;
param.offsetRigidBodies = bodies;
param.offsetSolverBodies = solverBodies;
param.numRigidBodies = numRigidBodies;
param.timeStep = timeStep;
param.separateBias = separateBias;
// ARA begin insert new code
#ifdef USE_PTHREADS
int ret = pfxSetupContactConstraints(param, taskManager);
#else
// ARA end
int ret = pfxSetupContactConstraints(param);
// ARA begin insert new code
#endif
// ARA end
if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxSetupJointConstraints failed %d\n",ret);
}
pc.countEnd();
pc.countBegin("setup joint constraints");
{
PfxSetupJointConstraintsParam param;
param.jointPairs = jointPairs;
param.numJointPairs = numJoints;
param.offsetJoints = joints;
param.offsetRigidStates = states;
param.offsetRigidBodies = bodies;
param.offsetSolverBodies = solverBodies;
param.numRigidBodies = numRigidBodies;
param.timeStep = timeStep;
for(int i=0;i<numJoints;i++) {
pfxUpdateJointPairs(jointPairs[i],i,joints[i],states[joints[i].m_rigidBodyIdA],states[joints[i].m_rigidBodyIdB]);
}
// ARA begin insert new code
#ifdef USE_PTHREADS
int ret = pfxSetupJointConstraints(param, taskManager);
#else
// ARA end
int ret = pfxSetupJointConstraints(param);
// ARA begin insert new code
#endif
// ARA end
if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxSetupJointConstraints failed %d\n",ret);
}
pc.countEnd();
pc.countBegin("solve constraints");
{
PfxSolveConstraintsParam param;
param.workBytes = pfxGetWorkBytesOfSolveConstraints(numRigidBodies,numPairs,numJoints);
param.workBuff = pool.allocate(param.workBytes);
param.contactPairs = pairs;
param.numContactPairs = numPairs;
param.offsetContactManifolds = contacts;
param.jointPairs = jointPairs;
param.numJointPairs = numJoints;
param.offsetJoints = joints;
param.offsetRigidStates = states;
param.offsetSolverBodies = solverBodies;
param.numRigidBodies = numRigidBodies;
param.iteration = iteration;
// ARA begin insert new code
#ifdef USE_PTHREADS
int ret = pfxSolveConstraints(param, taskManager);
#else
// ARA end
int ret = pfxSolveConstraints(param);
// ARA begin insert new code
#endif
// ARA end
if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxSolveConstraints failed %d\n",ret);
pool.deallocate(param.workBuff);
}
pc.countEnd();
//pc.printCount();
}
void integrate()
{
PfxUpdateRigidStatesParam param;
param.states = states;
param.bodies = bodies;
param.numRigidBodies = numRigidBodies;
param.timeStep = timeStep;
// ARA begin insert new code
#ifdef USE_PTHREADS
pfxUpdateRigidStates(param, taskManager);
#else
// ARA end
pfxUpdateRigidStates(param);
// ARA begin insert new code
#endif
// ARA end
}
void physics_simulate()
{
PfxPerfCounter pc;
for(int i=1;i<numRigidBodies;i++) {
pfxApplyExternalForce(states[i],bodies[i],bodies[i].getMass()*PfxVector3(0.0f,-9.8f,0.0f),PfxVector3(0.0f),timeStep);
}
perf_push_marker("broadphase");
pc.countBegin("broadphase");
broadphase();
pc.countEnd();
perf_pop_marker();
perf_push_marker("collision");
pc.countBegin("collision");
collision();
pc.countEnd();
perf_pop_marker();
perf_push_marker("solver");
pc.countBegin("solver");
constraintSolver();
pc.countEnd();
perf_pop_marker();
perf_push_marker("integrate");
pc.countBegin("integrate");
integrate();
pc.countEnd();
perf_pop_marker();
frame++;
if(frame%100 == 0) {
float broadphaseTime = pc.getCountTime(0);
float collisionTime = pc.getCountTime(2);
float solverTime = pc.getCountTime(4);
float integrateTime = pc.getCountTime(6);
SCE_PFX_PRINTF("frame %3d broadphase %.2f collision %.2f solver %.2f integrate %.2f | total %.2f\n",frame,
broadphaseTime,collisionTime,solverTime,integrateTime,
broadphaseTime+collisionTime+solverTime+integrateTime);
}
}
///////////////////////////////////////////////////////////////////////////////
// Create Scene
void createBrick(int id,const PfxVector3 &pos,const PfxQuat &rot,const PfxVector3 &boxSize,PfxFloat mass)
{
PfxBox box(boxSize);
PfxShape shape;
shape.reset();
shape.setBox(box);
collidables[id].reset();
collidables[id].addShape(shape);
collidables[id].finish();
bodies[id].reset();
bodies[id].setRestitution(0.0f);
bodies[id].setMass(mass);
bodies[id].setInertia(pfxCalcInertiaBox(boxSize,mass));
states[id].reset();
states[id].setPosition(pos);
states[id].setOrientation(rot);
states[id].setMotionType(kPfxMotionTypeActive);
states[id].setRigidBodyId(id);
}
void createWall(const PfxVector3 &offsetPosition,int stackSize,const PfxVector3 &boxSize)
{
PfxFloat bodyMass = 0.5f;
PfxFloat diffX = boxSize[0] * 1.02f;
PfxFloat diffY = boxSize[1] * 1.02f;
PfxFloat diffZ = boxSize[2] * 1.02f;
PfxFloat offset = -stackSize * (diffZ * 2.0f) * 0.5f;
PfxVector3 pos(0.0f, diffY, 0.0f);
while(stackSize) {
for(int i=0;i<stackSize;i++) {
pos[2] = offset + (PfxFloat)i * (diffZ * 2.0f);
createBrick(numRigidBodies++,offsetPosition+pos,PfxQuat::identity(),boxSize,bodyMass);
}
offset += diffZ;
pos[1] += (diffY * 2.0f);
stackSize--;
}
}
void createPyramid(const PfxVector3 &offsetPosition,int stackSize,const PfxVector3 &boxSize)
{
PfxFloat space = 0.0001f;
PfxVector3 pos(0.0f, boxSize[1], 0.0f);
PfxFloat diffX = boxSize[0] * 1.02f;
PfxFloat diffY = boxSize[1] * 1.02f;
PfxFloat diffZ = boxSize[2] * 1.02f;
PfxFloat offsetX = -stackSize * (diffX * 2.0f + space) * 0.5f;
PfxFloat offsetZ = -stackSize * (diffZ * 2.0f + space) * 0.5f;
while(stackSize) {
for(int j=0;j<stackSize;j++) {
pos[2] = offsetZ + (PfxFloat)j * (diffZ * 2.0f + space);
for(int i=0;i<stackSize;i++) {
pos[0] = offsetX + (PfxFloat)i * (diffX * 2.0f + space);
createBrick(numRigidBodies++,offsetPosition+pos,PfxQuat::identity(),boxSize,1.0f);
}
}
offsetX += diffX;
offsetZ += diffZ;
pos[1] += (diffY * 2.0f + space);
stackSize--;
}
}
void createTowerCircle(const PfxVector3 &offsetPosition,int stackSize,int rotSize,const PfxVector3 &boxSize)
{
PfxFloat radius = 1.3f * rotSize * boxSize[0] / SCE_PFX_PI;
// create active boxes
PfxQuat rotY = PfxQuat::identity();
PfxFloat posY = boxSize[1];
for(int i=0;i<stackSize;i++) {
for(int j=0;j<rotSize;j++) {
createBrick(numRigidBodies++,offsetPosition+rotate(rotY,PfxVector3(0.0f , posY, radius)),rotY,boxSize,0.5f);
rotY *= PfxQuat::rotationY(SCE_PFX_PI/(rotSize*0.5f));
}
posY += boxSize[1] * 2.0f;
rotY *= PfxQuat::rotationY(SCE_PFX_PI/(PfxFloat)rotSize);
}
}
void createScenePrimitives()
{
// sphere
{
int id = numRigidBodies++;
PfxSphere sphere(1.0f);
PfxShape shape;
shape.reset();
shape.setSphere(sphere);
collidables[id].reset();
collidables[id].addShape(shape);
collidables[id].finish();
bodies[id].reset();
bodies[id].setMass(1.0f);
bodies[id].setInertia(pfxCalcInertiaSphere(1.0f,1.0f));
states[id].reset();
states[id].setPosition(PfxVector3(-5.0f,5.0f,0.0f));
states[id].setMotionType(kPfxMotionTypeActive);
states[id].setRigidBodyId(id);
}
// box
{
int id = numRigidBodies++;
PfxBox box(1.0f,1.0f,1.0f);
PfxShape shape;
shape.reset();
shape.setBox(box);
collidables[id].reset();
collidables[id].addShape(shape);
collidables[id].finish();
bodies[id].reset();
bodies[id].setMass(1.0f);
bodies[id].setInertia(pfxCalcInertiaBox(PfxVector3(1.0f),1.0f));
states[id].reset();
states[id].setPosition(PfxVector3(0.0f,5.0f,5.0f));
states[id].setMotionType(kPfxMotionTypeActive);
states[id].setRigidBodyId(id);
}
// capsule
{
int id = numRigidBodies++;
PfxCapsule capsule(1.5f,0.5f);
PfxShape shape;
shape.reset();
shape.setCapsule(capsule);
collidables[id].reset();
collidables[id].addShape(shape);
collidables[id].finish();
bodies[id].reset();
bodies[id].setMass(2.0f);
bodies[id].setInertia(pfxCalcInertiaCylinderX(2.0f,0.5f,2.0f));
states[id].reset();
states[id].setPosition(PfxVector3(5.0f,5.0f,0.0f));
states[id].setMotionType(kPfxMotionTypeActive);
states[id].setRigidBodyId(id);
}
// cylinder
{
int id = numRigidBodies++;
PfxCylinder cylinder(0.5f,1.5f);
PfxShape shape;
shape.reset();
shape.setCylinder(cylinder);
collidables[id].reset();
collidables[id].addShape(shape);
collidables[id].finish();
bodies[id].reset();
bodies[id].setMass(3.0f);
bodies[id].setInertia(pfxCalcInertiaCylinderX(0.5f,1.5f,3.0f));
states[id].reset();
states[id].setPosition(PfxVector3(0.0f,10.0f,0.0f));
states[id].setMotionType(kPfxMotionTypeActive);
states[id].setRigidBodyId(id);
}
// convex mesh
{
PfxCreateConvexMeshParam param;
param.verts = BarrelVtx;
param.numVerts = BarrelVtxCount;
param.vertexStrideBytes = sizeof(float)*6;
param.triangles = BarrelIdx;
param.numTriangles = BarrelIdxCount/3;
param.triangleStrideBytes = sizeof(unsigned short)*3;
PfxInt32 ret = pfxCreateConvexMesh(gConvex,param);
if(ret != SCE_PFX_OK) {
SCE_PFX_PRINTF("Can't create gConvex mesh.\n");
}
int id = numRigidBodies++;
PfxShape shape;
shape.reset();
shape.setConvexMesh(&gConvex);
collidables[id].reset();
collidables[id].addShape(shape);
collidables[id].finish();
bodies[id].reset();
bodies[id].setMass(3.0f);
bodies[id].setInertia(pfxCalcInertiaSphere(1.0f,1.0f));
states[id].reset();
states[id].setPosition(PfxVector3(0.0f,15.0f,0.0f));
states[id].setMotionType(kPfxMotionTypeActive);
states[id].setRigidBodyId(id);
}
// combined primitives
{
int id = numRigidBodies++;
//E Both shapes and incides buffer have to be kept when creating a combined shape.
static PfxShape shapes[3];
PfxUInt16 shapeIds[3]={0,1,2};
collidables[id].reset(shapes,shapeIds,3);
{
PfxBox box(0.5f,0.5f,1.5f);
PfxShape shape;
shape.reset();
shape.setBox(box);
shape.setOffsetPosition(PfxVector3(-2.0f,0.0f,0.0f));
collidables[id].addShape(shape);
}
{
PfxBox box(0.5f,1.5f,0.5f);
PfxShape shape;
shape.reset();
shape.setBox(box);
shape.setOffsetPosition(PfxVector3(2.0f,0.0f,0.0f));
collidables[id].addShape(shape);
}
{
PfxCapsule cap(1.5f,0.5f);
PfxShape shape;
shape.reset();
shape.setCapsule(cap);
collidables[id].addShape(shape);
}
collidables[id].finish();
bodies[id].reset();
bodies[id].setMass(3.0f);
bodies[id].setInertia(pfxCalcInertiaBox(PfxVector3(2.5f,1.0f,1.0f),3.0f));
states[id].reset();
states[id].setPosition(PfxVector3(0.0f,5.0f,0.0f));
states[id].setMotionType(kPfxMotionTypeActive);
states[id].setRigidBodyId(id);
}
}
void createSceneJoints()
{
const int n = 10;
int startId = numRigidBodies;
PfxVector3 boxSize(1.0f);
PfxFloat boxMass = 1.0f;
for(int i=0;i<n;i++) {
createBrick(numRigidBodies++,PfxVector3(0,3.0f+i*2.5f*boxSize[1],0),PfxQuat::identity(),boxSize,boxMass);
}
for(int i=startId;i<startId+n;i++) {
PfxRigidState &stateA = states[i];
PfxRigidState &stateB = states[(i+1)%numRigidBodies];
PfxVector3 anchor;
if(i == numRigidBodies-1) {
anchor = stateA.getPosition() + PfxVector3(0,boxSize[1],0);
}
else {
anchor = ( stateA.getPosition() + stateB.getPosition() ) * 0.5f;
}
PfxSwingTwistJointInitParam jparam;
jparam.anchorPoint = anchor;
jparam.twistAxis = PfxVector3(0,1,0);
pfxInitializeSwingTwistJoint(joints[numJoints],stateA,stateB,jparam);
joints[numJoints].m_constraints[4].m_damping = 0.1f;
joints[numJoints].m_constraints[5].m_damping = 0.1f;
pfxUpdateJointPairs(jointPairs[numJoints],numJoints,joints[numJoints],stateA,stateB);
SCE_PFX_ASSERT(numJoints<NUM_JOINTS);
numJoints++;
}
states[startId].setLinearVelocity(PfxVector3(0,0,5));
states[startId].setLinearDamping(0.95f);
states[startId].setAngularDamping(0.95f);
}
void createSceneStacking()
{
createTowerCircle(PfxVector3(0.0f,0.0f,0.0f),8,24,PfxVector3(1));
}
void createSceneBoxGround()
{
int id = numRigidBodies++;
PfxBox box(150.0f,2.5f,150.0f);
PfxShape shape;
shape.reset();
shape.setBox(box);
collidables[id].reset();
collidables[id].addShape(shape);
collidables[id].finish();
bodies[id].reset();
states[id].reset();
states[id].setPosition(PfxVector3(0.0f,-2.5f,0.0f));
states[id].setMotionType(kPfxMotionTypeFixed);
states[id].setRigidBodyId(id);
}
void createSceneLandscape()
{
PfxCreateLargeTriMeshParam param;
param.verts = LargeMeshVtx;
param.numVerts = LargeMeshVtxCount;
param.vertexStrideBytes = sizeof(float)*6;
param.triangles = LargeMeshIdx;
param.numTriangles = LargeMeshIdxCount/3;
param.triangleStrideBytes = sizeof(unsigned short)*3;
if(gLargeMesh.m_numIslands > 0) {
pfxReleaseLargeTriMesh(gLargeMesh);
}
PfxInt32 ret = pfxCreateLargeTriMesh(gLargeMesh,param);
if(ret != SCE_PFX_OK) {
SCE_PFX_PRINTF("Can't create large mesh.\n");
}
int id = numRigidBodies++;
PfxShape shape;
shape.reset();
shape.setLargeTriMesh(&gLargeMesh);
collidables[id].reset();
collidables[id].addShape(shape);
collidables[id].finish();
bodies[id].reset();
states[id].reset();
states[id].setPosition(PfxVector3(0.0f,-5.0f,0.0f));
states[id].setOrientation(PfxQuat::rotationX(0.5f)*PfxQuat::rotationY(0.7f));
states[id].setMotionType(kPfxMotionTypeFixed);
states[id].setRigidBodyId(id);
}
void physics_create_scene(int sceneId)
{
const int numScenes = 4;
int sid = sceneId % numScenes;
numRigidBodies= 0;
numPairs = 0;
numContacts = 0;
numJoints = 0;
frame = 0;
switch(sid) {
case 0: // simple primitives
createSceneBoxGround();
createScenePrimitives();
break;
case 1: // joints
createSceneBoxGround();
createSceneJoints();
break;
case 2: // stacking
createSceneBoxGround();
createSceneStacking();
break;
case 3: // landscape
createSceneLandscape();
createScenePrimitives();
break;
}
SCE_PFX_PRINTF("----- Size of rigid body buffer ------\n");
SCE_PFX_PRINTF(" size * num = total\n");
SCE_PFX_PRINTF("PfxRigidState %5d * %5d = %5d bytes\n",sizeof(PfxRigidState),numRigidBodies,sizeof(PfxRigidState)*numRigidBodies);
SCE_PFX_PRINTF("PfxRigidBody %5d * %5d = %5d bytes\n",sizeof(PfxRigidBody),numRigidBodies,sizeof(PfxRigidBody)*numRigidBodies);
SCE_PFX_PRINTF("PfxCollidable %5d * %5d = %5d bytes\n",sizeof(PfxCollidable),numRigidBodies,sizeof(PfxCollidable)*numRigidBodies);
SCE_PFX_PRINTF("PfxJoint %5d * %5d = %5d bytes\n",sizeof(PfxJoint),numJoints,sizeof(PfxJoint)*numJoints);
SCE_PFX_PRINTF("PfxSolverBody %5d * %5d = %5d bytes\n",sizeof(PfxSolverBody),numRigidBodies,sizeof(PfxSolverBody)*numRigidBodies);
SCE_PFX_PRINTF("PfxBroadphaseProxy %5d * %5d = %5d bytes\n",sizeof(PfxBroadphaseProxy),numRigidBodies,sizeof(PfxBroadphaseProxy)*numRigidBodies);
SCE_PFX_PRINTF("PfxContactManifold %5d * %5d = %5d bytes\n",sizeof(PfxContactManifold),NUM_CONTACTS,sizeof(PfxContactManifold)*NUM_CONTACTS);
SCE_PFX_PRINTF("PfxBroadphasePair %5d * %5d = %5d bytes\n",sizeof(PfxBroadphasePair),NUM_CONTACTS,sizeof(PfxBroadphasePair)*NUM_CONTACTS);
int totalBytes =
(sizeof(PfxRigidState) + sizeof(PfxRigidBody) + sizeof(PfxCollidable) + sizeof(PfxSolverBody) + sizeof(PfxBroadphaseProxy)) * numRigidBodies +
(sizeof(PfxContactManifold) + sizeof(PfxBroadphasePair)) * NUM_CONTACTS;
SCE_PFX_PRINTF("----------------------------------------------------------\n");
SCE_PFX_PRINTF("Total %5d bytes\n",totalBytes);
// ARA begin insert new code
#ifdef USE_PTHREADS
if (!taskManager)
{
taskManager = PfxCreateTaskManagerPthreads(NUM_THREADS, NUM_THREADS, taskPoolBuff, TASK_MANAGER_POOL_BYTES);
taskManager->initialize();
}
#endif
// ARA end
}
///////////////////////////////////////////////////////////////////////////////
// Initialize / Finalize Engine
bool physics_init()
{
return true;
}
void physics_release()
{
// ARA begin insert new code
if (taskManager)
taskManager->finalize();
// ARA end
}
///////////////////////////////////////////////////////////////////////////////
// Pick
PfxVector3 physics_pick_start(const PfxVector3 &p1,const PfxVector3 &p2)
{
return PfxVector3(0.0f);
}
void physics_pick_update(const PfxVector3 &p)
{
}
void physics_pick_end()
{
}
///////////////////////////////////////////////////////////////////////////////
// Get Information
int physics_get_num_rigidbodies()
{
return numRigidBodies;
}
const PfxRigidState& physics_get_state(int id)
{
return states[id];
}
const PfxRigidBody& physics_get_body(int id)
{
return bodies[id];
}
const PfxCollidable& physics_get_collidable(int id)
{
return collidables[id];
}
int physics_get_num_contacts()
{
return numPairs;
}
const PfxContactManifold &physics_get_contact(int id)
{
return contacts[pfxGetConstraintId(pairs[id])];
}

View File

@@ -0,0 +1,49 @@
/*
Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.
All rights reserved.
Physics Effects is open software; you can redistribute it and/or
modify it under the terms of the BSD License.
Physics Effects 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 BSD License for more details.
A copy of the BSD License is distributed with
Physics Effects under the filename: physics_effects_license.txt
*/
#ifndef __PHYSICS_FUNC_H__
#define __PHYSICS_FUNC_H__
#define SCE_PFX_USE_PERFCOUNTER
#include "physics_effects.h"
using namespace sce::PhysicsEffects;
//E Simulation
//J ????????
bool physics_init();
void physics_release();
void physics_create_scene(int sceneId);
void physics_simulate();
//E Picking
//J ?????
PfxVector3 physics_pick_start(const PfxVector3 &p1,const PfxVector3 &p2);
void physics_pick_update(const PfxVector3 &p);
void physics_pick_end();
//E Change parameters
//J ????????
int physics_get_num_rigidbodies();
const PfxRigidState& physics_get_state(int id);
const PfxRigidBody& physics_get_body(int id);
const PfxCollidable& physics_get_collidable(int id);
int physics_get_num_contacts();
const PfxContactManifold &physics_get_contact(int id);
#endif /* __PHYSICS_FUNC_H__ */