Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 12ae961

Browse files
committed
addd boxes examples
1 parent f2e6284 commit 12ae961

File tree

6 files changed

+197
-0
lines changed

6 files changed

+197
-0
lines changed

sensors/ex2_boxes/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package=""><uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23"/><application android:debuggable="true" android:icon="@drawable/icon" android:label=""><activity android:name=".MainActivity">android:theme="@android:style/Theme.NoTitleBar"&gt;<intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>

sensors/ex2_boxes/Box.pde

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Box {
2+
Body body;
3+
float w;
4+
float h;
5+
6+
// Constructor
7+
Box(float x, float y) {
8+
w = random(50, 100);
9+
h = random(50, 100);
10+
// Add the box to the box2d world
11+
makeBody(new Vec2(x, y), w, h);
12+
}
13+
14+
void display() {
15+
Vec2 pos = box2d.getBodyPixelCoord(body);
16+
float a = body.getAngle();
17+
18+
rectMode(CENTER);
19+
pushMatrix();
20+
translate(pos.x, pos.y);
21+
rotate(-a);
22+
fill(175);
23+
stroke(0);
24+
rect(0, 0, w, h);
25+
popMatrix();
26+
}
27+
28+
void makeBody(Vec2 center, float w_, float h_) {
29+
// Define a polygon (this is what we use for a rectangle)
30+
PolygonShape sd = new PolygonShape();
31+
float box2dW = box2d.scalarPixelsToWorld(w_/2);
32+
float box2dH = box2d.scalarPixelsToWorld(h_/2);
33+
sd.setAsBox(box2dW, box2dH);
34+
35+
// Define a fixture
36+
FixtureDef fd = new FixtureDef();
37+
fd.shape = sd;
38+
// Parameters that affect physics
39+
fd.density = 1;
40+
fd.friction = 0.3;
41+
fd.restitution = 0.5;
42+
43+
// Define the body and make it from the shape
44+
BodyDef bd = new BodyDef();
45+
bd.type = BodyType.DYNAMIC;
46+
bd.position.set(box2d.coordPixelsToWorld(center));
47+
48+
body = box2d.createBody(bd);
49+
body.createFixture(fd);
50+
51+
// Give it some initial random velocity
52+
shake();
53+
}
54+
55+
void shake() {
56+
body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5)));
57+
body.setAngularVelocity(random(-5, 5));
58+
}
59+
}

sensors/ex2_boxes/Wall.pde

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Wall {
2+
float x;
3+
float y;
4+
float w;
5+
float h;
6+
7+
Body b;
8+
9+
Wall(float x_,float y_, float w_, float h_) {
10+
x = x_;
11+
y = y_;
12+
w = w_;
13+
h = h_;
14+
15+
PolygonShape sd = new PolygonShape();
16+
float box2dW = box2d.scalarPixelsToWorld(w/2);
17+
float box2dH = box2d.scalarPixelsToWorld(h/2);
18+
sd.setAsBox(box2dW, box2dH);
19+
20+
BodyDef bd = new BodyDef();
21+
bd.type = BodyType.STATIC;
22+
bd.position.set(box2d.coordPixelsToWorld(x, y));
23+
b = box2d.createBody(bd);
24+
25+
// Attached the shape to the body using a Fixture
26+
b.createFixture(sd, 1);
27+
}
28+
29+
void display() {
30+
fill(100);
31+
noStroke();
32+
rectMode(CENTER);
33+
rect(x, y, w, h);
34+
}
35+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mode=Android
2+
mode.id=processing.mode.android.AndroidMode

sensors/ex2_boxes/ex2_boxes.pde

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Android Mode tutorials
2+
// http://android.processing.org/tutorials/
3+
// Sensors - Boxes example
4+
5+
// Rectangles trapped inside the screen, and moving by gravity
6+
// Based on Box2DProcessing from The Nature of Code
7+
8+
import android.content.Context;
9+
import android.hardware.Sensor;
10+
import android.hardware.SensorManager;
11+
import android.hardware.SensorEvent;
12+
import android.hardware.SensorEventListener;
13+
14+
import shiffman.box2d.*;
15+
import org.jbox2d.collision.shapes.*;
16+
import org.jbox2d.common.*;
17+
import org.jbox2d.dynamics.*;
18+
19+
Context context;
20+
SensorManager manager;
21+
Sensor sensor;
22+
AccelerometerListener listener;
23+
float ax, ay, az;
24+
25+
Box2DProcessing box2d;
26+
ArrayList<Box> boxes;
27+
ArrayList<Wall> walls;
28+
29+
void setup() {
30+
fullScreen(P2D);
31+
orientation(PORTRAIT);
32+
33+
context = getActivity();
34+
manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
35+
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
36+
listener = new AccelerometerListener();
37+
manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
38+
39+
box2d = new Box2DProcessing(this);
40+
box2d.createWorld();
41+
42+
// A group of boxes
43+
boxes = new ArrayList<Box>();
44+
for (int i = 0; i < 20; i++) {
45+
Box p = new Box(random(200, width-200), random(200, height-200));
46+
boxes.add(p);
47+
}
48+
49+
// Invisible walls
50+
walls = new ArrayList<Wall>();
51+
walls.add(new Wall(width/2, -25, width, 50));
52+
walls.add(new Wall(width/2, height+25, width, 50));
53+
walls.add(new Wall(-25, height/2, 50, height));
54+
walls.add(new Wall(width+25, height/2, 50, height));
55+
}
56+
57+
void draw() {
58+
background(255);
59+
60+
// update gravity from accelerometer data.
61+
box2d.setGravity(-ax, -ay);
62+
63+
box2d.step();
64+
65+
for (Box b: boxes) {
66+
b.display();
67+
}
68+
}
69+
70+
void mousePressed() {
71+
for (Box b: boxes) {
72+
b.shake();
73+
}
74+
}
75+
76+
public void onResume() {
77+
super.onResume();
78+
if (manager != null) {
79+
manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
80+
}
81+
}
82+
83+
public void onPause() {
84+
super.onPause();
85+
if (manager != null) {
86+
manager.unregisterListener(listener);
87+
}
88+
}
89+
90+
class AccelerometerListener implements SensorEventListener {
91+
public void onSensorChanged(SensorEvent event) {
92+
ax = event.values[0];
93+
ay = event.values[1];
94+
az = event.values[2];
95+
}
96+
public void onAccuracyChanged(Sensor sensor, int accuracy) {
97+
}
98+
}

sensors/ex2_boxes/sketch.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mode=Android
2+
mode.id=processing.mode.android.AndroidMode

0 commit comments

Comments
 (0)