August 30, 2016

Fun with linear algebra

0 comments

Let's have some fun with math, especially linear algebra!

- Is this your idea of fun, Erik?

- Yes, when you see what you've learned is actually working in real life, then that's fun. Not fun as in drinking whiskey, but you get the idea...

When I studied linear algebra in school I had no idea when I would need to know if two vectors are pointing in the same direction or why you would ever need to use the cross product. But a few years later, I've found some practical problems to solve where I actually needed to know if two vectors are pointing in the same direction. I've collected these problems and their mathematical solutions in a new tutorial series: Use Linear Algebra to solve problems in Unity with C#.

The problems I've solved so far are:

The idea is to minimize the text and instead have finished code with comments that you can just copy and paste. Some of these problems you have solve in a rush. When I competed in the Ludum Dare competition, where the idea is to make a game in 48 hours, I needed to know if two line segments are intersecting with each other. But I couldn't solve the problem fast enough so I had to come up with another idea, which took some extra time and thus made the final game worse.



Solve the Uncaught unknown compression method Unity WebGL error

0 comments
This Sunday I had to finish a silly game called Traffic Light Simulator for the Ludum Dare competition.


One of the many problems I encountered happened when I tried to upload the first version of the game to my server. As you may know, Unity has abandoned the WebPlayer option in favor of the WebGL option, which is under development so there will be bugs! It was always super-easy to make the WebPlayer option work online, but with each Unity update you have to learn a new way to make the WebGL option work.

First of all, the offline version worked fine in Firefox, but when uploaded the project to the server I got the following error:
An error occured running the Unity content on this page. See your browser's JavaScript console for more info. The error was: Uncaught unknown compression method.

When you open the Developer Tools console, you will see the following:
  • Failed to load resource: the server responded with a status of 404 (Not Found): Invoking error handler due to Uncaught unknown compression method
  • Uncaught unknown compression method
  • Failed to load resource: net::ERR_CONNECTION_RESET: Could not download Release/Test.datagz
  • Failed to load resource: net::ERR_CONNECTION_RESET: Could not download Release/Test.jsgz
  
So what do you do when you encounter an error you have't seen before? You google it! And if you google these errors you will find a lot of people with the same problem. The solution seems to be to modify the .htaccess file. But in the latest version of Unity, there's no .htaccess file in the WebGL folder! The second link on Google was Unity's own documentation, which is not updated to the latest version which is 5.4, so there was no solution there either. Another solution was to contact the server provider.

After digging around on the web, I found the solution, and it was this super easy solution: What you need to do is to modify the index.html, which is the only file in the WebGL folder. So open it in Notepad or whatever program you are using. At the bottom of the file, you will see the following lines:
  • dataUrl: "Release/Test.data", 
  • codeUrl: "Release/Test.js", 
  • memUrl: "Release/Test.mem", 
To solve the problem you have, you just have to add gz to the end of those lines:
  • dataUrl: "Release/Test.datagz", 
  • codeUrl: "Release/Test.jsgz", 
  • memUrl: "Release/Test.memgz", 

If you upload the new version you should see that everything is working fine!

August 24, 2016

5 books about math you can read on the beach

0 comments
Summer is almost over (at least here in Sweden where we have about one month of summer), but everyone is not living in Sweden or maybe you are visiting a beach during the eleven months of winter. If you are, and if you would like to read a book about math, then here are five books you should consider reading. These books don't require neither a calculator nor a piece of squared paper to understand, so you can read them while drinking your umbrella drink.

A History of Mathematics. This book will give you a brief introduction to the history of mathematics. It's a big book, so it's not brief in that sense, but each individual is being introduced with a brief summary. Beginning with the old Egyptians, continuing with the Greeks, a tour to India, then back to Europe, and ending somewhere around the beginning of the 20th century, you will really learn how the math you saw in school has evolved.

Against the Gods. This book will tell you the history of risk. The title says "against the gods" which means that humans like you and me used to argue that we didn't need to take risk into account because the gods determined our fate. But you will see how we humans stopped believing in the gods and instead began believing in math to determine our fate. So this book will also tell you a history of mathematics but from a risk perspective.


How to Solve It. While reading about the history of mathematics maybe you decide that you want to be a part of the history of mathematics. What if someone would read your name 100 years from now? If that is the case, then you will have to begin solving math, and this book will tell you how to do that. The text is obviously not a magical solution that will make you solve math like you capture Pokemons, but it will get you started.

The Man Who Knew Infinity. Someone who could "solve it" was the Indian mathematician Ramanujan, which this book is all about. He taught himself math in rural India and then he was invited to England because he was so good at it. He who invited Ramanujan to England was the mathematician GH Hardy, so this book is also a biography on him. If you don't want to read the book, at least you should watch the movie with the same name.

Fermat's Enigma. One of the historical characters in the book A History of Mathematics is Fermat. He came up with a problem, and it would take several hundred years before the mathematician Andrew Wiles solved the problem. This book is about how he did it. To solve it he used a lot of math, so this book will also give you an introduction to the history of mathematics.

August 23, 2016

How to write a story for a book, movie, or game

0 comments
When I wrote a book about Elon Musk I learned that there are rules you are recommended to use to make the book you are writing better. These rules are no laws, but you are recommended to use the rules or your readers will give you bad reviews. In hindsight these rules are often obvious, but it's still difficult to learn them on your own if you haven't been told about them.

The first rule is that you should keep it simple. Most aspiring writers tend to write way too complicated, even though the rule says you should make the text as easy as possible. You should remove all unneeded words and text (Stephen King always removes 20 percent of the text after finishing the first draft), and use words like "but" often. To see if this really is true, I made some research on my own, by writing computer software to count the world in top selling books: Top words in top selling books. It turned out that the top selling books included a lot of: the, and, of, to, that.

Another good rule is that you should write about something no-one has heard before. If you are writing a story about a trip you've made to a beach, it's common to write about uninteresting things like how warm the water was and that the sand was soft. The problem is that those stories are boring to read. To make it more exciting for the reader you should tell stories no-one has heard before. So if you've made a trip to a beach, then tell a story from that beach no-one has heard before. Maybe someone was eaten by a shark? When I wrote the book on Elon Musk book I managed to find a story about Winston Churchill, who years before Elon Musk lived in the area was travelling around the country. Reviewers who read the book said that they really liked that connection because the story of Winston Churchill in South Africa is unknown to most people.

Movies, books, and games are not that far from each other, so I thought it could be a good idea to learn how to write a movie. The book about Elon Musk was a biography, so I couldn't make up what's happening in the book. But if you are writing fiction, then you have to be more creative. After some scientific research with the help of Google, I found the book Save the cat! by Blake Snyder. He's promising that the book is the "The Last Book on Screenwriting You'll Ever Need." Here are some key points from that book:
  • Recommends the books: 
  • The audience has to like the main hero. Liking the person we go on a journey with is the single most important element in drawing us into the story. "Save the cat" is the scene where we meet the hero and the hero does something - like saving a cat - that defines who the hero is and make us, the audience, like him/her so we want the hero to win in the end. But what if the hero is a bad guy? Then you should make the hero's enemy even more horrible. Anyway, this is the save the cat moment in Pulp Fiction:
Scene One of Pulp Fiction, basically, is where we meet John Travolta and Samuel L. Jackson. These are the "heroes." They are also drug-addicted hit-men (with really bad haircuts). Quentin Tarantino does a very smart thing when we meet these two potentially unlikable guys - he makes them funny. And naive. Their discussion about the names of McDonald's hamburgers in France is hilarious. And sort of childlike. We like these guys from the jump - even though they're about to go kill someone - we are "with" them.
  • You have to be able to describe the movie with one line. The customers should understand what the movie is about by looking at the title and the poster and by reading one line only. Otherwise, the customers have to trust other sources, such as rumors or what the star of the movie said in the newspaper, so they might end up seeing another movie. This is why we are seeing so many re-makes of movies, such as Batman 3 and Shrek 4, because the customers understand what the movie is by looking at the title or the poster. What you should do is to summarize what your movie is in one line. And you should spend a lot of time with this single line because it's the key to at least making someone considering watching your movie. For example, the one-line from the movie Die Hard is: "A cop comes to LA to visit his estranged wife and her office building is taken over by terrorists." This line should include irony, like the irony that a cop on holiday ends up in a building filled with terrorists.
  • "Give me the same thing... only different." Most movies are the same, but they are still different. This is actually connected to the travel story I wrote about in the beginning. Most travel stories are the same, but you should add something new. All monster movies are the same, but you need to add a new fresh monster. The author argues there are more categories (monster movies is just one example) and you have to make sure your movie is following the theme of the category but with a fresh twist. And don't worry about stealing what's working as long as you understand why what you are stealing is working in that movie.
  • The main character should have a primal goal. The main character should have a goal we identify with as humans because we are still cave-men/women. So the goal should be a primal goal, such as love, and not just buy a new car. But if the goal is to buy a new car to find love, then that's a primal goal. Primal goals include survival, hunger, sex, protection of loved ones, and fear of death. The primal goal in Die Hard is "the desire to save one's family."
  • Adapt to the target audience. Don't assume that just because you prefer something, everyone else will, too.
  • Follow the author's 15 sections of a movie script. Some of these sections intersect with each other and includes: 
    • The opening image, which will set the mood of the movie. Example includes the motorcycle ride in the movie Lawrence of Arabia, and when the fighter jets launch from the aircraft carrier lit by the morning sun in Top Gun.
    • The set up, which includes the opening image. During the first 10 minutes you really have to capture the audience or they might leave the theater and give it bad reviews. Lawrence has crashed his motorcycle, had his funeral, but now he's traveling on a Camel in the Middle East. Maverick and Goose is being sent to the Top Gun school after some wild flying. 
    • The B-story, which is a side-story, and in most screenplays this is a love story. The purpose is to give the audience a breather. When Maverick in Top Gun falls in love with the instructor the audience will have a break from the flying.   
    • The fun and games section, is a point in the movie that should be less serious, like in Die Hard when Bruce Willis is first outwitting the terrorists, or when Batman in Batman Begins is almost killing himself while wearing the first version of his suit.  
    • In the all is lost section, someone probably dies, like Obi Wan in the "first" Star Wars movie or Goose in Top Gun. A rule-of-thumb is that anything that involves death is good in this section. If you don't have anyone to kill, then make something up, like a dead flower - just show some death.   
  • Even professionals have trouble coming up with new ideas. "...we were taking a time-out from a story we were trying to break, bowed by despair and self-loathing over not knowing how." But as with everything else, the more you practice the easier it will get. "Dude, suckin' at something is the first step to being sorta good at something." 
  • You should use cards on which you write the different scenes in your story. About 40 scenes is enough and you stack multiple cards if multiple things are happening in that scene. But the stacked cards count as 1 card of the 40. This might seem as a waste of time, but the "story is seeping into your subconscious a whole other way." Now it will be much easier to identify holes in the story and, above all, remove unnecessary scenes, just like Stephen King is removing unnecessary text. A scene is here defined as a conflict with an opening, middle, and end, as well as an emotional change. When I wrote that book about Elon Musk I should have used this strategy before writing it, because I remember that I had to shuffle the text around in the word editor once too often. 
  • Keep it simple. It shouldn't have to take 40 minutes to explain the movie plot to the audience. Too many different supernatural themes in the same movie is a bad idea, so don't have both aliens and dinosaurs in the same movie. Less is more!  
  • Every single character in the movie (except the bad guys) must change in the course of your story. Otherwise the story is not worth telling, because the story has to be important to the characters in it and stories are about change. For example, Maverick in Top Gun is flying recklessly because everyone thinks his father wasn't a good pilot. But as the story unfolds he learns that his father was a good pilot and he doesn't have to fly recklessly anymore. It's the good guys that should change, because to succeed in life you have to accept change and see it as something positive, while the bad guys reject change and end up dead. The audience will be inspired and thus like the movie even more.
  • Each character needs its own personality. If you are unsure if your dialogue is unrealistic, try to cover up the names of those who speak, read the dialogue, and see if you tell who is saying what. You should be able to tell the difference between different characters without seeing their name.    

August 17, 2016

What a lot of things I don't need

0 comments
I'm reading the book Sophie's World: A Novel About the History of Philosophy by Jostein Gaarder. As the title says, it's a book about the history of philosophy, but it's not one of those boring text books on philosophy as the author of this book has chosen to dramatize the story. Sophie is the main character in the book and she begins to receive mysterious letters from an unknown sender and each letter includes a little history of philosophy.

I have't finished the book, but I found a section about the Cynics, who were the minimalists of philosophy. It goes like this:
The story goes that one day Socrates stood gazing at a stall that sold all kinds of wares. Finally he said, "What a lot of things I don't need." This statement could be the motto for the Cynic school of philosophy, founded by Antisthenes in Athens around 400 B.C. Antisthenes has been a pupil of Socrates, and had become particularly interested in his frugality.
The Cynics emphasized that true happiness is not found in external advantages such as material luxury, political power, or good health. True happiness lies in not being dependent on such random and fleeting things. And because happiness does not consist in benefits of this kind, it is within everyone's reach. Moreover, having once been attained, it can never be lost.
The best known of the Cynics was Diogenes, a pupil of Antisthenes, who reputedly lived in a barrel and owned nothing but a cloak, a stick, and a bread bag. (So it wasn't easy to steal his happiness from him!) 
One day while he was sitting beside his barrel enjoying the sun, he was visited by Alexander the Great. The emperor stood before him and asked if there was anything he could do for him. Was there anything he desired? "Yes," Diogenes replied. "Stand to one side. You're blocking the sun." Thus Diogenes showed that he was no less happy and rich than the great man before him. He had everything he desired.
The Cynics believed that people did not need to be concerned about their own health. Even suffering and death should not disturb them. Nor should they let themselves be tormented by concern for other people's woes. Nowadays the terms "cynical" and "cynicism" have come to mean a sneering disbelief in human sincerity, and they imply insensitivity to other people's suffering.