• Create Account

# dejaime

Member Since 14 Aug 2011
Online Last Active Today, 08:47 PM

### In Topic: [SFML]Impementing movement for a 2d car

Today, 09:43 AM

There are two simple ways to achieve this.
One, is to have a speed vector, like this:

Speed = [5,0] or Speed = [2, 3.5]

So, to steer it right or left, you'd need to rotate this vector, using something like Speed.Rotate(15º);. It should steer 15º to the left (counter-clockwise) while keeping the vector's length (the speed) intact.
The advantage of this one is that moving your object is as easy as [ Position += Speed ]. But it makes steering a bit trickier, while accelerating/breaking is harder since you need to evaluate how much should go on both sides of the vector.

Also, calculating the length (linear speed) is a costly operation.

As an example, this is an example of a rotation function:

```inline static v2 RotateVector(const v2& p_V, const angle& p_Angle) {
float t_fSin = p_Angle.sin();
float t_fCos = p_Angle.cos();

//return v2 (x, y);
return v2( (p_V.x*t_fCos - p_V.y*t_fSin), (p_V.y*t_fCos + p_V.x*t_fSin) );

}
```

The second approach is to have a speed value and a direction, vector or angle, like this:

Speed = 30;

Direction = 45º //or ( pi / 4 ) (or even Direction = [0.707, 0.707] )

The advantage of this approach is that rotating is as simple as updating the angle, as in (90º + 15º = 105º). The downside is that you can't simple add 30 (speed) to the position vector.

it should be Position += [Speed * Cos(Direction), Speed * Sin (Direction)].

I particularly prefer the second approach for this kind of movement, while the first one for free movements, with no need for steering.

### In Topic: random generators

Yesterday, 09:10 PM

Is it "more correct" to use one generator with its own seed for each purpose, or it is fine to just use the same generator with different distributions?

It depends a little. The advantage of using the same random generator to all subsystems is that you'd only need one instance of it, store one single seed and et cetera. The headache comes when your cute deterministic random generator starts to get out of sync due to the several subsystems using it at the same time.

If, let's say, one computer handle floats slightly differently from another one, it could break the determinism due to a random call being done with one frame of difference in a subsystem, messing with the previous order. And I'm not even going to start talking about multi threading or cooperative multithreading.

It is very easy to lose your determinism when you're using the same generator throughout all subsystems. This determinism won't matter on most cases, but you'll love the predictability when trying to track a nasty bug.

I try to use an independent random stream source for each independent subsystem, it is usually more stable this way. I should say though that I started to do this for precaution, I never really had any problems caused by using a single generator.

### In Topic: Needing some quick tips on how to organize my code

Yesterday, 08:39 PM

I'm extremely (really) picky when it comes to colors. I can't stand writing code in a white background

We're two then! I am lightly photophobic, and I use a modified version of the C::B theme called Son of Oblivion (a Son of Obsidian port to C::B). I also program with only around 15% brightness on my LED monitor (40% during the day, but I do the huge majority of my coding at night)...

I also search the web with blackle.com, feels better (I don't know if it really saves any energy in LED monitors)

(GD.net is too white, but we can't have everything, can we? )

I assure you, the thing that helped me the most was actually studying Object Orientation. It helped clear where one part of the code ends and the other starts, so it helps define/choose the files I needed/wanted.

But It is a fact that using IDEs helped me further in learning some more. Still, I know some guys to what it did the opposite and made them go on brainless file creation rampages on every project they started!

I also use IDEs for their color schemes, even though Sublime Text is said to be the best here. But as IDEs have a world of more features for C++ development, I can't go with a fancy Text Editor over an Integrated Environment; or else, I'd need to write loads of build scripts that wouldn't be necessary with a full fledged IDE).

I like how C::B handles its highlighting color schemes, maybe I am just used to it...

It also has some neat features for C++, such as active/inactive code highlighting (to name one):

Comment the #define

About variable declarations, they surely can go on header files. If the variables are global, declaring them on the header will be enough, but if they are "class globals" (static class variables) you need to put them in the class definition and then declare them in the .cpp file.

### In Topic: Needing some quick tips on how to organize my code

Yesterday, 06:10 PM

My personal experience was similar to yours. I started programming with really basic C, that could all fit in one implementation file or, at most, the implementation and a header. To step forward, I had a tough time, since I didn't really know what the problem was (what exactly I wasn't grasping).

I started to study C++ and Object Orientation, and that gave me an overview of modeling and architecture. I was then able to separate my code better, so I did with the files.

But the only time I really learned to structure my files was when I started to use an IDE. I had been using notepad++ and a command line compiler. I started to use the IDE templated file creation features to create files with some pre-defined code (such as the basic class structure and this kind of stuff); it forced me into a good style, somehow.

So, study more, it will suddenly become clearer. It is kind of like learning a new spoken language; one day you can't understand anything you're hearing, but the next one you can't believe you understand almost everything.

### In Topic: How do I use my scroll bar to increment a std::vector

14 March 2014 - 09:05 AM

Take a look here:

https://gist.github.com/dejaime/836813eebb5d262bfa1f

If you compile it, just insert + and - in the console.

"++++++++++++++++++++" should work too (to move the 'bar' several slots at a time).

PARTNERS