PVector and Android Processing
in
Android Processing
•
2 years ago
Hi all
I've recently been working on a simple game made with Processing. My goal was to work a project from start to finish and end up with something that ran well on Android. I was disappointed to find that when I loaded my sketch to the device i ran into OutOfMemory errors and after I got past those I found the whole thing ran at about 1fps when I'd been getting a steady (in fact capped at) 50fps on my desktop, I realise there is really no comparing the two performance wise so I decided to strip away as much as possible to see what was causing the problems, and at it's barest bones I still couldn't get the sketch running at an even semi-reasonable framerate.
As a result I decided that I would run a test. Following is a simple sketch based around Dan Shiffman's introduction to PVector that runs with with ease capped at 50fps on my desktop, yet when I switch to Android mode, i start averaging about half of that.
Can anyone explain why? Is PVector very computationally expensive? Or am I missing something?
Thanks in advance, code as follows.
Dave
Mover mover;
void setup() {
size(480, 800);
background(255);
mover = new Mover();
}
void draw() {
noStroke();
frameRate(50);
fill(255, 10);
rect(0, 0, 480, 800);
mover.update();
mover.checkEdges();
mover.display();
println(frameRate);
}
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
Mover() {
location = new PVector(240, 240);
velocity = new PVector(0, 0);
acceleration = new PVector(-0.001, 0.01);
topspeed = 10;
}
void update() {
acceleration = new PVector(random(-1, 1), random(-1, 1));
acceleration.normalize();
acceleration.mult(random(2));
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display() {
stroke(0);
fill(175);
ellipse(location.x, location.y, 16, 16);
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
}
else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
}
else if (location.y < 0) {
location.y = height;
}
}
}
void setup() {
size(480, 800);
background(255);
mover = new Mover();
}
void draw() {
noStroke();
frameRate(50);
fill(255, 10);
rect(0, 0, 480, 800);
mover.update();
mover.checkEdges();
mover.display();
println(frameRate);
}
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
Mover() {
location = new PVector(240, 240);
velocity = new PVector(0, 0);
acceleration = new PVector(-0.001, 0.01);
topspeed = 10;
}
void update() {
acceleration = new PVector(random(-1, 1), random(-1, 1));
acceleration.normalize();
acceleration.mult(random(2));
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display() {
stroke(0);
fill(175);
ellipse(location.x, location.y, 16, 16);
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
}
else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
}
else if (location.y < 0) {
location.y = height;
}
}
}
1