Do you put code in your engine/game you don't fully understand?

Started by
23 comments, last by bioglaze 9 years, 3 months ago
I do this very rarely but if I do it is math code.
Especially all this vector and matrix magic stuff.

However those code snippets do often way more then I need so I come back later someday when I have the muse.

Going line by line eliminating code lines I do not need because the outcome would be always the same in my case reducing the overall code size often a lot after iterating over it multiple times.
Then I check if the code is still working.

If it does I try to understand what happens now.
This has the advantage that
1# The code size is smaller and so more easy to understand, often also with less variables used/needed
2# By eliminating code line by line I already got an indepth view about what seems important and what is propably not
3# Less code - better performance
Advertisement

from a quick glance at the code you linked to, it appears they express the two vectors as follows:

a start point, and a unit vector in the desired direction, times a constant magnitude.

they then assume the two vectors intersect, and solve for the two constants when the resulting endpoints are the same:

unit_vector_1 * constant_1 = unit_vector_2 * constant_2

i'd find a book on vector math, use a different better documented algo, black box test thoroughly, or find a math nut who memorizes this kind of stuff.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php


So as the title suggest, do you copy code from the internet and put it in your game/engine without fully understanding it?

Sometimes. Less and less as time goes on. I do always go back, however, and work on that code later, to gain an eventual understanding.

But I bet swiftcoder understands what the code is doing and could write it from scratch if needed.

There is a big difference between using someone else's code (be it library or coworker) without having reviewed it, and using code that performs concepts that you don't understand. I don't want to write a Vector library or a PNG file loader if I don't have to. I am happy to use one. But I COULD write my own if I needed to. I think using using a library to perform tasks you do not understand will eventually come around and get you. It will be fine if nothing is wrong.

For example, a long time ago I needed to do a TFTP in Java. I grabbed a library from Apache. It ended up that there was a bug in the code that wouldn't ever allow the connection to work. I don't remember the specifics, but it was a nasty bug inside even nastier code ported from C. There were actually a bunch of gotos in the Java code.

I went out and found it: http://commons.apache.org/proper/commons-net/jacoco/org.apache.commons.net.tftp/TFTPClient.java.html#L129

Long story short (too late), if I didn't understand the underlying technology, not only would I have not known I was doing everything right, I wouldn't have been able to fix the bug.

There is nothing wrong with using someone else's code, but doing so without knowing you could write it yourself is a shortcut that will be paid for, one way or another.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532


But I bet swiftcoder understands what the code is doing and could write it from scratch if needed.

For the most part, yes. Some things (for example, quaternion conversions) continue to be a little black-box to me, though I've mostly farmed those out to 3rd party libraries (cml, in this instance).

However, I think your point applies almost as much to 3rd party libraries as it does to copy/pasted code. At least in the copy/paste case the components are usually small enough that one can understand them easily. If there is a bug in some upstream dependency like a physics engine, my ability to sit down and write a new physics engine is not quite so good :)

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I use a set of Simplex noise functions I found online as a black box for procedural generation. It has a lot of math operations that may or may not be optimizable to my benefit, but I usually leave it alone. Although I did have to port it from Java to C#. There is also using Magic Bits for encoding and decoding Morton codes for interleaved values. It is harder to understand than the naive implementation, but it's also much quicker.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor


If there is a bug in some upstream dependency like a physics engine, my ability to sit down and write a new physics engine is not quite so good

Yes, but you see my point.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

So as the title suggest, do you copy code from the internet and put it in your game/engine without fully understanding it?

To be fair, between copy pasting a block of code and including a lib into your project, there isn't that much of a difference.

There are quite a bit of things I wouldn't be able to do in my own (at least in a reasonable amount of time) such as: OpenGL wrapper, OpenGL itself, Bullet wrapper, Bullet itself, GLFW wrapper, GLFW itself, etc. And honestly, I don't have an issue with not knowing how to do those things.

There is this snippet of code in my shaders that basically is normal mapping without having to do the whole tangent/bitangent shenanigans. Its 12 lines of code, computes a few partial derivatives, a "cotangent frame", and voila, I got a normal mapped mesh!

I have no clue how it works, but it does. It saves me up the trouble of doing my own tangent/bitangent computation code when loading models (I hate coding file loaders, its the most unrewarding thing ever for me). I sort of get the idea of normal mapping, of using "tangent space" to make the normal map valid no matter the transformations the mesh is subjected to, but thats all I know, I have no idea how it works.

That's a success story, there is also a failure one:

For my deferred renderer, I based the position reconstruction code from a blog post. I didn't knew exactly how it worked but I rolled with it. Position reconstruction was used for computing lighting, and turns out it didn't worked at all. I spent weeks on and off from that code until I narrowed it down to the last step in the position reconstruction process. Showing the view space positions I could see XY plane was working fine, but Z was broken, it was either too far or behind the camera. Looking at the code I noticed that the NDC to view space depth function was... iffy, it didn't seemed right.

Checked resources around on NDC space and how OpenGL stores depth values in the depth buffer, and remade the function in a way I thought it would be correct. With the proper view space depth function working, lighting equations could work properly.

I'm not a very big fan of math so those are the spots where I end up searching for code. I enjoy a lot the "architecture" part though, so I do have my own math classes because I simply dislike the structure of the math libraries I found out there for Java.

There are parts that one likes to do, and parts that one doesn't likes to do. I'd say that if anyone expects to make anything, game or whatever, they should recognise the parts they don't like and fill it with libs as soon as possible. Don't sweat over it, just concentrate on what you like.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

In general I think most people have used code that isn't theirs. I think a much more pertinent question is if everyone provides proper credit for the snippet. There are a few places in Hieroglyph 3 where I link to the StackOverflow topic where some code came from. In particular, code for converting from std::string to std::wstring is totally ridiculous and complex, but I found a snippet that worked well so I linked to the topic in a comment. I understand it at a high level, but I couldn't rewrite it from scratch without a reference...

I'll occasionally use code I don't understand if the scope of the code is small enough and I understand exactly what the code is intended to do. For example, I might use a compression algorithm I don't fully understand as long as I understand the (hopefully very simple) API to use the code, or I might use someone else's triangulation code even if I don't understand the underlying algorithm. As long as I understand how to use the code, and what the result of using the code is supposed to be, then I'm fine. And, as Jason Z says, it's important to properly attribute such code to its original creators, at the very least to be able to track down where the code came from originally if you have some questions in the future.

So as the title suggest, do you copy code from the internet and put it in your game/engine without fully understanding it? I am having trouble understand an algorithm and I have been trying to understand for a week now with no luck. so today I gave up and just copied the code from the internet and put it in my engine. But I feel really bad and its really annoying me that I don't understand it.

On occasion, yes. I used to do that more when I was newer to programming, but don't need to do it anywhere near as frequently. Occasionally it still occurs for algorithms I just don't comprehend. If, after researching it, I really don't get something, copying examples and using it and modifying it gradually bit by bit is part of my learning process. Months may go by before I unintentionally am forced to return to it and, with a fresh set of eyes, understand it.

One thing that sometimes helps is rewriting the code piece by piece (changing it to my project's code style) to help understand what the code is doing. It doesn't always give me insight into the concept, though.

Sometimes the same applies to bugs in my code. I try to fix them. If they are difficult, I spend several days trying to fix them. But on really difficult bugs, sometimes I have to leave them for a month or two until I am forced to return to them with a completely different mindset and renewed determination.

Pay attention to the licensing. Though you don't have to accredit public domain code, like @JasonZ I also link to the sources in comments even though my project is close-sourced; partly this is for my own benefit and partly so I can quickly find the original source when sharing a class or function with others.

This topic is closed to new replies.

Advertisement