Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 29 Aug 2000
Offline Last Active Sep 25 2014 10:49 AM

Posts I've Made

In Topic: For-loop-insanity

02 September 2014 - 12:51 AM

Classic mistake. There is a generic way to make it work, if you really want to, though:

uint8_t j = 0;

  printf("%i\n", j);
while (j++);

Which is pretty readable, since the while loop is clearly going to terminate as soon as j overflows. And like Ravyne said, it doesn't really make anything faster in general, that said I personally don't see this as an optimization tactic, but more as a type correctness thing, in other words, the loop variable is going to be used as an 8-bit unsigned integer, so let's not lie to ourselves, make it an 8-bit unsigned integer. But with this comes the responsibility of watching out for overflow and other low-level things which generally don't come up when simply using a sufficiently large signed integer like int, people will have their own opinions on that matter smile.png

Don't you mean ++j?


I just tried it on ARM7, which has only 32-bit registers, and it compiled to a compare with 256, branch if not equal. So it doesn't actually force the wrapping, but did get the logic correct, and no different than using an int and comparing with 256.

In Topic: Can't solve problems without googling?

12 August 2014 - 04:31 PM

Lots of good answers here. I especially like Glass_Knife and phantom's.


Another thing you can do is read just the overview of the solution, and try to work out the details yourself, but with a chicken exit if you need it.


Also, don't be afraid to solve the same problem more than once :) Sometimes when you have a large and complex challenge, you just have to dive head first into it and make a big mess of code. Then refactor and it looks a little better. But with your deeper understanding of the problem, you might be able to approach it from a different angle and come up with a more elegant system overall.


A good example for me is side scroller collision detection with slopes. It seems so simple, and often it is, depending on how many cases you want to handle. But it can get really complicated if you want to support slopes steeper than 45 degrees, sloped ceilings, not having off-by-one problems between left and right slopes, colliding the center of your rectangle with the slope but corner when standing at the edge of a solid tile, etc. I finally managed a system that does everything I want, but it's not quite the level of elegance I want to reach before writing an article series on it (because all the articles I've read don't solve it completely). So one more iteration, one of these days :)

In Topic: Need help calculating imprecise bullet trajectories in a 2D plane

17 April 2014 - 12:12 AM

This would be much easier working in angles rather than vectors. Use atan2 to convert vector to angle, and sin/cos to convert back to vector.


Sounds like all you need is to add a small random value to the angle of the shot. And for the AI safety, compare the angle between the AI position and the player position with the AI's facing direction. If the difference is greater than the range of the random value you add for inaccuracy, then there's no chance of the player being hit.

In Topic: How to calculate normals for a trianglemesh?

16 April 2014 - 08:57 AM

For polygons, cross product two of the edges. Like (v2-v1)x(v3-v2). Then normalize it.

Once you have all the face normals, you can calculate vertex normals by averaging all the face normals that the vertex belongs to. Easiest way to do it is to zero out all the vertex normals, then loop over all the faces and add the face normal into each of its vertices' normal. Then loop over all the vertex normals and normalize them.

In Topic: Do you have any tips to share on making/creating a level editor?

16 April 2014 - 04:05 AM

This is one of the reasons I like writing my own software rendering code for games, and only use SDL to display the final frame on the screen... I can just use the same rendering code in the editor, and dump it to the screen basically the same way.


My main advice is to watch out for feature creep. My editor has gotten a little excessively nice to use, but over 16K lines of code to wade through ohmy.png


I'm a big fan of C++ with smart pointers and RAII style programming for this kind of thing. FLTK is my current API of choice, but WxWidgets and Qt are good too.


I would recommend NOT to scrimp on undo/redo functionality, because the feeling of safety it provides makes working much more relaxing smile.png At least one level of undo/redo, but preferably multiple. My system is pretty elaborate, but I can describe it if you want.


Serialization is another big thing. That is, saving and loading from disk. I like to keep actual disk operations to a minimum, and do all my serializing in memory. For example, I have a Map class with lots of member variables, which is where all the actual editing happens, and a MapSaveData class, which is really just a single block of data, which can be created from a Map, or a Map can be created from it, or it can be written or read from disk with a single fwrite/fread.


IMO, the main reason to make your own map editor is the ability to place sprites and enter custom data for them akin to RPG Maker. If all you need is basic tilemap editing and placing of enemies in a side scroller or something, then it may not be wroth the trouble... although it will still be good learning experience if you've never done anything like it.