Bay 12 Games Forum

Please login or register.

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

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

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #870 on: October 13, 2012, 05:17:52 am »

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

You need this (like you had at the end of your struct definition):

Code: [Select]
planets planetarray[x]
...since all of your planets have to go somewhere. No need for that int array.
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 #871 on: October 13, 2012, 05:30:20 am »

Ok, so I cleaned up the code, renamed the variables, switched to using structures instead of a 2d array, made sure everything couldn't go wrong
And it went wrong.
I'm gonna go nuts, here's the code as of now:
Code: [Select]
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

int main()
{
int nop; //Number of planets
int g,j,A,B,c; //Iterators
int sloop; //No. of times sim loops

sloop = 20; //No. of times sim loops
nop = 3; //No. of planets

struct planets //Planet structure
{
float x;
float y;
float xacc;
float yacc;
int mass;
}planet[nop];

for(g=0;g<nop;g++) //Planet generation
{
float randx;
float randy;
int mass;

randx = rand() % 500 + 1;
randy = rand() % 500 + 1;
mass = rand() % 19 + 2;
planet[g].x = randx;
planet[g].y = randy;
planet[g].xacc = 0;
planet[g].yacc = 0;
planet[g].mass = mass;
cout <<"Planet: "<<g<<" x: "<<planet[g].x<<" y: "<<planet[g].y<<" Mass: "<<planet[g].mass<<endl;
}

for(j=0;j<sloop;j++) //MAIN LOOP
{
for(A=0;A<nop;A++) //INDIVIDUAL LOOP
{
for(B=0;B<nop;B++) //VALUE LOOP
{
float da = planet[B].x - planet[A].x; //Dist calcs
float db = planet[B].y - planet[A].y;
float dist = sqrt(da*da + db*db);
if(dist = 0)
{
dist = 1;
}

float angle = atan2(db, da); //Angle calcs

float force = ((planet[A].mass * planet[B].mass*0.5)/(dist*dist)); //Force calcs

float acceleration = force/planet[A].mass; //Acceleration calcs
if(A==B)
{
acceleration = 0;
}

planet[A].xacc += cos(angle)*acceleration; //Vector conversion
planet[A].yacc += sin(angle)*acceleration;
}
}

for(c=0;c<nop;c++)
{
planet[c].x += planet[c].xacc; //Updating planets positions
planet[c].y += planet[c].yacc;
cout <<"Iteration: "<<j<<" Planet: "<<c<<" x: "<<planet[c].x<<" y: "<<planet[c].y<<endl;
}

}

return 0;
}
Here's what it returns:
Spoiler (click to show/hide)

Edit:
Also skyrunner how often do you change your profile pic? :P

Edit2:
IT LIVES
OH MY GOD YES
It was a problem in the distance calc, in the if statement, causing all the distances to be 0, making all values infinite or something but in the end screwing everything over
It might have actually been working before, just it wasn't using floating point data so the additions and subtractions were too small to actually change the ints.

Yaaaaaay, thank you guys for all your help :D
« Last Edit: October 13, 2012, 07:21:19 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.

Mephisto

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

Edit:
Also skyrunner how often do you change your profile pic? :P

It's dynamic. Repeatedly refresh the page and it's different each time.
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #873 on: October 13, 2012, 04:39:56 pm »

@Singularity

I suspect you don't need to do angle calculations at all, just do (not real code):

Code: [Select]
for each Planet A begin
 for each planet B begin
  float dx = B.x - A.x
  float dy = B.y - A.y
  float dist = sqrt((dx)*(dx)+(dy)*(dy))
  float acc = B.mass/dist
  A.xacc+=acc*dx
  A.yacc+=acc*dy
 end
end

Another fun thing to try, perhaps after you play with OpenGL to display some dots for the point masses, would be to add polarization to convert it into a charge simulation then build atoms or something.
« Last Edit: October 13, 2012, 04:42:53 pm by alfie275 »
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #874 on: October 13, 2012, 08:14:57 pm »

I think you have to do

(A.mass + B.mass)/dist/dist, not just B.mass.
« Last Edit: October 14, 2012, 05:29:47 am by Skyrunner »
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

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #875 on: October 14, 2012, 04:18:37 am »

why does OpenGL seem so insanely difficult?
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

RulerOfNothing

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

I'm not sure if I have enough experience with OpenGL (especially the versions after 2.0) to help you Valid_Dark, but what are you having problems with?
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #877 on: October 14, 2012, 09:41:22 am »

I think you have to do

(A.mass + B.mass)/dist/dist, not just B.mass.

The equation for force is indeed (A.mass*B.mass)/(dist*dist) but since you want acceleration in the end, which you get by dividing by A.mass, you can just leave A.mass out of the force equation to get acceleration.
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #878 on: October 14, 2012, 04:04:58 pm »

I'm not sure if I have enough experience with OpenGL (especially the versions after 2.0) to help you Valid_Dark, but what are you having problems with?

Just all of it,   I just need to put more effort into it I guess.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

alfie275

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

Something I've found has helped is to write my own GUI library. Essentially you have a Handler class which handles everything, eg the drawing loop, setting up openGL etc. And you also have Elements, which are called by the Handler during the draw loop.
An example of an element could be a rectangle, text or a button. The key thing is that they all inherit from the base Element class so the Handler need not worry about specifics.

This kind of program structure also promotes code isolation, which makes it much easier to port to other operating systems.
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #880 on: October 16, 2012, 05:41:09 am »

Something I've found has helped is to write my own GUI library.
...but make use of what you are given as well, is my advice.

I have a horrible tendency to re-write the most basic graphics primitives[1] from the ground up with the most basic peek-n-poke graphics method I can find in a graphics library (or via a direct hardware call).  Totally circumvents (and loses the efficiency from) any inherent library functions that have all the chance in the world of being hardware-aware insofar as stuff like GPU functionality.  But it's an old, old habit from times past[2] when it was easier to write my own memory-location-setting code.

I like the control, but obviously I don't take the advantages of the rest of the prepared lib (where applicable).  (That's one reason why I'm not that much of a publisher of my own 'toy' projects!)

OTOH, it does help to understand some of the principles concerned.  (And I'm always learning new things, as I'm optimising and re-using functionality.)


[1] Solid and anti-aliased (according to an arbitrary overlap value) pixels using the most basic pixel-setting code or function, then from that solid and anti-aliased (and 'end-only' anti-aliased') lines, then from that triangle surrounds/flooding/shading/bitmapping plots (with and without anti-aliased edges, or even mixed), from which more convex shapes arise, and by which (with a little LOD and LOS ordering) I can get any form of 3D I want [and, in a couple of implementations, from which I can get any 4D I want. ;)], the limit only being processing power.

[2] You see, I always trust "object.setpixel[x,y]" or its equivalent to do exactly what I want (once I have a handle on if it's TL- or BR-origined, typically, and if that's at 0,0 or 1,1 in this particular environment, which are the most typical dislocations of system one used to encounter In The Old DaysTM!)
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #881 on: October 16, 2012, 06:30:47 pm »

OpenGL does most of it for you essentially, for example with the MSAA extension you literally use one command to turn on anti aliasing and one to turn it off.
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

ductape

  • Bay Watcher
  • MAD BOMBER
    • View Profile
    • Alchemy WebDev
Re: Programming Help Thread (For Dummies)
« Reply #882 on: October 17, 2012, 01:54:36 am »

random question:

I wonder how difficult it would be to program a game similar to King of Dragon Pass. I mean, its mostly a choose your own adventure game with some strategy stuff in there. Its seems rather basic, in terms of code. Am I totally wrong?

What seems amazing about the game is the world, the writing, the art. Why dont we have more games like this? I would love to see another one someday.
Logged
I got nothing

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #883 on: October 17, 2012, 03:38:03 am »

random question:

I wonder how difficult it would be to program a game similar to King of Dragon Pass. I mean, its mostly a choose your own adventure game with some strategy stuff in there. Its seems rather basic, in terms of code. Am I totally wrong?

What seems amazing about the game is the world, the writing, the art. Why dont we have more games like this? I would love to see another one someday.

The code would be really simple if you ask me. The hardest part, for me, would be the art work and making it all look nice.
If you just wanted an ascii clone of the original you could use any of the ascii libraries and have something playable in a matter a days and probably enjoyable not long after that.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

ductape

  • Bay Watcher
  • MAD BOMBER
    • View Profile
    • Alchemy WebDev
Re: Programming Help Thread (For Dummies)
« Reply #884 on: October 17, 2012, 03:57:12 am »

random question:

I wonder how difficult it would be to program a game similar to King of Dragon Pass. I mean, its mostly a choose your own adventure game with some strategy stuff in there. Its seems rather basic, in terms of code. Am I totally wrong?

What seems amazing about the game is the world, the writing, the art. Why dont we have more games like this? I would love to see another one someday.

The code would be really simple if you ask me. The hardest part, for me, would be the art work and making it all look nice.
If you just wanted an ascii clone of the original you could use any of the ascii libraries and have something playable in a matter a days and probably enjoyable not long after that.

Well, I thought it would be simple but I think you may be oversimplifying it a tad. I mean, the branching story trees and how each of the Council members contribute to events, etc is at least a little more that a few days of coding.

for example:
http://kingofdragonpass.blogspot.com/2012/09/how-many-scenes.html

Anyway, seems like a great game to borrow inspiration from for those who like to write, do arts, and create worlds. Wish I were focused enough and endowed with enough time to do such things, but alas.
Logged
I got nothing
Pages: 1 ... 57 58 [59] 60 61 ... 91