Please tell me what's wrong with this math
in
Programming Questions
•
10 months ago
Why is the angle (varname: craftvec) which is meant to be in the direction of travel, not correct in this sketch? If you copy and run it, everything else seems alright, so what am I missing? The calculation is on line 43.
- float psize = 50;
- float pmass = 200;
- float gconst = 9.8;
- float craftmass = 2;
- float craftx, crafty, craftvelx, craftvely, craftvec, craftmag;
- void setup() {
- size(500, 500, P2D);
- craftx = 100;
- crafty = 100;
- craftvely = 2;
- craftvelx = -2;
- }
- void draw() {
- movecraft();
- background(0);
- text(degrees(craftvec), 10, 20);
- fill(200, 180, 20);
- noStroke();
- ellipse(width/2, height/2, psize, psize);
- fill(255);
- ellipse(craftx, crafty, craftmag, craftmag);
- stroke(255, 0, 0);
- strokeWeight(1);
- line(craftx, crafty, (cos(craftvec) * 20) + craftx, (sin(craftvec) * 20) + crafty);
- }
- void movecraft() {
- float gV = (((craftmass * pmass) / sq(dist(width/2, height/2, craftx, crafty))) * gconst);
- pushMatrix();
- translate(craftx, crafty);
- float accvec = atan2(crafty-height/2, craftx-width/2);
- popMatrix();
- craftvelx = (craftvelx - (gV * cos(accvec)));
- craftvely = (craftvely - (gV * sin(accvec)));
- craftx += craftvelx;
- crafty += craftvely;
- craftmag = sqrt(sq(craftvelx) + sq(craftvely));
- craftvec = atan((crafty / craftx));
- }
1