Sunday, April 23, 2006

Program Update


I expect anyone reading this is getting tired of me talking about my program all the time, but it's nearly finished now. And it can do 4 planets now. And colours. And Is accurate. And stuff.
I should probably mention that to do the graphics, I'm using a program called GrWin, which has a website here: http://spdg1.sci.shizuoka.ac.jp/grwinlib/english/. It's very, very useful for vector graphics, and I'd have been lost without it. Also, it's free. Kudos to the programmers who wrote it.
What I've done since I posted last night is move almost everything in function main into other functions. That separated the data from the process, and allowed me to add the other planets without having to rewrite everything. What I mean is that the procses was then generalised, and would accept any data fed into it, not just Earth[X_POS], or whatever.
That's another thing I changed. Instead of having separate variables for x and y coordinates etc, I created 1 array per planet to contain everything. That meant I only needed one parameter for my functions, not 5 or 6.
The last thing I did was the coloured trails. Previously, I had only a single colour. However, this had the disadvantage that once the planet had orbited once, you couldn't tell where it was. So I had to expand the arrays to store the last 2 positions, and then draw over them with the new colour.


Saturday, April 22, 2006

YES!

I did it! It's working! I have a planet that goes round the sun in a circular orbit once a year! Hah!
Also, I may be hyper.
Yes!

Thursday, March 16, 2006

Status report

Some more code for you to look at:

/* Gravity simulator version 3.0 by Alex Heywood*/
/* Actual units km, kg, N, days, */
#include
#include
#include
#include
#define GRAVITY 6.67e-11
#define PMASS 5.97e22
#define SUNMASS 2e22
main()
{
int days, secs;
double pX, pY, vX, vY, fX, fY, f, n;
/* Set starting Conditions */
pX = 0;
pY = 15e10;
vX = 8.9e11;
vY = 0;
days = 365;
/* Main Process */
while ( days > 0 )
{
secs = 86400;
while ( secs > 0 && (pX * pX) + (pY * pY) != 0) /* Second half of this statement prevent division by 0 */
{
f = (GRAVITY * SUNMASS * PMASS)/((pX * pX) + (pY * pY)); if (pX == 0) /* If either co-ordinate is 0, all the force will be along the othre axis. */
{ /* These if statements allow this, and prevent division of/by 0 */
fX = 0; fY = f;
}
else if (pY == 0)
{
fX = f; fY = 0;
}
else
{
n = (pX * pX) / (pY * pY);
fX = sqrt (n * f * f / (n + 1));
fY = sqrt (f * f / (n + 1));
}
if (pX > 0)
{
vX = vX - (fX / PMASS);
}
else
{
vX = vX + (fX / PMASS);
}
if (pY > 0)
{
vY = vY - (fY / PMASS);
}
else
{
vY = vY + (fY / PMASS);
}
pX = pX + vX; /* Definitely looking dodgy. What about halves? */
pY = pY + vY;
--secs;
}
--days;
printf ("x = %.2f, y = %.2f, days left = %4d \n", pX, pY, days);
}
printf ("End\n");
}
I should mention that it doesn't work properly yet. I messed around with the numbers to get it to work at all, and there's an error in it somewhere causing some strange behaviour. Still, the maths part is pretty much all there. Which leaves the graphics...
If anyone reading this has some experience writing in C or programming in general, feel free to get in touch and make suggestions.

Wednesday, February 15, 2006

The code continues...


Ok, its definately less than perfect, but its a good start.
Using the gotoxy() function, I've got it to display the rough position of the 'planet' on the screen. It doesn't show the sun but it's assumed that is at the center.
The numbers are the number of seconds left of the simulation. It starts at 70, but they overwrite each other.
It's worked better than I expected. Now I need to put a slight delay on the while loop so that I can see it's progress better. Then I'll research into graphics programs.

Monday, February 13, 2006

More code

Ok, I didn't work on the programme today, all I've got is:
/* Test of the gotoxy() function */
#include
#include
main()
{
gotoxy(10, 10);
printf("O");
}
This is probably the single least useful program ever written. What does it do? It repositions the cursor on the MS-Dos prompt.
The reason for writing this at all is because currently my gravity simulator only outputs the numbers, and the MS dod prompt can only show 52 lines. I need to output as a graphic, but at the moment I don't know how (It doesn't seem to be in the C version 4 library reference book). One option I've been looking at is a program called Delphi which I got free a while back, but that's another thing to learn.
I had hoped to get it finished today, but it got a lot harder. Hopefully I can get the graphics hooked up before school starts again, then look into calculus.

Sunday, February 12, 2006

What the Hell?

The unthinkable has happened. I've posted on my blog
(well, I did warn you in the description)
Anyway, I need to talk about programming, and this seems the best place.
Ok, I'm trying to simulate the effects of gravity on an object in a C program.
This is it so far:
#include
#include
#define GRAVITY 6.67 /* This needs to be changed to 6.67e-11 */

main()
{
long sunmass, planetmass, cycles;

float pX, pY, fX, fY, vX, vY, f, n;
/* Set starting Conditions */
planetmass = 1;

sunmass = 10;
vX = 0;
vY = 0;
printf("Initial X co-ordinate\n");

scanf("%f", &pX);
printf("Initial Y co-ordinate\n");
scanf("%f", &pY);
printf("Number of seconds\n");
scanf("%d", &cycles);
printf("%4.2f, %4.2f, %d\n", pX, pY, cycles);
/* Main process */
while ((cycles > 0) && (pX != 0) && (pY != 0)) /* Last two statements are required to avoid dividing by 0 */

{
n = (pX * pX)/(pY * pY);
f = (GRAVITY * sunmass * planetmass)/((pX*pX) + (pY*pY));
fX = sqrt(n * (f * f / (n + 1)));
fY = sqrt(f * f / (n + 1));
if (pX > 0) /* These 8 lines allow for negative values of pX and pY */
vX = vX - (fX / planetmass);
else vX = vX + (fX / planetmass);
if (pY > 0) vY = vY - (fY / planetmass);
else vY = vY + (fY / planetmass);
pX = pX + vX;
pY = pY + vY;
printf("x=%4.2f, y=%4.2f vX=%4.2f, vY=%4.2f, fX=%4.2f, fY=%4.2f, f=%4.2f, t=%d\n", pX, pY, vX, vY, fX, fY, f, cycles);
if ( (pX <> -1)) /* These 4 lines prevent a bug where after passing (0,0), the planet accelerates rapidly. */
pX = 0;
if ((pY <> -1))
pY = 0;
--cycles;
}
printf("End of Simulation\n");
}
Apologies for the messiness, it didn't copy across properly.
Pretty impressive for learning to write in C two days ago, hmm?

The problem I'm faced with now is that gravity is only calculated every second in the simulation. This has led to some blatant physics errors, such as violation of the conservation of energy principle etc.
I could just calculate it more often, but that won't solve the basic problem. I suspect that this is why Newton invented fluxions (calculus to the rest of us). So I nowhave to learn that too. Wonderful. Oh well, it was in my New Years resolution...

Wednesday, December 07, 2005

Feedback

A couple of people (real people, not internet one) have mentioned that they feel this blog is too depressing. I'll take that as a compliment to my ability to portray the world realistically.
Wasn't that depressing.
Anyway, what I actually want to say is that Civilization 4 (http://2kgames.com/civ4/home.htm) is a great game. I've been unable to tear myself away from it to do anything useful or important on my compute since I installed it. Very addictive.

Monday, October 31, 2005

Grief

Someone I knew fairly well died last week, and I've only just found out. My trombone teacher Niel was killed in an accident. I had a lot of respect for him.
This is the first time someone I've know well has died, and it's quite a shock. Since I'm not religious, I've got no illusions that his soul will go to heaven or anything like that, so possibly it's harder on me than it could be. Writing this helps though. As long as people remember him though, his life hasn't been a waste.
His funeral is tomorrow. I've run out of words.

Thursday, October 13, 2005

Forum members required

One of my friends has now got a website at http://www.gameradar.bravehost.com. If you're interested, sign up for the forum. Go on... you know you want too.

Edit: Also check out http://www.robonline.tk/

Friday, October 07, 2005

Return of the Lost Blog!

Well, it's been a while since I updated this (bloody hell: 7 months!). To tell the truth I'd forgotten about it. It's not really telling anyone anything important, so I guess it doesn't matter.
I ought to say how much I respect the people who are risking their lives to get news out of China and other oppressive countries. Freedom of Information is one of the greatest things about the internet, and is sometimes all that keeps my faith in humanity going.
It's got a lot to go against. From what I've read of George Bush (admittedly my sources aren't exactly unbiased but honestly: "I call upon all nations to stop these terrorist killers. Thank You. Now watch this drive." That borders on the sick), and of events in China, the Middle East and the US, it seems the world is sliding. Groups with in the UK scare me too (UKIP for example) There were a couple of days where a while back where I was scared of everything.
Still it's hard to believe that the world will have a new Dark Age. The point hasn't really been driven home to me yet that everything could collapse around me. Let's hope it doesn't happen.