Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 56 57 [58] 59 60 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100675 times)

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #855 on: October 12, 2012, 01:09:29 am »

I think I should try and learn another language, but I'm not xD

If you're interested in learning web programming, you could do what I do and constantly remake a simple website. It's actually kind of fun; the feeling of adding new features as you learn how to do different things is always pleasant.
I played with HTML once, got disillusioned, and gave up xD

@GE: o_O That reminds me, I need to finish learning C#... Too lazy Dx
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #856 on: October 12, 2012, 10:39:41 am »

I have also begun a mysterious construction.

I've got lots of electronic books. There's many, many GURPS books, a bunch of free tabletop games, some textbooks from school, a number of programming books, and more. They aren't always named properly and I'm too lazy to open up a terminal and rename them all manually.

Since I'm so lazy, I'm creating an ebook management system that allows you to create libraries of books, modify their information, open them in your favorite book viewer, and possibly a bit more. I don't really care about language efficiency or if the language is suited to the task. This is more of a learning exercise than anything. One thought was to use my newfound Perl skills and see what the language can do. Other than that, I may fall back on the love of my life, Python, and finally get around to some GUI programming.

I started out in C#. There are lots of nifty features and lots of things that I would need to write manually in the other languages are already there. The only problem is that portability is somewhat important to me as a user of several operating systems and a potential OSS dev. I know Mono exists, but forcing Windows users to install .NET 4 (or Mac/Linux users to install Mono) for a glorified file manager seems to be a bit overkill.
Logged

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #857 on: October 12, 2012, 01:15:52 pm »

I think I should try and learn another language, but I'm not xD

If you're interested in learning web programming, you could do what I do and constantly remake a simple website. It's actually kind of fun; the feeling of adding new features as you learn how to do different things is always pleasant.
I played with HTML once, got disillusioned, and gave up xD

@GE: o_O That reminds me, I need to finish learning C#... Too lazy Dx

Disillusioned? How so?
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #858 on: October 12, 2012, 05:01:15 pm »

use my newfound Perl skills
Oooh, so I'm not the only one with at least a passing romance with the language... ;)

I played with HTML once, got disillusioned, and gave up xD
Disillusioned? How so?
When I first read that, I imagined it was because "it's not a language", but on seeing your repeat I now realise there are no actual words there to indicate that this is the reason I thought it was.

...but that's a mix of 'advice', "wot I do" and some grumbles about bad practices, probably none of it useful to this thread or the issues at hand but I'll let it sit there (behind the armour of the spoiler-tag) in case anyone wants to criticise or correct me, and thus possibly to help better motivate Skyrunner in the process. ;)
Logged

Singularity-SRX

  • Bay Watcher
  • *shudders*
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #859 on: October 12, 2012, 11:44:25 pm »

Hey guys, I've decided to get back into coding by coding a 2d gravity sim.
Last time I coded in c++ it got overwhelming, so I gave up, but not now.
This project has a bunch of things that I wanted to learn, but couldn't bring myself to do so (like arrays and functions).

It doesn't display an actual 2d field, just the raw information, which I was going to put into excel and plot onto a graph.

I'm having a few problems though, mainly that the x/y xacc/yacc info gets screwed up beyond comprehension.

The IF statement in the VALUES loop is to make sure that it doesn't compute values against itself (the program iterates through each planet and calculates the force/dist/acc for every other planet, then applies it to itself).
But when the if statement is there, no x/y value changes at all.
When it's NOT there, the x/y vals for each planet go to the highest or lowest value possible.

Sorry if my code is really messy as well, still a youngin at C++.
Quote
Code: [Select]
#include <iostream>
#include <cmath>
#include <stdlib.h>
#define PI 3.14159265
using namespace std;

int getDegrees(float a)
{
float b = a*180/PI;
return(b);
}

int main()
{
int x; //Number of planets
int g,j,n,m;         //Iterators
int i; //No. of times sim loops

x = 3; //No. of planets
i = 20; //Number of times sim loops

int planets [x][5];

for (g=0; g<x; g++) //Generation loop
{
float randx;
float randy;
int mass;
float acc;

acc = 0;
randx = rand() % 500 + 1;
randy = rand() % 500 + 1;
mass = rand() % 19 + 2;
planets[g][1] = randx;
planets[g][2] = randy;
planets[g][3] = acc;
planets[g][4] = acc;
planets[g][5] = mass;
cout <<"Planet: "<<g<<" x: "<<planets[g][1]<<" y: "<<planets[g][2]<<" Mass: "<<planets[g][5]<<endl;

}

for(j=0;j<i;j++) //MAIN LOOP
{
for(n=0;n<x;n++) //INDIVIDUAL PLANET LOOP
{
for(m=0;m<x;m++) //VALUES LOOP
{
float a = planets[m][1] - planets[n][1]; //this
float b = planets[m][2] - planets[n][2]; //and this are just to make the dist calc shorter
float dist = sqrt(a*a + b*b);
float angle = getDegrees(atan2(planets[m][2]-planets[n][2], planets[m][1]-planets[n][1]));
float force = ((planets[n][5]*planets[m][5])*0.5/(dist*dist));
float accel = force/planets[n][5];

if(planets[n]==planets[m])
{
accel = 0;
}

planets[n][3] += cos(angle)*accel;
planets[n][4] += sin(angle)*accel;
}

planets[n][1] += planets[n][3]; //add x acc to x
planets[n][2] += planets[n][4]; //add y acc to y
cout <<"Iteration "<<j<<" Planet: "<<n<<" x: "<<planets[n][1]<<" y: "<<planets[n][2]<<endl;
}
}
return 0;
}

Also I should mention the the array I'm using to store the planets in (planets[ x][5]) uses the first dimension to store each planet, and the second to store x, y, xacc, yacc and mass in that order.
Logged
Quote from: Vertigon
slamming his left hand into the baby's face
I came to this forum with no intention of ever uttering that phrase.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #860 on: October 13, 2012, 12:09:09 am »

Why do you use an array to store the information? Is there a specific reason?
Wouldn't

Code: [Select]
struct planet
{
int x;
int y;
float xacc;
float yacc;
int mass;
}

work just the same, but with better legibility? Or use

Code: [Select]
enum class INDEX {X = 0; Y = 1; XACC = 2; YACC = 3; MASS = 4}
Code: [Select]
planets[g][INDEX::X]
planets[g][INDEX::Y]
planets[g][INDEX::XACC]
and so on.

Also, since you said
Code: [Select]
int planets [x][5];
Code: [Select]
planets[g][1] = randx;
planets[g][2] = randy;
planets[g][3] = acc;
planets[g][4] = acc;
planets[g][5] = mass;
should start with [0]. I don't see how you didn't get an out-of-bounds error >.> Remember the zero-indexing of C++.

Also, can you describe how you are doing the gravity calculations? >_<
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Singularity-SRX

  • Bay Watcher
  • *shudders*
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #861 on: October 13, 2012, 12:31:44 am »

All the stuff you said at the start of the post scares and confuses me.
If you run the code with the main loop commented out, it just runs the generation code.
At the end of it it prints the values of each planet, their x,y and mass. At the start I was doing it so that it was planets[g][0] through to planets[g][4] but that returned a bunch of whacked out values.

The gravity calcs are this:
It has 2 loops, the first one goes through each planet, and the second one goes through each planet again but doing the calcs from the first planets view, so it has things like a=F/m where m would be the mass of the first planet (aka the first loop).

The algorithm works out the dist between planets[n] and planets[m], as well as the angle planet [m] has to planet [n]
The force is the gravitation formula without the gravitational constant ((m1*m2*)/distance^2)

Acceleration is the Force/mass

It then works out the xacceleration and yacceleration with the bit at the end of the values loop; xacc = cos(angle)*accel, yacc = sin(angle)*accel

In the individual loop, it then adds the xacc and yacc to the values stored in the array.
xacc can be negative, so it evens out the forces if there are multiple planets around the one it's currently going through.

The if statement should just make a single accel value become 0, and because it adds it to the overall xacc/yacc, it shouldn't affect it.

If it's confusing, I understand, I spent all day working on this, just trying to get my head around it hahahah ^^
Logged
Quote from: Vertigon
slamming his left hand into the baby's face
I came to this forum with no intention of ever uttering that phrase.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #862 on: October 13, 2012, 12:43:09 am »

cos and sin take angles in radians, not degrees (so you shouldn't be converting the calculated angle to degrees). It is possible to work out the x and y components of the acceleration without using trigonometry by using vector math instead.

Basically, xacc=accel*(a/dist) and yacc=accel*(b/dist) using the variable names in your source code. These are the projections of the acceleration vector onto the x and y axes.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #863 on: October 13, 2012, 12:48:37 am »

...Why would a suggestion to use structures instead of hard-to-read array index numbers scare you? D:

They're really nice.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Singularity-SRX

  • Bay Watcher
  • *shudders*
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #864 on: October 13, 2012, 01:41:16 am »

Never used them before hahahaha ^^

I tried your suggestion rulerofnothing, but it was still giving nutsy reults.
Fixed up the angles problem though, thanks for that.

Also noticed a few problems where the dist would become 0 and therefore it would be dividing by 0, giving infinite results.

Still getting wacky numbers though :\
Logged
Quote from: Vertigon
slamming his left hand into the baby's face
I came to this forum with no intention of ever uttering that phrase.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #865 on: October 13, 2012, 02:07:11 am »

Another thing I noticed is that you are not clearing the acceleration values between each simulation loop, so I am guessing that this is causing the values to have a bigger error with every loop. I'm also going to second Skyrunner's suggestion to have the second array index range from 0 to 4 instead of 1 to 5 (or at least replace the [5] with [6])

EDIT: I just noticed you are comparing the planet arrays in the VALUE LOOP's if statement, instead of the indexes (so you want if(m==n) instead of if(planets[m]==planets[n]))
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #866 on: October 13, 2012, 02:30:37 am »

http://www.cplusplus.com/doc/tutorial/structures/  Data structures.

Writing your planet arrays as planet[1][1], planet[1][2] makes it harder for other people to read, and for you to instantly know, compared to planet[1].X, or planet[1].xacc.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Singularity-SRX

  • Bay Watcher
  • *shudders*
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #867 on: October 13, 2012, 05:00:39 am »

@RulerOfNothing
I thought that the accel value resets to a new number each time on the loop due to
Code: [Select]
float accel = force/planets[n][4];?
And yeah, I cleaned up the array indexes, and it looks like i'll be cleaning them up a bit now due to the fact that those data structures look pretty simple to implement, thanks for that :)

EDIT: I just noticed you are comparing the planet arrays in the VALUE LOOP's if statement, instead of the indexes (so you want if(m==n) instead of if(planets[m]==planets[n]))
I fixed that up just in case, but doesn't it technically do the same thing? As in compare the 2 planets or compare the numbers?

Edit: And just out of curiosity, is anyone else working on anything?
EditNumoroDos:
If I have this:
Code: [Select]
struct planets
{
float x;
float y;
float xacc;
float yacc;
int mass;
}planets[x]
do I still need this:
Code: [Select]
int planets[x]
?
« Last Edit: October 13, 2012, 05:09:49 am by Singularity-SRX »
Logged
Quote from: Vertigon
slamming his left hand into the baby's face
I came to this forum with no intention of ever uttering that phrase.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #868 on: October 13, 2012, 05:04:30 am »

Whoops, I meant the velocity values. And I'm not really working on anything at the moment.
Logged

Singularity-SRX

  • Bay Watcher
  • *shudders*
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #869 on: October 13, 2012, 05:15:47 am »

That's intended, as this adds up the ALL the forces it would have against any other objects; for example if you had a planet to the left of the one currently being computed, and one to the right of it, it adds up the forces, so if the forces were equal, and at equal distances, at equal angles, the planet wouldn't move at all.
I DON'T want it to calculate the force of a planet it's going against, update it's position, then do the calculations for the next one, as it would appear to be ahead of the others.
Admittedly each object gets updated in sequence, so the first planet to be generated at the start would be the one slightly ahead of all the others.

Ideally it works out all the forces, for each planet against every other planet, and at the end of the iteration it updates ALL the planets positions, then moves on to the next iterations and does it all again.
Logged
Quote from: Vertigon
slamming his left hand into the baby's face
I came to this forum with no intention of ever uttering that phrase.
Pages: 1 ... 56 57 [58] 59 60 ... 91