Making a bouncing balls animation using linked lists

Started by
8 comments, last by Tangletail 7 years, 5 months ago

So i got this assignment which I feel i've made some progress on but I am currently stuck and hoping some kind soul could help me get unstuck.

http://imgur.com/a/XQ1vx is a picture of the assignment.

The part which I am struggling on is connecting the actual list and the bouncing ball animation and a part of the list I am pretty sure is not working correctly but not sure whats wrong.

Code is here: https://gist.github.com/anonymous/07151a2542db16a0fe9bc453351387d6

Any pointers would be appreciated!

Advertisement

Have you implemented a bouncing ball without the list yet?

Your list does not need an iterator. Don't over-complicate this.

You're not storing unknown objects here. Use a ball* instead of a void* in your node struct. When you add a node to the list you should be taking a ball pointer as an argument and attaching it to the new node.

While it is sometimes useful to track the size of a list, there is no need or requirement to do so in this case. You can simply have a node pointer as your root and ignore the size.

It looks like you don't quite know how to traverse the list. I'm guessing this is where the iterator came from. If the list is empty then the root node should be null. Otherwise it will point to the first element. If there's no next item then that element's 'next' should be null. When you need to remove the last element you'll need two pointers, one for the previous node and one for the current node. Walk through the list updating them as you go so that when the current node has a 'next' that equals null you can delete it and set prev->next = null.

Adding to the end of the list is simpler, just walk the list to the end and tack on the new node.

Walking the list to the end looks like this:


if!(root) { /* list is empty */ }

listnode_t node = root;
while(node->next) {
  node = node->next;
}

// 'node' now points to the last node.

If you need to do some work on the list items, like - for example - updating their physics or drawing them, then just pass them each to the relevant functions as you traverse the list.

Understand?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

The actual functions are given by the assignment and I need to use them, I have simply input the code into each of the functions, so I need to use the iterator unfortunately.

But yeah I think I get it, thanks for the tips.

Ah. That's unfortunate. Let us know if you run into any more trouble.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
I think I am getting close, however I have some trouble connecting it all, my plan was to:
1. Create the list.
2. add the balls to the list.
3. Run the animation once everytime the list iterates.
The code looks something like this:
list_t *list=list_create();
list_iterator_t *iter;
list_addfirst(list,ball);
list_addfirst(list,ball);
list_addfirst(list,ball);
list_size(list);
iter=list_createiterator(list);
for(i=0;i<=3;i++){
list_next(iter);
BouncingBalls(screen);
}
Where the bouncing ball function is the animation, and the ball variable is the physics of and properties of the ball. This almost works except it only spawns a ball when I attempt to close the window of the compiler.

for(i=0;i<=3;i++){
  list_next(iter);
  BouncingBalls(screen);
}

Can you walk through that on paper and trace what it will do?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Ah I see it just runs it three times it doesn't actually go through the list.

So I guess there is something that I have to do within the iterator function then?

You're going to need to:

Every Frame

--For every member

----Update member

----Draw member

You can use the iterator to step through the members, but you need to do so each frame, not just once.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

So something like this:


for(i = 0; i < SPHERE_NUMTRIANGLES; i++){
    object = CreateObject(screen,&sphere_model[i],SPHERE_NUMTRIANGLES);	
    //save each object into the list
    list_addfirst(list,object);

and now I just need to iterate through it and draw it?

If that is for when you initialize, that's fine. But if you do that every frame, it's a problem.

But yes, you'll need an iterator now. Every time you update the frame, you loop over the entire object list.

This topic is closed to new replies.

Advertisement