I'm wondering why my code isn't working as I want it to work.
I have small header file. Called Particle.h
#ifndef PARTICLE_H
#define PARTICLE_H
#include <math.h>
#include <SDL\SDL.h>
#include <SDL\SDL_gfxPrimitives.h>
#define NUM 75
class Particle
{
public:
Particle();
~Particle();
void reset_particles(int n);
void update(SDL_Surface* screen);
void reset_all();
void attract(struct particles *A, struct particles *B);
void render(SDL_Surface* screen);
private:
struct particles
{
double mass;
double x, y;
long oldX, oldY;
long xp, yp;
double ax, ay, vx, vy;
}p[NUM];
int cx, cy;
};
#endif
#include "Particle.h"
#include "Globals.h"
Particle::Particle()
{
}
Particle::~Particle()
{
}
void Particle::attract(struct particles *A, struct particles *B)
{
double distance;
double dist, distX, distY;
double transX, transY;
//increase the vector position of particle by it's velocity it's respected direction
A->x += A->vx;
A->y += A->vy;
//calculate the distance between particles
distX = A->x - B->x;
distY = A->y - B->y;
dist = distX*distX + distY*distY;
if(dist != 0)
{
distance = 1/dist;
}
else dist = 0;
transX = distX*distance;
transY = distY*distance;
//accelerate each particle acceleration (m*d)
A->ax = -1 * B->mass * transX;
A->ay = -1 * B->mass * transY;
//increase velocity by acceleration value
A->vx += A->ax;
A->vy += A->ay;
//scale position to the screen
A->xp = cx + A->x;
A->yp = cy + A->y;
}
//reset a number of particles
void Particle::reset_particles(int n)
{
p[n].mass = 0.001;
p[n].ax = 0;
p[n].ay = 0;
p[n].xp = 0;
p[n].yp = 0;
p[n].x = rand() % width/4;
p[n].y = rand() % height/4;
p[n].vx = 0;
p[n].vy = 0;
}
void Particle::reset_all()
{
int n;
cx = width/2;
cy = height/2;
for(n=0; n<NUM; n++)
resetparticles(n);
}
void Particle::update(SDL_Surface* screen)
{
int n;
int i;
//erase old particle
for(n=0; n<NUM; n++)
{
circleColor(screen, p[n].oldx, p[n].oldy, 5, 33);
//apply gravity between every particle
for(i=0; i<NUM; i++)
{
if(i != n) attract(&p[n], &p);
}
//reset particle if it gets to far away
if((p[n].xp < 0) || (p[n].xp > 600) || (p[n].yp < 0) || (p[n].yp > 400))
reset_particles(n);
//plot new particle
circleColor(screen, p[n].xp, p[n].yp, 5, 33);
//keep track of current position
p[n].oldX = p[n].xp;
p[n].oldY = p[n].yp;
}
//draw primary partice
circleColor(screen, p[0].xp, p[0].yp, 5, 33);
}
I'm working with the book Game Programming All in One, and it uses Allegro to program by examples games. That's fine, I'm just trying to see how particles work, and how easy/difficult it is to make a particle system, and within chapter 2 there's a particle system, so I make it in Allegro no problem.
I tried to make it myself in SDL, and I get
Particle.cpp
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(13) : error C2511: 'void Particle::attract(Particle::particles *,Particle::particles *)' : overloaded member function not found in 'Particle'
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.h(13) : see declaration of 'Particle'
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(70) : error C3861: 'resetparticles': identifier not found
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(81) : error C2039: 'oldx' : is not a member of 'Particle::particles'
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.h(30) : see declaration of 'Particle::particles'
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(81) : error C2039: 'oldy' : is not a member of 'Particle::particles'
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.h(30) : see declaration of 'Particle::particles'
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(86) : error C2664: 'Particle::attract' : cannot convert parameter 1 from 'Particle::particles *' to 'particles *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(94) : warning C4244: 'argument' : conversion from 'long' to 'Sint16', possible loss of data
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(94) : warning C4244: 'argument' : conversion from 'long' to 'Sint16', possible loss of data
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(102) : warning C4244: 'argument' : conversion from 'long' to 'Sint16', possible loss of data
c:\documents and settings\c.s. finch\my documents\visual studio 2008\projects\no bs guide to sdl\part ii\breakout\breakout\particle.cpp(102) : warning C4244: 'argument' : conversion from 'long' to 'Sint16', possible loss of data
Generating Code...
is there a special way of calling structures as pointers that I'm not aware of yet ?