Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 07 Mar 2002
Online Last Active Today, 07:17 AM

#5311459 C++ Angle bracket function

Posted by on 19 September 2016 - 11:20 AM

void addComponent(std::unique_ptr<BaseComponent> component) {
Probably something like that, perhaps with a different type of pointer, depending on how exactly you handle ownership.

#5311188 Game works when built in the IDE, but crashes when I run it from the .exe file.

Posted by on 17 September 2016 - 05:27 AM

Here's what to do in situations like this one: Make a copy of your code and boil it down to the smallest possible program that still shows the problem. Remove absolutely everything you can that doesn't have to do with the particular crash. Put all the code in a single file.

Now either of two things will happen:
(1) You discover the problem in the process of creating the small version of the program.
(2) You have a short program that you can post here, so we can all reproduce the problem and help you much better. If it turns out the short program is obviously correct and it's still breaking, you can include this code in a bug report.

Now, in the last 20 years of programming I have found a compiler bug exactly once. So the prior probability that you found one is very small, and the fact that your code has some issues (like not checking return values) leads me to lower my estimate in light of what I know about the situation.

#5310841 Find the domain of this function.

Posted by on 14 September 2016 - 03:33 PM

That's a second-degree inequality in v^2:

(v^2)^2 - 2gy * v^2 - g^2x^2 >= 0

The solutions are values of v for which

v^2 >= g * (y + sqrt(y^2 + x^2))
v^2 <= g * (y - sqrt(y^2 + x^2))

But (y - sqrt(y^2 + x^2)) is always negative, so the second condition never really happens.

Taking a square root on the first inequality, you get what I think is the final answer:

abs(v) >= sqrt(g * (y + sqrt(y^2 + x^2)))

#5310621 Why can't I print out my std::string vector?

Posted by on 13 September 2016 - 12:49 PM

A singleton is rarely a good solution. In a well-designed program you typically don't have that many dependencies, so perhaps you need to rethink your design. Sending the references everywhere at least makes the dependencies explicit. Otherwise you'll try to reuse some piece of code for a new project and you'll realize that the whole project has become one monolithic mess and you can't do it.

If you end up using global state of some sort, what is the advantage of using a singleton over using a global variable?

#5310425 Faster Sin and Cos

Posted by on 12 September 2016 - 04:34 AM

I might include a note about your my_atan(), and possibly exp() and some others.  Did you come up with the original code?

Yes, I did. I got the idea of using something like x/polynomial from this paper.

I just found a very interesting trick I hadn't thought about: You can reduce everything to computing atan(x) for x smaller than tan(pi/12) ~= 0.268, where it's really easy to approximate well. See here. The idea seems to come from "Math Toolkit for Real-Time Programming" by Jack Crenshaw, which I don't own.

#5310345 Faster Sin and Cos

Posted by on 11 September 2016 - 10:04 AM

Then you may find these constants more to your liking:
1.00022506713867187500f 0.324211299419403076172f 0.0511969886720180511475f

L. Spiro

That's great! The maximum difference is about .00074, which is really good for such a simple formula.

Here is the code with updated coefficients:

float atan_near_0(float x) {
  float x2 = x * x;
  return x / (1.00022506713867187500f + 0.324211299419403076172f*x2 - 0.0511969886720180511475f*x2*x2);

float my_atan(float x) {
  static const float pi_halves = float(std::atan(1.0)*2.0);
  if (std::abs(x) > 1.0f)
    return std::copysign(pi_halves, x) - atan_near_0(1.0f/x);
  return atan_near_0(x);

Any chance we can get a version with a higher-degree polynomial?

#5310270 Precision errors when calculating normal map for planet

Posted by on 10 September 2016 - 01:07 PM

With the parametrization you have used, the samples at the poles are very close together. Try using doubles for the computation, but even that might not fix the very last line, because all those points map to the same point on the pole and you are then looking at degenerate triangles.

A clean solution would be to use a cubemap instead of spherical coordinates.

#5310244 Faster Sin and Cos

Posted by on 10 September 2016 - 08:06 AM

It would be good to have my_atan(1) be exactly pi/4, because otherwise my_atan will not be monotonic around 1.

#5310108 Rotating in the direction of velocity

Posted by on 09 September 2016 - 06:20 AM

"Didn't have any luck" is a pretty bad description of the problem. What does it do? How does it differ from what you want? Are there cases where it does the right thing?

In any case, you are better off ditching angles altogether if you can: Use normalized unit vectors instead. If you are not scared of math, think of them as unit-modulus complex numbers.

Whatever you are doing with the angle, at some point you are going to compute its cosine and its sine. But those are precisely the coordinates of the unit-length vector!

If you are using an API that requires you to specify an angle, use atan2.

#5310088 What is the Quaternion equivalent for multiplying two 3x3 rotation matrices t...

Posted by on 09 September 2016 - 03:15 AM

The equivalent of matrix multiplication when using quaternions is... quaternion multiplication! (Surprise!)

#5310085 Computer understands your text and is able to speak back

Posted by on 09 September 2016 - 03:03 AM

Here's a book recommendation to gain some appreciation for punctuation: https://www.amazon.com/Eats-Shoots-Leaves-Tolerance-Punctuation/dp/1592402038

#5310015 Platformer style tutorial. How to import files from tiled etc?

Posted by on 08 September 2016 - 02:49 PM

Although this is not the code-heavy guide you are looking for, this is my favorite link on platform games: http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/

#5309960 C++ Going Beyond Basics

Posted by on 08 September 2016 - 09:22 AM

It doesn't matter what language I learn because all of them have the same exact logic...the only difference are the way you type the code.
instead of: cout <<"hello, world" << endl
you type: print "hello, world"

Yes, those things look superficially similar. But then you need to worry about whether `cout' and `endl' are visible or if you need to specify the `std::' in front, what that `#include <iostream>' means, whether you should be using `endl' or "\n" or '\n', what the hell `std::ostream &operator<<(std::ostream &, MyType)' means when you are trying to print your own type, etc., etc.

C++ is very unfriendly to beginners, and trying to learn fundamental programming concepts using it is a handicap that you do not need.

#5309956 C++ Going Beyond Basics

Posted by on 08 September 2016 - 08:57 AM

If you don't know programming, C++ is a terrible language to learn. I would start by learning a friendlier language first. Python is often suggested for this purpose, but I don't think there is anything wrong with learning C, for instance.


I think you have the right idea in trying to find projects that motivate the learning of different concepts.


Beyond input and output, try to learn:

 * Loops (make a multiplication table)

 * Fancier loops (test if a number is prime by trying to divide it by trial division)

 * Functions (learn to organize your code in understandable chunks with a good name and a clean interface, provided by the parameters and the return value of the function)

 * Data structures (things like arrays, lists and trees; but the details depend on the exact language you are working with)

 * File I/O (process a text file and count how many characters, words and lines it contains)

#5309933 Faster Sin and Cos

Posted by on 08 September 2016 - 05:15 AM

One thing that will make it faster and more accurate is to replace:

static const float pi_halves = float(std::atan(1.0)*2.0);
const float pi_halves = 1.5707963267948966192313216916398f; // float(std::atan(1.0)*2.0)

Actually, my compiler produces the exact same binary with that modification. As usual when optimizing, even if you have a plausible argument for why a change should be an improvement, you just need to test.