linked list-asteroids together

Hi, I am doing the game asteriods. I just want to link the asteroids together, so I can destroy it later in the game.
Any suggestions? Do I need an additional pointer to link the asteriods together? Right now I have a loop that draws the x,y, then the number of points. But, if I can store the information for one asteroid-that would be better. Thanks

/* Vertex object represents one point in an asteriod */

typedef struct
{
GLfloat x, y;
}VERTEX;

/* Astriod Object, number of points, and a vertex for each */
typedef struct
{
GLfloat velx,vely;
GLint numberOfPoints;
VERTEX *vertex;
}ASTERIOD;

typedef struct
{
GLint numasteriod; //number of asteriods
ASTERIOD* asteriod; //Asteriod

}SPACE;

typedef SPACE* SpacePtr;

I have seen some good code for an Astroids game and it can be modified to become the start of a great 2D game engine. (Believe me,it is just the start) The source came with the Borland C++ Version 5.0 compiler.

Anyways to summerize that code if you are aware of Object-Oriented Programming.

They created a base class with several low level properties/methods of all the objects that are in the game, such as position, momentum, flag for the destroyed object … and to allow easy access to the next object they used a pointer to the next object in the link list and a pointer to this link list which allowed objects already in the link listed to add other objects into the linked list. These items that were added were the smaller astroids and the “bullets” from the player’s ship. This class also contained a few methods of UpdatePosition (which updated every object in the linked list and checked for collisions) and another method that Displayed the link list objects. The Display method checked to see if the object was destroyed and if the flag was set it would dynamically deleted and free up the memory which that object was consuming.

They also created an astroid class along with the ship class, which inherited the methods and properties of the base object class.

This is all I can remember of the code since 1) I do not have it with me, 2) The last time I looked at the code it was ages ago.

If you want to add/remove elements from the list (not at beginning or end) then yes you would want a doubly linked list…

typedef struct element{
struct element *forward;
struct element *backward;
}Selement;

for(/find element 2 remove/
{
/*** some conditional /
temp = /
an element **/
}

backward->forward = forward;
forward->backward = backward;
free(temp);
sorta idea, hope that helped…

gav

I’m actually confused by the examples given by the net on asteroids. I understand the example on adding/deleting nodes. Any suggestions on just a linked list object of the asteriods? Is this possible?

Hello,

Gavin is right insofar as a doubly linked list is useful, but you can delete elements wiht only a singularly linked list; but it might not be feasible dependong on your circumstance.

Suppose that you have a singularly linked list of asteroids, and you want to traverse this list and perform a set of operations on it once. (Each op might be draw/collision/yada yada). You can get away with a singularly linked list in this circumstance, but you’ll need a sentinel element.

LinkedList *ptr, *prev;
ptr=start->next;
prev=start;
while(ptr) {
  if(collision(ptr)) {
    /* ack!fatal; asteroid dead */
    prev->next=ptr->next;
    delete ptr;
    ptr=prev;
  } else {
    /* draw it, or something */
    render(ptr);
  }
  
  ptr=ptr->next;  /* keep on goin' */
}

This works in this case, but a doubly linked list makes deleting with “random access” easier. (ie. you can delete nodes with jusst a single ptr to the node).

cheers,
John

Thank you for your help