# Topic awaiting preservation: FINALLY started controlling my movements with vectors (Page 1 of 2)

DocOzone
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted 10-08-2002 12:00

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!

InI

From: Somewhere over the rainbow
Insane since: Mar 2001

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

From: Massachusetts, USA
Insane since: Mar 2000

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 =)

DocOzone
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

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.

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

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

From: Somewhere over the rainbow
Insane since: Mar 2001

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

From: Somewhere over the rainbow
Insane since: Mar 2001

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

From: Somewhere over the rainbow
Insane since: Mar 2001

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

From: Massachusetts, USA
Insane since: Mar 2000

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

From: Somewhere over the rainbow
Insane since: Mar 2001

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 10-08-2002 21:30

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

From: Somewhere over the rainbow
Insane since: Mar 2001

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

From: Massachusetts, USA
Insane since: Mar 2000

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

From: Somewhere over the rainbow
Insane since: Mar 2001

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

From: Massachusetts, USA
Insane since: Mar 2000

posted 10-08-2002 22:14

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

InI

From: Somewhere over the rainbow
Insane since: Mar 2001

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 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

From: Somewhere over the rainbow
Insane since: Mar 2001

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
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

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!

InI

From: Somewhere over the rainbow
Insane since: Mar 2001

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

From: Massachusetts, USA
Insane since: Mar 2000

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
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

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!

InI

From: Somewhere over the rainbow
Insane since: Mar 2001

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

From: Massachusetts, USA
Insane since: Mar 2000

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
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

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".

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!

Slime

From: Massachusetts, USA
Insane since: Mar 2000

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 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
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

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.)

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!

InI

From: Somewhere over the rainbow
Insane since: Mar 2001

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 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 10-09-2002 13:24

I wish I could click and drag the sun =)

Slime

From: Massachusetts, USA
Insane since: Mar 2000

posted 10-09-2002 15:08

Coool! I love the graphics!

DocOzone
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

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.)

NoJive
Maniac (V) Inmate

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

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 10-09-2002 18:09

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

___________________
Emps

FAQs: Emperor

Wakkos

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

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

DocOzone
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

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. ;-)

NoJive
Maniac (V) Inmate

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

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

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

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 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

[1] 2Next Page »