Jump to content
  • Advertisement
Sign in to follow this  
mackattac

Making a bouncing balls animation using linked lists

This topic is 687 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
for(i=0;i<=3;i++){
  list_next(iter);
  BouncingBalls(screen);
}

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Edited by mackattac

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!