Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Some questions about optimizations  (Read 1452 times)

0 Members and 1 Guest are viewing this topic.

Tofugames

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Some questions about optimizations
« on: December 02, 2012, 07:10:56 pm »
I have been using SFML for some time now but have not written anything with a large amount of objects or that is fairly complex, and have been instead trying to learn the engine and how it works by making smaller projects. I am now finally working on my first larger-scale project, and am running into the issues of optimization for the first time. Rather than specific excerpts of code, I would rather simply like some guidance and suggestions about how to optimize things in SFML.

The first thing I am trying to learn how to better optimize would be drawing. I already have a system that only renders objects that are visible in a view, however often times there are a very large amount of objects visible and I believe that drawing all of these objects is definitely having an impact on my program slowing down. Right now I draw objects in a simple for loop that goes through a vector of objects and does a RenderWindow.draw() for all of them that are on screen. I have read a bit about VertexArray but I'm not sure if it would really be much more efficient for me as all of the objects in my program are moving, and none are static. I also tried to create depth buffer of objects to avoid over-drawing, but that turned into something way more complex than I am able to understand at the moment.


I am also trying to optimize my collision detection, and after doing some reading I have found that a QuadTree sounds like my best option for doing this, so I have started working on an implementation for that. The issue I am facing is that every tutorial that I have found about QuadTrees seems to assume that every object being added is the same size, when in the case for my program this is not the case. If I have large objects that might be larger than a single node in my tree and thus require collision checks in other nodes I am not sure how to implement something that would be able to handle varying object sizes.

One last optimization I am wondering about is directional movement. Right now I use trigonometry to calculate how each object should move based on the angle it is moving in, but I've read that trigonometry is a fairly inefficient and slow method to use, although I have not found any alternatives. Are there any good and efficient methods that you know of to calculate directional movement that do not use or are more efficient than trigonometry?

These are the first optimizations that I am trying to work on, but any other suggestions for things to try optimizing would be greatly appreciated as well because this is my first time facing optimization as a real issue. Any help would be great! Thank you!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Some questions about optimizations
« Reply #1 on: December 02, 2012, 07:18:06 pm »
The first thing to do is to profile your application to find what is slow. For example, your trigonometric calls have probably no effect on the overall performances, and trying to optimize that would be a waste of time, and make your code more complicated and less maintainable.
Laurent Gomila - SFML developer

Sui

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://www.suisoft.co.uk/
Re: Some questions about optimizations
« Reply #2 on: December 03, 2012, 09:49:15 am »
Laurent is right about the profiling. You need to identify the real bottleneck in your program and focus your optimisation efforts there.

That said, chances are it is your collision detection that is the bottleneck if you have a lot of objects to test. I hit this issue with my first game (Gravity Core) and used a variant of the 'Recursive Dimensional Clustering' algorithm published in Game Programming Gems volume 2.

The first two books in the GPG series are well worth picking up. They are full of good ideas and useful code samples.

They're not too expensive if you opt for used:
http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=game+programming+gems

I can dig out some code for 2D collision checking if you like. Let me know. It will need some tailoring as it's tied into my game engine object model. To be honest the best way is to pick up the books and learn how it works from the ground up.
Gary Marples, Developer of games and other stuff.
www.suisoft.co.uk/games/BlogTwitter