Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
Pedro Almeida
Pedro Almeida's Profile
1
Posts
1
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Trying to create a gravity system but keep getting NullPointerException and can't find the error...
[7 Replies]
09-Jul-2013 03:59 PM
Forum:
Programming Questions
Hey,
Can anyone help me understand why I'm getting a NullPointerException?
I'm new to this and from what I understand it means I didn't initialize something.. I just can't seem to find what..
I'm trying to learn create a gravitational system, here is my code:
float G_CONSTANT = 6.674;
Planet moon,earth;
void setup(){
size(500,500);
moon = new Planet(100,100,10);
earth = new Planet(300,300,30);
}
void draw(){
moon.display();
moon.gravitationPull(earth);
earth.display();
earth.gravitationPull(moon);
}
class Planet {
int x,y;
float radius = 0;
float mass;
PVector location, velocity, acceleration,gravitation;
Planet(int x_, int y_, float radius_){
x = x_;
y = y_;
location = new PVector(x,y);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
mass = PI * sq(radius);
}
void display() {
noStroke();
fill(0);
ellipse(x,y,radius*2,radius*2);
}
void gravitationPull(Planet planet_) {
PVector dist = PVector.sub(planet_.location,this.location);
float GmDividedbyMag= ((-G_CONSTANT*planet_.mass)/dist.mag());
PVector gravitation = PVector.mult(dist,GmDividedbyMag);
acceleration = planet_.gravitation;
acceleration.div(planet_.mass);
velocity.add(acceleration);
location.add(velocity);
}
}
«Prev
Next »
Moderate user : Pedro Almeida
Forum