Particle Syestem
Author |
Message |
Tallguy
![](http://compsci.ca/v3/uploads/user_avatars/515706924539b443d32a6e.gif)
|
Posted: Fri Nov 04, 2011 1:32 pm Post subject: Particle Syestem |
|
|
Hello all,
Been awhile since I've been on the forums, last time i was doing turing now C!!!!!!!!!!!!!! (College foe Compsci Engineering)
My question:
I need to implement a linked List and create a Particle Syestem with characteristics of my design, are there any tutorial to this, i need to OpenGL to run it, I have no idea of how to start with my profs instructions, can anyone point me in the right direction? not do it for me....
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
02_CST8234_Particles_V1.0.pdf |
Filesize: |
160.13 KB |
Downloaded: |
1985 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Fri Nov 04, 2011 2:30 pm Post subject: RE:Particle Syestem |
|
|
First, the assignment only says that "A front end in OpenGL will be posted, so you could visualize your particle system". You don't need to write ANY OpenGL.
This assignment is basically "write a linked list" with some "particle" sugar on top. Start with the particle struct (given) and implement the functions in the order given.
The first function, particle_init, is very simple: just populate the values in the given particle struct. For now, just use some sensible defaults: make your particles red, at (0,0,0) with speed and direction (0,0,0). Make sure that the "next" pointer is NULL.
The second function, particle_add, is where you start dealing with the linked list implementation. You are given the head of the list, and you want to add a new particle to the end of the list. You will have three main steps:
1. Find the end of the existing particle list using the "next" pointer.
2. Create a new particle using your particle_init method.
3. Set the "next" pointer of the last part of the list to point to your new particle.
I'll leave the rest up to you; if you need more help later on, post back here.
|
|
|
|
|
![](images/spacer.gif) |
Tallguy
![](http://compsci.ca/v3/uploads/user_avatars/515706924539b443d32a6e.gif)
|
Posted: Thu Nov 10, 2011 2:01 pm Post subject: Re: Particle Syestem |
|
|
I have an issue of having all the particles showing up, if i put the constant as 10, i only see about 8 or so? I took of the transparent color and each x,y is rand so none should be riding on top of each other.
code: |
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DELTA_LIFE_SPAN 1
#define DFLT_INIT_NUM_PARTICLES 1000
#define GRAVITY 3
struct color{
float r;//red
float g;//green
float b;//blue
float a;//alpha
};
typedef struct color Color4;
struct vector{
float x;
float y;
float z;
};
typedef struct vector Point3D;
typedef struct vector Vector3D;
struct particle{
Color4 col;
Point3D pos;
Vector3D dir;
Vector3D spd;
int lifespan;
int size;
struct particle* next;
};
int particle_init(struct particle* p);
int particle_add(struct particle **head);
int particle_remove(struct particle* p);
int particle_destroy(struct particle **head);
int particle_update(struct particle **head);
int particle_init(struct particle* p){
float x,y,z;//position xyz variables
float sx,sy,sz;//speed
float dx,dy,dz;//direction
//screen is 600 by 600
//srand(time(NULL));
x = (rand () % (1)+1);//poistion///mess with this
y = (rand () % (1)+1);//poistion//ditto
z = (rand () % (1)+1);//poistion//ditto
sx= (rand () % (2)+1);//speed
sy= (rand () % (2)+1);//speed
sz= 0;
dx= (rand () % (3)-1);//direction
dy= (rand () % (3)-1);
/*while(dx ==0||dy == 0){
dx= (rand () % (2)-1);
dy= (rand () % (2)-1);
}*/
dz= 0;//(rand () % (2+1)+1)-2;
p->col=(struct color){(rand () % (1+1) +1) -1,(rand () % (1+1) +1) -1, (rand () % (1+1) +1) -1, 1};//color
p->pos=(struct vector){x,y,z};//postion
p->spd=(struct vector){sx,sy,sz};//speed
p->dir=(struct vector){dx,dy,dz};//direction
p->size = 5;//sze
p->lifespan = rand () % (20+1)+1;//lifespa
}
int particle_add(struct particle **head){
struct particle *newnode = malloc (sizeof(struct particle));
newnode->next = *head;
particle_init(newnode);
*head = newnode;
return 0;
}
int particle_remove(struct particle* p){
}
int particle_destroy(struct particle **head){
}
int particle_update(struct particle **head){
struct particle *p = *head;
while(p != NULL){
p->lifespan -= DELTA_LIFE_SPAN;
p->pos.x += (p->spd.x * p->dir.x);
if (p->pos.x > 100 || p->pos.x < -100){//boundry checker
p->dir.x *= -1;
}
p->pos.y += (p->spd.y * p->dir.y);
if (p->pos.y > 100 || p->pos.y < -100){
p->dir.y *= -1;
}
p->pos.z += (p->spd.z * p->dir.z);
p = p->next;
}
}
|
I have a feeling it is with this function
code: |
int particle_update(struct particle **head){
struct particle *p = *head;
while(p != NULL){
p->lifespan -= DELTA_LIFE_SPAN;
p->pos.x += (p->spd.x * p->dir.x);
if (p->pos.x > 100 || p->pos.x < -100){//boundry checker
p->dir.x *= -1;
}
p->pos.y += (p->spd.y * p->dir.y);
if (p->pos.y > 100 || p->pos.y < -100){
p->dir.y *= -1;
}
p->pos.z += (p->spd.z * p->dir.z);
p = p->next;
}
| but am unsure
i will attach the parent class we are using (teachers code)
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
particles_OpenGL.c |
Filesize: |
9.27 KB |
Downloaded: |
357 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Thu Nov 10, 2011 2:50 pm Post subject: RE:Particle Syestem |
|
|
Hmm...try running your code with velocities set to (0,0,0) and count the number exactly. I suspect you'll find that there are exactly 9 (not 8, not 10).
The reason I say that is that I'm pretty sure I found a bug in the supplied OpenGL code. It's fairly obvious even if you don't know OpenGL: look at particles_OpenGL.c on lines 243 and 253. Note that the while condition is wrong and won't draw the last particle in the list (which has next == NULL).
I don't see a problem with your code, except that it could be formatted and commented a bit better.
|
|
|
|
|
![](images/spacer.gif) |
Tallguy
![](http://compsci.ca/v3/uploads/user_avatars/515706924539b443d32a6e.gif)
|
Posted: Thu Nov 10, 2011 3:43 pm Post subject: RE:Particle Syestem |
|
|
but if you even put like 1000 particles in, only about 20 show
|
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Thu Nov 10, 2011 5:15 pm Post subject: RE:Particle Syestem |
|
|
By the way, please be more exact about what you see. Details matter!
Okay. Clearly we need to go back to the basics to solve this. There are a few realistic possible problems:
1. The list implementation is wrong.
2. The particles aren't visible for some reason, though they are in the list.
Let's assume for the moment that #1 is not happening, since your implementation of that looks reasonable. So we'll investigate just #2 for the moment. First, we want to eliminate the trouble of things moving around, so comment out the contents of particle_update() for the moment. Second, we want to make sure things appear in a recognizable pattern, so change your particle_init() from using random numbers to using an increasing series of numbers. For example, let's put all the particles along the line from (0,0,0) to (n,0,0), and let's only look at a very small number, such as 2.
You can set the speed and direction vectors to be (0,0,0).
With those changes, exactly how many particles do you see on screen? Where are they, relative to the window? Feel free to post a screenshot.
|
|
|
|
|
![](images/spacer.gif) |
Tallguy
![](http://compsci.ca/v3/uploads/user_avatars/515706924539b443d32a6e.gif)
|
Posted: Fri Nov 11, 2011 8:54 am Post subject: Re: Particle Syestem |
|
|
Thanks for your help man, Sorry for not being specific and kinda hasty on my last post so stressed out with college ATM..I think i got it......I fixed up my teachers code (will post) I changed the GLUT imports for mac settings (wont work on Linux) VM drags down my system.
I was NOT thinking when doing this..more coffee
so this is my code ATM i need to add a delete list function and delete one node function
TY for your man, much appreciated
/*format for some reason is messed when i post :/*/
code: |
/*
Program: A02
Author: Alex van der Mout <040 659 061>
Keithton Li <040 677 250>
Date: 11/11/11
Purpose: Create a particle syestem implementing a sinlge linked list
Difficulty Level: 3
Correlation: I believe so.
Completion: 100%
Notes: Interesting, had a few problems with the assigmnet
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DFLT_INIT_NUM_PARTICLES 1000
/*********************************************
Declares all my structs that will be used to
creat each particle and run them
*********************************************/
struct color{
float r;//red
float g;//green
float b;//blue
float a;//alpha
};
typedef struct color Color4;
//X Y Z coordinates
struct vector{
float x;
float y;
float z;
};
typedef struct vector Point3D;
typedef struct vector Vector3D;
struct particle{
Color4 col;
Point3D pos;
Vector3D dir;
Vector3D spd;
int size;
struct particle* next;
};
/*********************************************
Tried to make my own keybaord control function
*********************************************/
typedef int BOOL;
#define TRUE 1
#define FALSE 0
static BOOL keyDown = FALSE;
int particle_init(struct particle* p);
int particle_add(struct particle **head);
int particle_remove(struct particle* p);
int particle_destroy(struct particle **head);
int particle_update(struct particle **head);
//void keyUp (unsigned char,int,int,struct particle* p);
/*****************************************************************************
/* FUNCTION : particle_init
/* PURPOSE : initialize the properties of a single particle
/* INPUT : pointer to the particle structure to be initialized
/* OUTPUT : returns -1 on error, 0 on success
/* NOTES : Initalizes each partical that will be displayed
to screen. Each ones gets a color, starting
position, a direction and a speed
******************************************************************************/
int particle_init(struct particle* p){
float x,y,z;//position xyz variables
float sx,sy,sz;//speed
float dx,dy,dz;//direction
//screen 100 by 100
x = 0;
y =0;
z =0;
sx= (rand () % (2)+1);//speed
sy= (rand () % (2)+1);
sz= 0;
dx= (rand () % (8)-4);//direction
dy= (rand () % (8)-4);
while(dx ==0||dy == 0){
dx= (rand () % (8)-4);
dy= (rand () % (8)-4);
}
dz= 0;
//p->col=(struct color){(rand () % (1+1) +1) -1,(rand () % (1+1) +1) -1, (rand () % (1+1) +1) -1, 1};//color
p->col=(struct color){1,0,0,1};//sets color to red for testing
p->pos=(struct vector){x,y,z};
p->spd=(struct vector){sx,sy,sz};
p->dir=(struct vector){dx,dy,dz};
p->size = 5;//sze
}
//*****************************************************************************
/* FUNCTION : particle_add
/* PURPOSE : add a particle to the dynamic particle linked list
/* INPUT : struct particle *head. Head of the particle list
/* OUTPUT : returns -1 on error, 0 on success
/* NOTES : Calls particle_init()
*****************************************************************************/
int particle_add(struct particle **head){
struct particle *newnode = malloc (sizeof(struct particle));
newnode->next = *head;
particle_init(newnode);
*head = newnode;
return 0;
}
/*****************************************************************************
/* FUNCTION : particle_remove
/* PURPOSE : remove a specific particle from the dynamic particle linked list
/* INPUT : pointer to the particle to remove
/* OUTPUT : returns -1 on error, 0 on success
/* NOTES : Particle can be situated in any place in the list.
/* Usually deleted because the lifespan ran out
*****************************************************************************/
int particle_remove(struct particle* p){
}
/******************************************************************************
/* FUNCTION : particle_destroy
/* PURPOSE : free memory used by the dynamic particle linked list
/* INPUT : struct particle **head. Head of the particle list
/* OUTPUT : returns -1 on error, the number of particles destroyed on success
/* NOTES : removes all particles from the list
Calls particle_remove()
*****************************************************************************/
int particle_destroy(struct particle **head){
}
/******************************************************************************
/* FUNCTION : particle_update
/* PURPOSE : update the particles properties to be rendered in the next frame
/* INPUT : struct particle **head. Head of the particle list
/* OUTPUT : returns -1 on error, 0 on success
/* NOTES : Tried to have it so each time the user presses a key
the particles would return to orgin
****************************************************************************/
int particle_update(struct particle **head){
int reset;
struct particle *p = *head;
while(p != NULL){
//glutKeyboardUpFunc(keyboard);
p->pos.x += (p->spd.x * p->dir.x);
if (p->pos.x > 100 || p->pos.x < -100){//boundry checker
p->dir.x *= -1;
}
p->pos.y += (p->spd.y * p->dir.y);
if (p->pos.y > 100 || p->pos.y < -100){
p->dir.y *= -1;
}
p->pos.z += (p->spd.z * p->dir.z);
p = p->next;
}
}
|
I do want to add a function that calls 'keyboard' in the openGL code so that when any key is pressed all particles return to origin (0,0) and then continue moving..if time permits, but my partner has to do some code.. lol
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
particles_OpenGL_Mac.c |
Filesize: |
9 KB |
Downloaded: |
375 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Fri Nov 11, 2011 10:38 am Post subject: RE:Particle Syestem |
|
|
No problem. I'm also impressed that you managed to modify OpenGL settings without screwing anything up -- that's a bit of a feat even for an experienced developer.
Word of caution though: you may want to avoid posting full name / student ID combinations on a public website.
I also have to say, I'm confused by these method signatures. For example, particle_remove can be done from the signature given, but it's not obvious how and it's frankly kinda dangerous (it involves deleting a node other than the one you were told to delete, I think). Additionally, particle_update gets called with struct particle **head, but should probably be called with struct particle *first, because it shouldn't be modifying the head pointer (your code doesn't, but it is possible).
And then there's the (incredibly obvious) bug in the linked-list code in the code your teacher supplied. Let's just say I'm a little less-than-impressed with my first taste of Algonquin here.
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tallguy
![](http://compsci.ca/v3/uploads/user_avatars/515706924539b443d32a6e.gif)
|
Posted: Fri Nov 11, 2011 11:42 am Post subject: RE:Particle Syestem |
|
|
Whoops my bad on the header,(can't edit my code to delete my name etc, gotta wait for Tony or Dan to do it lol) We just changed the while loop conditions within our prof's source code
Yea...those method signatures..working on them
Agian? I will take at look at her code, not too good with linkedList yet...LOL most of our profs are RLY good, just happens to be that my prof isnt one of them...
|
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Fri Nov 11, 2011 2:26 pm Post subject: RE:Particle Syestem |
|
|
Not "again", just the bug I pointed out earlier. That's a rookie-level mistake, and should easily be caught by testing.
|
|
|
|
|
![](images/spacer.gif) |
Tallguy
![](http://compsci.ca/v3/uploads/user_avatars/515706924539b443d32a6e.gif)
|
Posted: Fri Nov 11, 2011 3:08 pm Post subject: Re: Particle Syestem |
|
|
Awesome man, thanks for all your help.
Got all issues fixed and program works!!
-run the make file 'make'
-press 'o' to make all particles go back to the origin (0,0)
-each one has a random live time, will disappear after a few seconds
**Linux run cmd only ATM**
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
Assign2.zip |
Filesize: |
24.72 KB |
Downloaded: |
363 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
|
|