We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Currently working on some planetary stuff, but I don't like the formula I figured out for gravity. I don't like that I had to make different formulas for the right side of the atan and the left side. Also, when my object goes trough the center of the gravity it makes a glichy speed change (click mouse to see). (Please check line13-18 and see if you can help.) So, what's actually happening guys? And if you know better ways of calculating these stuff please share them with me. Thanks ^,^
float speedx=-0.5,speedy=-0.1,x,y=-70,g;
void setup() {
fullScreen();
rectMode(CENTER);
}
void draw() {
background(200);
translate(width/2,height/2);
ellipse(0,0,50,50);
rect(x,y,20,20);
if (x<0) {g=1/sq(dist(0,0,x,y));}
if (x>=0) {g=-1/sq(dist(0,0,x,y));}
speedx=speedx+30*g*cos(atan(y/x));
speedy=speedy+30*g*sin(atan(y/x));
x=x+speedx;y=y+speedy;
if (mousePressed) {y=50;x=0;speedx=0;speedy=0;}
}
Answers
Why not use dist()? And make the gravity correspond to the distance to the planet?
I did, in line11
What if X and y are 0, 0?
Okay, I think I figured it out. It might be a bit unrealistic but that's how framerates work (while going trough the center(for everyone to learn from)): While getting closer to the center cuz of the squareing rule the object gets so fast, that it will be further away on the other side of the gravity center, so it will not gather back as much momentum in the backwards way and that's why it will get shot out such way. Sorry for wasting your time guys.
Also thanks for Eeyorelife (seriously) cuz probably cuz of him I worked out a better formula, so you can see that edit now.
Take a look at Pvector() in the reference, and perhaps take a look at Daniel Shiffmans Nature of Code. It might be easier.
This is definetily not my area of expertise, but I don't understand why you would have to multiply the gravity by anything related to sine or cosine...
If the centre of mass is at 0,0 and you are at 10,10 then you want to move towards 0,0. So in total you want to move 10 steps to the left and ten steps up. If you normalize that vector you get a vector -1,-1. Which corresponds to one step to the left, one up.
If you are at 10,20, you want a vector -10,-20. Normalize it and you get -1,-2. A vector -1,-2 may of cours be too large, so you may want to divide by some number.
The dist() function basically returns a vector. Why not just use that?
koogs: text(x,float,float) and text(y,float,float) will show "NaN" which stands for error when dividing with zero. So umm, if there is a possibility for 2 objects meeting in their centers then it's just a case of checking if they'r both zero... but if they get so close then they will shoot out so fast, you will never find them again anyways.
it's not just a problem when they are equal, it's a problem when they get close because then 1 / sq dist gets enormous and things tend to shoot off. (slingshot effect, used to accelerate satellites)
the way around this is to add a constant to the denominator so that it cannot be 0 (or small).
but, more realistic perhaps, is to implement a collision in the code, because if that satellite is closer to the centre of the planet than the planet is wide then that's crashed into the surface.
btw, to indent code, paste it, highlight it and press ctrl-o. the button or backticks are meant for single lines, or
snippets
, not whole blocks of code.also, try adding some spaces to your code, it'll be easier to read and not the dense mass it currently is
vs
(also, atan2 is prefered - what if x is 0?)
Oh my! Thanks for sharing atan2() i didn't know there was such thing. it also calculates all possible 360 degree not like atan(). Also thx Eeyo for the PVector, I'll definitaly dig into that sometime. But you definitaly need to up your math (like me when I begun the coding of these stuff), sin and cos are essential for these kind of calculations. Thx again, I might share the full code stime in the future. Also sorry for using the wrong format, didn't know about this, also im on phone right now. Oh and thx for the staff for nicely tidying it up for me ^.^