Closed Thread Icon

Topic awaiting preservation: FINALLY started controlling my movements with vectors Pages that link to <a href="https://ozoneasylum.com/backlink?for=8338" title="Pages that link to Topic awaiting preservation: FINALLY started controlling my movements with vectors" rel="nofollow" >Topic awaiting preservation: FINALLY started controlling my movements with vectors\

 
Author Thread
DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-08-2002 12:00

Link: http://www.bigbonfire.com/bugs/

I was out last weekened isiting a friend, he's now back in school and studying "Forensic Accounting", with a heavy emphasis on maths and computers, heavy shite that. What was fun is he has like *all* these math books, so while he did his homework, I jiggered some formulas out of the system. Like here's a simple one! Slime wanted me to do this ages ago, but I never got around to it.

Zobjects[x].top = Zobjects[x].top + (Zobjects[x].baseSpeed * Math.sin(Zobjects[x].dir));
Zobjects[x].left = Zobjects[x].left + (Zobjects[x].baseSpeed * Math.cos(Zobjects[x].dir));

Assign a speed (in pixels) and a direction (in radians? Zero to Math.PI*2 in any case), and off you go! Now all I needed to do was dig out the formulas for *2* vectors at once, something I didn'ty do. I'm trying for a gavioty effect, but my specks won't form orbits, they just crash into the sun, hrmph.

sunDir = Math.atan2(Zspot.left-Zobjects[x].left,Zobjects[x].top-Zspot.top) - Math.PI/2;

That last formula gets me the direction of the sun from the specks, and the distance is easy enough to calculate, but what am I missing? Grr, I wish I had paid more attention in school!

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 15:14

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-08-2002 15:16

Cool! it looks very smooth.

To get them to form orbits, do two things:

1. pull them towards the "sun" each step with an acceleration of fudge_factor/distance^2. Be sure to check for the case where distance = 0.
2. start them off with a velocity that's perpendicular to the sun.

You might find that it's easier to store the velocity as a vector itself, so that you don't have to use trig to find the position. Rather, you can use trig to find the acceleration, which can simply be added to the velocity (v.x += a.x; v.y += a.y). Then changing the position is similar to what you already have (obj.left += v.x; obj.top += v.y).

In fact, one thing I discovered at one point or another is that sine and cosine aren't necessary at all. The reason is this: you're finding the angle between the projectile and the sun, and then you're using sine and cosine on that angle to get horizontal and vertical components. But those horizontal and vertical components are the same ones you calculated in the first place and passed to atan2()!

I might have just confused you with all that though. Good luck =)

(BTW, radians is correct.)

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-08-2002 15:31

Slime! My man, of *course* you confused me, but not as badly as I might have feared, I've been digging through a bunch of these equations lately and they're starting to stick. You have such a perfect knack for explaining tough subjects! I've learned so many new things from you over these years; for this I give you thanks, and kudos bars, yum!

Right now I have "orbits" of a sort, but I'm actually cheating, the "pull" from the sun is actually at an angle instead of directly towards it, which causes the circling to happen. (Looks cool, but physics doesn't work that way, does it? Hmm, maybe with a swiftly rotating black hole? Gotta ask Stephen Hawking next time I see him.) I'm really anxious to make this happen, it opens up lots of cool game possibilities once I have the codebase in place.

Your pal, -doc-

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 10-08-2002 16:03

Extremly cool! Very nice work indeed.
Now I wonder what would happen with the patterns over time if every floating object had different size and a gravitational pull in proportion to it's size...

Dan
Ps
I have to agree with the Doc, Slime, your explanations are appr a billion times better than what my math teacher managed back in the early 80's...
Ds

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 16:41

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 16:47

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 20:06

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-08-2002 20:50

That was a pretty good explanation of how orbits work, actually. That's exactly what it is: the struggle between the acceleration of gravity pulling the object inwards, and inertia (the object's natural tendancy to go forwards.) When these two things work together, the object tries to move to the side, but then is pulled in a little bit, then moves to the side, and is pulled in again, causing it to go in a circle.

Illustration
Note that the "pulling" is only done on the velocity, which in turn changes the way that the object moves. The object's position itself isn't changed directly by acceleration. (acceleration changes velocity, velocity changes position.)

And all that's necessary to cause this effect is to continually pull the velocity vector towards the gravitational source. This is done by adding (the direction to the gravitational source times some constant) to the velocity vector.

[This message has been edited by Slime (edited 10-09-2002).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 20:58

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 10-08-2002 21:30

Wow. Amazing thread fellas.

Oh and a big thankyou to slime for the Picci. Mah simple brain likes pictures -- helps the medicine go down. =)

Now I'm getting an itching to nab some of the physics book from the campus library. I never thought I'd hear myself say that. Inspiration comes from the strangest places.

I'll be on holidays in three weeks and I was planning to bury myself in Director and have a crack at making some dogey games. Having a solid grasp on Vector math should come in handy. I don't suppose any of you could recommend any good books for stuff like this? I have access to the biggest library in the southern hemisphere so I should be able to get my hands on just about anything.

Oh yah. I haven't done much math since highschool. Although I did do quite a lot of trig and geomerty related math in my two years of engineering, but nothing even vaugly related to movement, t'waz doing civil engineering, sleap inducing stuff I tell ya. Actually, I've forgoten most of what I learnt then but it shouldn't take me too long to pick it back up again.

[This message has been edited by Dracusis (edited 10-08-2002).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 21:38

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-08-2002 21:59

Yeah... I never really liked high school physics text books. Didn't think they did a great explanation job. I was just lucky enough to learn from an extremely good teacher junior and senior year in high school.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 22:11

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-08-2002 22:14

Wow, InI, that's an *excellent* illustration.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 22:17

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 10-08-2002 22:41

Wow. That's amazing InI. Thanks for that; I really appreciate you taking the time to make it, even if it only took 5 minuites.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 23:03

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-08-2002 23:09

Yep, spotted that bug, you're running the init() function before it gets to writing those <div>s. My easiest hack was to just put a second <script> tag in the end of the document after the <div> tags, that'd work. (Actually, I usually load a setup() function onload, lots of stuff is better handled there, after loading.)

I've got the orbit part now, I calculate my first point where I am before the function as P(x,y), and my built-in vector sends me to the next point O(x,y). But I never get there, I calculate the pull of the planet/sun on me and apply the second vector, which places me at my final spot.

Another way to do this would be to smush both vector calculations into one pass and get a third vector, which would be my final destination.

The biggest problem I see with the first method is the "close pass" to the object, my time jumps are larger than I'd need, so I may get all messed up as I approach the closest (perigee? Appogee? Something like that.) And instead of making a nice smooth swoop around my planet, I fly off in some strange random direction. I may have to add a special case to handle that aspect.

Ideally I'll manage to get one "sun" source that works with one other "planet" object. I was imaging an OZONE GRAVLAB application, with a nice CSS shaded control panel to the right, and a square display window to the left. It'd be nice if I could just create and drag a new "sun" onto my field, which would further muck things up, or create additional new planets, affect their speed and mass, etc... Should be doable, the planets are generated as needed already, all I need to worry about is making the "gravity object" an subroutine so I could make more than one work at the same time.

I should be working. This is what I look like when goofing off!

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 23:14

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-08-2002 23:19

I'm not quite sure if what you described yourself doing is correct. This is what you should be doing:

(there should be a velocity vector remembered for each element)
// find distance to sun as a vector:
distancetosun.x = element.left-sun.left;
distancetosun.y = element.top-sun.top;
// find length to sun:
lengthtosun = Math.sqrt(distancetosun.x*distancetosun.x + distancetosun.y*distancetosun.y);
// find direction to sun as a vector of length 1:
directiontosun.x = distancetosun.x/lengthtosun;
directiontosun.y = distancetosun.y/lengthtosun;
// apply acceleration to velocity
element.velocity.x += fudge_factor/(distancetosun.x*distancetosun.x);
element.velocity.y += fudge_factor/(distancetosun.y*distancetosun.y);
//apply velocity to position
element.left += element.velocity.x;
element.top += element.velocity.y;

For the problem when things get too close to the sun, what you can do is run more iterations for those elements, simultaneously changing the fudge_factor. So if you choose to run 5 times as many iterations for an element that's within a certain distance of the sun, use fudge_factor/5.

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-08-2002 23:30

Hah! Yah, I had a feeling I wasn't quite getting this exactly right, which is part of the reason I explained my understandings again, I was guessing someone would correct me. Slime, that's a great summation of the variables and formula! So, you're telling me that each element has 2 velocity values, on the 'x' and a 'y' axes, right? That's incedible, things shouldn't be so simple, should they? Coolness!

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-08-2002 23:31

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-09-2002 00:12

Oh, that's cool. It's not physically realistic, of course (since gravity is stronger farther away), but it probably creates a cool effect.

Oh! and in my last post: the two lines calculating directiontosun are unnecessary. I just realized that. The direction to the sun isn't actuall used anywhere. Also, the lengthtosun variable isn't necessary for the same reason.

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-09-2002 01:40

OK, I do have something there that seems to work, though I also noticed that some of the lines from Slime didn't work as expected. Here's what I ended up with...

code:
// find distance to sun as a vector:
distancetosunX = Zobjects[x].left-Zspot.left;
distancetosunY = Zobjects[x].top-Zspot.top;

// find length to sun:
lengthtosun = Math.sqrt((distancetosunX*distancetosunX) + (distancetosunY*distancetosunY));

// find direction to sun as a vector of length 1:
if (lengthtosun != 0) {
directiontosunX = distancetosunX/lengthtosun;
directiontosunY = distancetosunY/lengthtosun;}

// apply acceleration to velocity
Zobjects[x].velX -= directiontosunX/lengthtosun;
Zobjects[x].velY -= directiontosunY/lengthtosun;

//apply velocity to position
Zobjects[x].left += Zobjects[x].velX;
Zobjects[x].top += Zobjects[x].velY;



I lose more than half of my planets right off the start, but they make periodic appearances along the way, they were just too close at the start so they got to much "boost".

Link: http://www.bigbonfire.com/bugs/index2.php

So! It does work as it is right now, but I'm still hoping for more of a cool "black hole, things whipping áround in a tight orbit" kinda thing. ANYways, it seems to work! Now to go and distort the laws of Physics, whee!

Your pal, -doc-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-09-2002 01:49

Close! Change

Zobjects[x].velX -= directiontosunX/lengthtosun

to

Zobjects[x].velX -= directiontosunX/(lengthtosun*lengthtosun)
(and the same for the Y vectors)

for full physical accuracy, and it should be right... I think =)

[edit: actually, no, I'm wrong. Something about the way you're doing it causes them to move more diagonally than any other direction (click randomize). Try going back to what I said before, but removing the directiontosun and lengthtosun variables - they shouldn't be necessary, and I think they're causing things to behave a little strangely.]

You may also want to multiply directiontosunX (and Y) by some constant to increase or decrease acceleration. (This is really just sort of like zooming in and out of the system, in a sense.)

[This message has been edited by Slime (edited 10-09-2002).]

RoyW
Bipolar (III) Inmate

From:
Insane since: Aug 2001

posted posted 10-09-2002 02:01

I though I might get in on this if that's OK.
My first post here was moving an object in a circle. After I posted I modified it for orbits.
I found I had to fudge the 'G' constant.

Here is the demo

I thought I might try to explain how I came up with the math (it could be wrong, its years since I did this kind of stuff)

Hope this comes out OK.

code:
Using the formula for gravitational attraction


G.m.m2
F = ---------
2
r

And newtons law of motion

F = m.A therfore A = F
---
m

And noting that
ax x
---- = ----
A r

And
ay y
---- = ----
A r

You can substitute to come up with

G.x.m2
ax = ------
3
r

G.y.m2
ay = ------
3
r

where
x = obj2x - obj1x
y = obj2y - obj1y
r = the distance of the two objects = sqrt(x*x + y*y);

ax = x component accell;
ay = y component accell;

m2 = mass of the "other" object

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-09-2002 07:16

Ow ow ow, math hurt head! Head aching, yes it is. Heh, it's late, and I've been at this for a while now. Here's my newest toy for now, until I get back to it! (Yes, I linked it from the main OZONE site, I'm getting better at that.)

Link: http://www.bigbonfire.com/bugs/

I found that one sun would tend to kick off any stray planets pretty quick, it's a miracle that we manage to circle instead of some crazy ellipse, this points that out! I found things much more stable once I dropped a second "sun" in place, now I have a binary system with 25 planets!

Options: http://www.bigbonfire.com/bugs/?howMany=99 // for those of you with better CPUs.
Options: http://www.bigbonfire.com/bugs/?howFast=1 // Affects the refresh rate. Ditto as above.

Or add 'em together, I do that for debugging a lot, run with only 2-3 planets. I had fun with this, now to think of what to do with it!

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-09-2002 08:53

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 10-09-2002 09:31

Doc! very nice work!

That reminds me of Gibson's Wizmo gravitons from grc.com

Schitzoboy
Paranoid (IV) Inmate

From: Yes
Insane since: Feb 2001

posted posted 10-09-2002 13:24

I wish I could click and drag the sun =)

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-09-2002 15:08

Coool! I love the graphics!

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-09-2002 16:28

Yep, the next bit will be to allow positioning of the sun, I was thinking of making it so you could drag new suns onto the field as you wanted, could be fun! I'm also thinking of treating the suns as orbital bodies also, that'd be kind of neat if they also tended to oprbit each other, no? First I figure I'll make the suns a bit less "hard coded", and make them into created objects, then dragging comes next I'd think. (Maybe I'll do the dragging first, that could be way neat.)

Your pal, -doc-

NoJive
Maniac (V) Inmate

From: The Land of one Headlight on.
Insane since: May 2001

posted posted 10-09-2002 17:13

"FINALLY started controlling my movements with vectors"

Most people suffering this affliction use a laxative!

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 10-09-2002 18:09

Fun with vectors:
http://tech.irt.org/articles/js214/

___________________
Emps

FAQs: Emperor

Wakkos
Maniac (V) Mad Scientist

From: Azylum's Secret Lab
Insane since: Oct 2000

posted posted 10-10-2002 09:25

Jheez guys!
we can make a big wiki with these tips here!!!!!!!

I remember that circle post Roy, very useful to me since I was making some round circle (just round, sen 1, cos 1) but I remember

Wakkos
follow the white rabbit

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-10-2002 11:50

OK, I finished this for now. I can do a lot more with the interface and controls, and I will probably use this as my next "thing", but I need to stop now, I have work to do, and Malin is getting nervous! Suns can now be dragged, and you can click the "New Sun" button to get more, but once you have too many you gotta reload for now. I was thinking it'd be nice if the suns drifted according to gravity (pseudo), I could probably just make them more "massive" by assigning them a smaller fudge_factor? That should work. I'm mostly picturing using maybe 25% of the screen to build a big control panel, with an "object creation lab" attached. Build a custom sun, set it's gravity and launch it into the system! Ditto with the planets.

I'm also thinking about how I can normalize the orbits as time passes, it's be great if those "wild swings" dissapeared and the orbits tended to find a stable point, that'd make for some sweet motions I bet. But really! I should be working. (On work. Bleh. ;-)

Your pal, -doc-

NoJive
Maniac (V) Inmate

From: The Land of one Headlight on.
Insane since: May 2001

posted posted 10-10-2002 13:26

A ton of fun and a ton of work. Certainly more than enough to keep my tiny little mind occupied for a while.

Only one blue one?

As for your next 'thing' I think it would be pretty dramatic if you could incorporate your globe from the old front end. Those spheres whizzing in and out through and around the 'strapping' would be quite the visual. I always liked that front end.

Now go sooth that nervous woman.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-10-2002 17:59

As a fun way to play with this I bet a DHTML version of This game would be neato



.:[ Never resist a perfect moment ]:.

RoyW
Bipolar (III) Inmate

From:
Insane since: Aug 2001

posted posted 10-10-2002 19:47

It looks very nice and is very interactive. It always amazes me how much effort you put into these things and make it a finished tool. I always get so far then get bored (or have to get busy with something else)

I was wondering if I could discuss your orbit calculation. Looking at your code your accelleration of each planet is based on one over the square of the distance of the object from the sun.

If you look at these lines

code:
lengthtosun = Math.sqrt((distancetosunX*distancetosunX) + (distancetosunY*distancetosunY));
directiontosunX = distancetosunX/lengthtosun;
Zobjects[x].velX -= ((directiontosunX/lengthtosun)*fudge_factor);


You can write it as

code:
lengthtosun = Math.sqrt((distancetosunX*distancetosunX) + (distancetosunY*distancetosunY));
Zobjects[x].velX -= ((distancetosunX/(lengthtosun*lengthtosun))*fudge_factor);


Which shows the accelleration is based on one over the sqare of the lengthtosun. In other words you have

code:
K.x
ax = ---
2
r



Where K = fudge_factor;

In reality the acceleration is based on one over the cube of the distance.

code:
G.m2.x
ax = ------
3
r



G is very small and m2 has to be really big for gravity to take effect. However you could say that the m2 will never change and G is a constant so you could write it as

code:
C.m2.x
ax = ------
3
r


where C is a larger constant value (fudge_factor in your code) that allows m2 to be a smaller value.

As your suns all have the same mass you can even remove m2 from the eqation and just get

code:
K.x
ax = ---
3
r



Where K = fudge_factor;

This may (or may not) produce a more realistic gravity well.

Anyway - it looks great as is. Keep on amazing us.

Cheers
Roy

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-10-2002 19:56

> In reality the acceleration is based on one over the cube of the distance.

No, it's not; It's one over the square of the distance =)

But, you did point out one important thing. Your simplified code:

code:
lengthtosun = Math.sqrt((distancetosunX*distancetosunX) + (distancetosunY*distancetosunY));
Zobjects[x].velX -= ((distancetosunX/(lengthtosun*lengthtosun))*fudge_factor);



Can be simplified even farther like so:

code:
lengthtosun_squared = (distancetosunX*distancetosunX) + (distancetosunY*distancetosunY);
Zobjects[x].velX -= ((distancetosunX/lengthtosun_squared)*fudge_factor);



This gets rid of the Math.sqrt(), which should prove to be an *immense* speedup (calculating square roots is slooooow). Doc, this means you may be able to increase the number of planets, if you want. =)

andy_j
Nervous Wreck (II) Inmate

From: uk
Insane since: Aug 2002

posted posted 10-10-2002 21:57

Wow this is fantastic stiuff. I love the demos..

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-10-2002 23:26

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

RoyW
Bipolar (III) Inmate

From:
Insane since: Aug 2001

posted posted 10-10-2002 23:57

Slime.
OK I agree. The accelleration is proportional to 1 over the square of the distance.
However - I think the x component should be over distance cubed.
Take a look
<BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre>

Forces - The force acts from mass 'm' towards mass 'm2'
fx
m----->

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-11-2002 00:03

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-11-2002 00:06

Wow, that worked perfectly! It's still kind of slow when you go crazy on the planets, especially when you add too many suns. Check it out with 99, http://www.bigbonfire.com/bugs/?howMany=99 - or add in a new number if it works for you. You can also add the control ?howFast=[x] where [x] is the refresh rate of the setInterval function. Thanks for the help, this is going to end up as a short sweet neat function, I love it!

Your pal, -doc-

RoyW
Bipolar (III) Inmate

From:
Insane since: Aug 2001

posted posted 10-11-2002 01:46

Hi Ini,
It is difficult to post equations.
m and m2 are 2 different parameters. The parameter 'm' is the mass I am moving 'm2' is the mass of the other object.
The only squared parameter is 'r'. (It is m.(r^2) but it the equation it may look like (m.r)^2 which it is not)

If I write it like this

F = (G.m.M)/(r^2)
F = m.A

m.A = (G.m.M)/(r^2)

Can't I illiminate m and just say?
A = (G.M)/(r^2)

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-11-2002 02:47

Yes, that part is right, you can cancel out the m's. (It's interesting to realize that this means that the mass of the object being pulled has no effect on how fast it accelerates.)

Now, to be perfectly honest with you, besides the fact that you're saying "sine" when you should be saying "cosine", I'm not sure if there's anything wrong with your calculations. It's possible that yours are really exactly the same as mine. At this point, though, I'm really quite confused. I'm not sure if Doc's final code is actually correct anymore, and I'm just really lost. I'd have to start over from scratch to get it right =)

OK. So I'll do that. Starting over from scratch.

a=C/r^2
(C is some constant that we don't care about exactly, related to the mass of the object. fudge_factor. whatever you want to call it.)

ax = cos(theta)/r^2
ax = x/r^3

Wait, yes, you're right! And this helps me understand what I was confused about with the Doc's code. In reality, what he has is only an acceleration proportional to 1/r.

This is why before I was saying

directiontosunX/(lengthtosun^2) // (direction, not distance, to sun; found by dividing X distance by the length to sun)

Which happens to be equivalent to

distancetosunX/(lengthtosun^3)

So, doc, in order to be fully correct, you'll have to use *directiontosun* instead of *distancetosun*. Which means you may have to bring in the direction calculating code again, which may require a square root =\

On the other hand, this may help create correct orbits.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-11-2002 07:59

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

RoyW
Bipolar (III) Inmate

From:
Insane since: Aug 2001

posted posted 10-11-2002 08:51

Hi Slime,

quote:
(It's interesting to realize that this means that the mass of the object being pulled has no effect on how fast it accelerates)


Drop a feather and a hammer on the moon - they hit the gound at the same time

I am always confused about sine (=opposite or adjascent?) over hypotenuse and cosine, thats why in my first attempt I didn't use sine or cosine, i just used the fact (x/r) == (ax/A).

Er.. anyway - Doc's demo looks cool.
I just had to find out about the calculations because I couldn't find anything in the web about how to calculate the x,y coordinate of a mass in orbit..

Cheers
Roy.


DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-11-2002 21:13

Yah, the problem with squaring the value there is that the movement, it scarcely happens at all! Whereas now, it at least appears to be somewhat proper to the eye. I'm already using this...

lengthtosun_squared = (distancetosunX*distancetosunX) + (distancetosunY*distancetosunY);
dirX = distancetosunX/Math.abs(distancetosunX);
dirY = distancetosunY/Math.abs(distancetosunY);
Zobjects[x].velX -= ((distancetosunX/lengthtosun_squared)*fudge_factor);
Zobjects[x].velY -= ((distancetosunY/lengthtosun_squared)*fudge_factor);

Are you saying I should need to square this again? Hmm, my caveman brain sees thing like "Ugh! Gravity work now, caveman scared to fix!" :-)

I've added an additional fudge factor so that things tend to slow down over time, this eventually makes all the planets tend to cluster around the stars at higher gravity, but if I didn't do that all the stars eventually get spun away, showing up only every 5 minutes or so. I have thought about calculating the direction again, so I could use it to flatten out the elliptical orbits, that would end up looking cool, I'd love to see those totally squashed ellipses turn into more circular orbits over time.

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-11-2002 21:22

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 10-13-2002 20:16

well doc, are you doing this game for fun or is this another strange job?
just wondering?

great JS work, btw.

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-14-2002 07:55

Nah, just goofing around, avoiding work, heh. I was thinking it could end up as some kind of cool space game maybe? I could set up a map of stars throught the "galaxy", I'd just have to make sure that I didn't bother calculating stars too far away otherwise the cpu-burn would be heavy. Then I guess, well, you could fly around and maybe.. shoot stuff? Or something.

I guess I was just goofing off. It'll turn into something, I'm sure!

Your pal, -doc-

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 10-14-2002 10:21

Space War was one of my favorite arcade games "back in the day".

hmm... shooting stuff? asteroids perhaps? Wouldn't that be superb!

. . : slicePuzzle

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-14-2002 19:20

Hah! Bugs, I was wondering when someone would bring up that game, I loved playing that! I was thinking maybe something that'd give me that same thrill, but with an endlessly generating universe. Even if you just "flew" around, picture the thrill as you have to slingshot past a black hole, virtually! Add in a whole mess of stuff you need to shoot along the way, well, that's game enough for me! Heck, maybe I'd even add in scoring as a perk. ;-)

Hmm, just imagined this in 3D, and got dizzy. Wish I was better at that stuff, that'd be way cool.

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-14-2002 22:14

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

RoyW
Bipolar (III) Inmate

From:
Insane since: Aug 2001

posted posted 10-18-2002 06:13

Hey Doc,
(Hope you don't mind me calling you Doc?)
I was just looking at the latest version of the script (the graphics are fantastic!) and was wondering if you had thought about adding a zoom factor. That way when "planets" fly off into the ether you can zoom out to see where they've gone.
Here is a really crude demo to show what I mean. http://www.javascript-fx.com/post/ozone/orbit/orbit3.html

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-18-2002 09:15

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 10-18-2002 11:21

Actually, I'm building at a different address now, I've been having too many problems with the bigbonfire.com domain. Look now at http://ozoniclabs.com/gravlabs/ - I hope it'll remain here for now. (Last night I had a dream about building this into a spherical, or possibly toroidal world, something you could circumnavigate. You could fly around the screen, or if you got closer to the edge, the background would scroll and you'd see new stars. Hitting planets would be bad, crash! Hitting a star would be nearly impossible, unless it was a black hole. I could set up "fuel depots" around the landscape, each with it's own gravity. (Circle it, press a key, and then land, they'll fill you up.) Run out of fuel? Drift endlessly, eventually you'll end up sucked into the wormhole at the end, or crash into a planet or black hole. Somehow you'd have to fly around and pick up [something worth points] finishing up your foray by crashing into a wormhole, leading to the next level. Levels could be generated from a data file, so this opens the possiblilty of level designers.

Sound like fun?

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-18-2002 15:02

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-20-2002 11:33

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

« BackwardsOnwards »

Show Forum Drop Down Menu