Archive

Archive for June, 2011

Revisions to Space Fight’s Physics

Yesterday I posted about the physics simulation in my game, Space Fight. Since then I have had some suggestions on cplusplus.com for improvement. In particular it was pointed out that there is arguably a difference between the physical position of an object and it’s position on screen (thanks helios).

Last time I said the physics object didn’t keep track of it’s own position as it was part of sf::Sprite in the graphics object. I am now in the process of changing things so that the physics object keeps track of its position separately from the graphics object and then the two values are synchronised by the GameBase class which inherits from both of them.

More on this later, when I’ve finished implementing it!

-Xander

The End of Tip of the Day

As some of you know, I was posting a tip of the day each day. However, I have decided this is counter constructive – some days I have more tips to give than others, so it makes more sense just to post on demand. Thus, shall be renaming Tip of the Day to Tips and Tricks and they will become less patterned, and instead released when I have something to say.

-Xander

Categories: Notice

Space Fight – Physics

So how do objects interact with each other in the game, and indeed, how can we generalise their individual manipulation in the world?

Important: This is my first post where I use some physics and vector algebra. If you aren’t comfortable with these topics, and would like help understanding the posts, feel free to comment and I’ll give you some help and/or suggestions. Moreover, if you are happy with these topics, but think I’ve done a bad job of explaining, then tell me that as well!

Individual Objects

Position and Facing

A physics object in the game doesn’t keep track of it’s own position or facing for technical reasons (they is part of the graphics object, which gets them from sf::Sprite). Nonetheless, a physics object is capable of retrieving and modifying it’s position via virtual functions and polymorphism. The same is true for it’s facing/angle.

Read more…

Pure Virtuals with Bodies

Here is the link to a fascinating article by Herb Sutter which discusses some circumstances in which one might want to write a pure virtual function with a body. Enjoy!

Categories: C++, Links and Libraries

More on Suffix Return Types

Previously I discusses suffix return types, and I provided an example of how it could simplify notation. This:

Icanos::System::String Icanos::System::FormattedLog::ApplyIndent(const String&);

became this, which I’m sure you’ll agree is tidier:

auto Icanos::System::FormattedLog::ApplyIndent(const String&) -> String;

However, there is another application which is perhaps more relevant. The above example is a matter of scope: String is not in scope before the function’s name, but it is after, so we don’t have to qualify it fully. But the following is also to do with scope.

Consider a C++ template function to find the quotient of two values (yes, I know it’s contrived…). It’s return type depends on the template types passed to it. Indeed, it’s return type is decltype(s / t) where s and t are the values to be divided. But we can’t put this as s and t aren’t in scope before the parameter list. They are, however, in scope after it.

template <class S, class T>
auto quotient(S s, T t) -> decltype(s / t)
{
return s/t;
}

And there we are. It works beautifully! Just as a contrast, here is how Dr. Stroustrup says you would have to do it with C++03/98 syntax. As he points out, it is much more messy.

template <class S, class T>
declytype(*(S*)(0) / *(T*)(0)) quotient(S s, T t)
{
return s/t;
}

While s and t aren’t in scope before the parameter list, S and T (the types themselves) are in scope. Thus we can cast 0 to a null pointer to S and a null pointer to T, and then dereference it. This wouldn’t be acceptable at runtime, but it works as decltype is evaluated at compile time. Though it works, the former solution is clearly nicer.

Read more about it on his FAQ.

C++ Articles

22.06.2011 5 comments

My favourite C++ website, the C++ Resource Network, has just received a revamp – giving it a brand new article system. Articles can now be rated and categorised, and their content can now contain HTML. Not to mention, once can upload files to be attached to the article. All in all it’s a massive improvement, and I can but give my most heartfelt thanks to the site’s operators.

Check out the new articles section now! And you can find my article on there – about C++0x suffix return types. It’s on the blog too, of course ;)

C++0x Declared Types

Sometimes you might want to know the type of an expression. At runtime you can use RTTI (runtime type information) and typeid. But what about compile time? You can use typeof in some cases, but that is a nonstandard extension. C++0x provides a new keyword decltype which adds this functionality to the standard.

decltype stands for declared type and you can read more about it on Dr. Stroustrup’s FAQ. In summary though, if e is an expression, declytype(e) represents the type of the expression. So decltype(4) would mean int and decltype(4.0f) would mean float. Naturally this can be extended to user types.

Note that decltype is supported by Microsoft Visual C++, Intel Composer XE and GCC/MinGW.


class Blog {
	// etc
};

Blog SFMLCoder;
decltype(SFMLCoder) // <--- this means 'Blog'

Read on for examples! Read more…

Space Fight – Non Players

I’ve told you about my plans for the player, but what about other game objects? What are they? And what role will they play in the game?

Enemy Spacecraft

So what is there in Space Fight apart from the player. Well, there are enemy spaceships, which the goal is to destroy with gunfire. Their sprite is shown below. Basically, once they are implemented fully, they will navigate between asteroids and attempt kill the player with their own weapons.

Read more…

Reprise

Hi again! I’m back – just one last exam to go so I should have more time for the blog again now. I’ll try to pick up the pace of the tutorials and videos now, and also continue to post about the asteroid game I’m writing.

-Xander

Categories: Notice

Tutorial Requests!

I know I said I wouldn’t post until Wednesday evening, but I’ve just got a small time window here. Earlier today, I published a new page.

It’s a form to let you request tutorials. At the moment, I already have two requests and a multitude of tutorials which I have decided on myself so progress will be slow. Nonetheless, if there’s something you’d like to see a tutorial on, then let me know on that page! If I like the idea, I’ll add it to the list of tutorials and make it when I finally get the chance.

Request a Tutorial

I also remind you that I provide other support options: the FAQ (pending), the Q & A page, the contact page and the support feedback form. So, use them!

Follow

Get every new post delivered to your Inbox.

Join 74 other followers