Jump to content

  • Log In with Google      Sign In   
  • Create Account


HappyCoder

Member Since 28 Apr 2007
Offline Last Active Yesterday, 04:01 PM

#5178954 How to design my game

Posted by HappyCoder on 08 September 2014 - 04:06 PM

If all the content in your game could be loaded from a file, then each package could simply be a collection of content for you game. This means levels, models, textures, and even game logic all needs to be independent of the compile time code.

 

Another option is to have the game come bundled with all of the content but downloading a 'package' simply unlocks it.




#5177299 OpenGL Camera

Posted by HappyCoder on 31 August 2014 - 05:17 PM

I'm not sure what you mean by the axis drifting, but there is something that looks off in your code.

looking at the quaternion reference form glm, it looks like you are getting the parameter order wrong in the quat constructor. It expects w first, you are passing it in last.

 

Also, usually the forward vector for the camera goes along the z axis. For your up down rotation, it looks like you are rotating around the z axis. This will result in the camera rolling instead of pitching. Try switching the rotUP to rotate around the x axis.




#5177229 learning to render

Posted by HappyCoder on 31 August 2014 - 10:59 AM

I would recommend learning DirectX 11. OpenGL is full of legacy code for backwards functionality. This means OpenGL will let you continue to do things the old way so you may be using functionally that has been outdated since the 90s.

 

Directx 11, on the other hand, has trimmed the fat and only offers an interface to graphics that is up to date. This will help you learn the more modern way of doing things. OpenGL will also have much of the same updated functionality but it is hard to know what is the state of the art in all of those API calls.

 

With modern graphics, there is a lot to take in. You will find you have to write a lot of code just to draw a single triangle. Don't be frustrated if you don't understand it all right away. Just try following some tutorials and get those working. Then start to play around with the tutorial code to get it to do what you want it to. You will learn as you experiment. If you have any questions or hit any roadblocks feel free to post your questions here.

 

Here are some good tutorials to get you started




#5177224 How to organize your code and design your system

Posted by HappyCoder on 31 August 2014 - 10:39 AM

Learning good design comes in part by trail and error. You find out what works and what doesn't. A few tips to help you out though.

 

Each class should only do one thing. Other classes should not need to know the inner workings of another class to use it. As an example below, say there is a class, Foo, where every time you assign fooBar you should incriment bar

////////////////////////
// The wrong way to do it
class Foo
{
    public int bar = 0;
    public int fooBar = 0;
}

Foo foo = new Foo();

++foo.bar;
foo.fooBar = x;

////////////////////////
// The better way to do it
class Foo
{
   private int bar = 0;
   private int fooBar = 0;

   public void SetFooBar(int value)
   {
        ++bar;
        fooBar = value;
   }
}

Foo foo = new Foo();
foo.SetFooBar(x);

If you find that you are repeating the same lines of code over and over again try to find a way to make a method out of it and make that method part of a class where it makes the most sense, the method should work with either members of the class or parameters passed to the method. Do not use global variables or singletons. Global constants are good though, they help you avoid magic numbers.

 

Only use inheritance when you need polymorphism. If you just need the functionality of another class just make it use the other class

////////////////////////
// Bad
class Foo extends Array
{

}

Foo foo = new Foo();
foo.Add(item);

//////////////////////
// Better
class Foo
{
    private Array array;
    void Add(Item item)
    {
        array.Add(item);
    }
}

If any single function gets long split it up into smaller functions where each function does one well defined thing and the original function uses these small functions. This practice when done right will also help your reduce duplicate code. The same goes for large classes. If you have a large class, split up the functionality. There is no exact value for what is too long, but when a functions is over 50 lines of code you should consider breaking it up. If a class is approaching a few hundred lines of code, you should break it up.

/////////////////////////
// Bad
void Update(float timeStep)
{
  // update position 
  position.x = position.x + velocity.x * timeStep;  position.y = position.y + velocity.y * timeStep;
  velocity.x = velocity.x + acceleration.x * timeStep;  velocity.y = velocity.y + acceleration.y * timeStep;

  if (doesCollide(world))
  {
     // collision code
  }

  // update AI
}

////////////////////////////
// Better
Point Add(Point a, Point b)
{
    return new Point(a.x + b.x, a.y + b.y)
}

Point Scale(Point a, float b)
{
   return new Point(a.x * b, a.y * b);
}

void UpdatePhysics(float timeStep)
{
  position = Add(position, Scale(velocity, timeStep));
  velocity = Add(velocity, Scale(acceleration, timeStep));
}

void UpdateCollision(float timeStep)
{ 
  if (doesCollide(world))
  {
     // collision code
  }
}

void UpdateAI(float timeStep)
{
  // update AI
}

void Update(float timeStep)
{
  UpdatePhysics(timeStep);
  UpdateCollision(timeStep);
  UpdateAI(timeStep);
}

I am going to stop there for now before I turn this into an article, but hopefully this is useful to you.

 

EDIT:

I would also suggest looking at Unity to see how they use a component based system. It by no means is the only way to organize a game, but it is a good example of how to solve the problem, it definitely isn't an easy one to solve.




#5176754 Include file not found in GCC, due to path size?

Posted by HappyCoder on 28 August 2014 - 03:13 PM

If you add the path D:\ZTUFF\Projects\EDGE\ as an include path for your project, you can include a file based on its absolute location relative to project

#include "Source\Engine\Resources\Material Resource\MaterialResource.h"



#5176303 [2D]Make a camera follow multiple characters.

Posted by HappyCoder on 26 August 2014 - 04:21 PM

The bounding box is the smallest box that contains a list of points. You can represent a bounding box by the location top, bottom, left, and right edges. The top and bottom only need to store a y coordinate value, right and left store an x coordinate. To calculate the bounding box, you loop through all points and for each one, you extend the bounds of the box if the point isn't contained in the bounding box. However, the first point in the list needs to initialize the bounding box.

 

 

Calculate the bounding box

boundingBox.left = points[0].x
boundingBox.right = points[0].x
boundingBox.top = points[0].y
boundingBox.bottom = points[0].y

// set i to 1 to skip the first point since it was used to initialize the bounding box
for (int i = 1; i < points.count; ++i)
{
   if (points[i].x < boundingBox.left)
      boundingBox.left = points[i].x

   if (points[i].x > boundingBox.right)
      boundingBox.right = points[i].x

   if (points[i].y < boundingBox.top)
      boundingBox.top = points[i].y

   if (points[i].y > boundingBox.bottom)
      boundingBox.bottom = points[i].y
}

The aspect ratio is just the width divided by the height. where the width of the bounding box is boundingBox.right - boundingBox.left and the height is boundingBox.bottom - boundingBox.top




#5176057 Funniest line of code ever ?

Posted by HappyCoder on 25 August 2014 - 02:09 PM

The comment here is kinda funny
 

class Lens
{
public:
	virtual ~Lens();

	virtual Matrix4f GetProjectionMatrix() const = 0;
protected:
	Lens(float near, float far); 
	
	float mNear;
	float mFar;
	//float mWhereEeeeeevarYouAre;
private:
};



#5176044 Sampling the Render target while rendering to it

Posted by HappyCoder on 25 August 2014 - 12:44 PM

Another option would be to use two seperate render targets. You render everything to one target that doesn't require any special treatment, then you copy the contents of that render target to another render target. You then use one as the input and one as the output.




#5175884 [2D]Make a camera follow multiple characters.

Posted by HappyCoder on 24 August 2014 - 05:47 PM

First you create a bounding box of your level that contains all of your characters plus a bit of a margin so a character isn't right on the edge.

 

You then compare the aspect ratio of this bounding box to the aspect ratio of the screen. If the bounding box has a larger aspect ratio, then you base the zoom of the camera on screen.width / boundingBox.width, otherwise you use screen.height / boundingBox.height. The center point of the camera is simply the midpoint of the bounding box.




#5175740 Line vs Triangle collision in 3D

Posted by HappyCoder on 23 August 2014 - 09:41 PM

First, you need to find the plane the triangle lies in. Next find the point of collision between the line and the plane. Then you find the barycentric coordinates of that point relative to the triangle. If all three values of the barycentric coordinates are in the range [0,1], then the point is inside the triangle meaning line intersects the triangle.




#5173986 Checkstyle

Posted by HappyCoder on 15 August 2014 - 02:59 PM

A friend of mine told me about an experience he had in one of his programming classes. I found it funny, so I thought I would share it here.
 

Programming joke of the day; this actually happened to me:

We have a little program we need to run on our code called "Checkstyle" that goes through our code to find anything stylistically wrong with it. If it doesn't pass check style, we can't get credit on it. Sometimes, it takes issue with stuff that really shouldn't (in my opinion) be an issue.

Today, it spat out a bunch of error messages telling me that some of my lines of code were more than 100 characters long. I said "Are you kidding me? We aren't allowed to have lines of code longer than 100 characters?"

A haughty T.A. passing by said "Of course. Checkstyle likes readable code. It's more professional."

A person sitting next to me said "Hey, if your line is too long, try taking out the 'space' characters between your operators and your operands. They aren't needed to compile anyway."

I said "Great Idea! That'll cut down the line size, which makes it more readable, right?"

Somebody else suggested "Are your variable names too long?"

I said "Yeah, probably. I'll global search and replace them into names that are only one character long. That should help too."

After looking at my code, I found that the issue was my error message. My error message was about 60 characters long, politely informing that the only legal strings allowed in the method are "heads" and "tails". I replaced the error message with a frowny face emoticon instead.

After putting the finishing touches on my code, I ran it through checkstyle. It passed. I got 102% on the lab. I'm so glad they gave me such a wonderful tool to make my code more professional and readable!

Along a similar vein, checkstyle doesn't like "magic numbers." In one lab, it told me that "4" was a magic number, and needed to be replaced. In class, somebody commented on it. "What is a magic number, and how do you avoid using them?"

The teacher said "A magic number is a number in the code that is there for no apparent reason, but makes the program work right. Typically, you should replace them with global variables. Like, can anybody tell me how to handle the magic number '4' in the first lab?"

I raised my hand.

"Well, '1' isn't considered a magic number, so just replace all instances of '4' with '(1+1+1+1)."

The entire class laughed. Including the teacher.

"Just in case you were wondering," he added, "that was not the right answer."




#5172127 Particle "Engine"

Posted by HappyCoder on 07 August 2014 - 03:00 PM

1. You should be able to do all of what you need using compute shaders.

 

2. A good approach would be to have some common code that all particle systems use, but make a lot of the behavior implemented in separate functions that you can change depending on the behavior of the effect. Don't try to make code for a single particle effect that can handle any situation.

 

3. Compute shaders will be flexible enough to allow you to handle collision. However, you will probably only get good results with either simple shapes like spheres or static geometry.




#5169200 Random river and road generation without knowing full world?

Posted by HappyCoder on 25 July 2014 - 05:38 PM

I have a friend who was working on a procedural city. He found that voronoi patterns using manhattan distance made a very nice non uniform city looking layout. Perhaps voronoi patterns might be useful here too.




#5169052 How to allow player to walk in a Mesh?

Posted by HappyCoder on 25 July 2014 - 12:18 AM

Define a capsule shape for the characters. Next check the collision of the capsule shape to each triangle in the terrain. If they overlap, push the capsule out of the triangle. You can accelerate the collision against the terrain by using a octree.

 

Collision detection can be quite tricky. I would recommend implementing collision in 2D first to get a feel for how its done. When you do try to tackle 3D, try it first using shapes that cannot rotate. This article might be useful. 

 

If making your own collision detection system sounds hard, (because it is), I would recommend using an existing physics engine, like bullet.




#5168720 Weird Syntax Tricks

Posted by HappyCoder on 23 July 2014 - 02:04 PM

So I was looking to see if any languages supported overloading a function by return type and found this answer.
 

The bottom of the post contained this snippet of code that fakes overloading by return type in c++. 

struct func {
   operator string() { return "1";}
   operator int() { return 2; }
};

int main( ) {
   int x = func(); // calls int version
   string y = func(); // calls string version
   double d = func(); // calls int version
   cout << func() << endl; // calls int version
   func(); // calls neither
}

While I would never use this hack in a real project, it does make a fun exercise in understanding c++.

 

What other examples of strange language hacks do you know of?






PARTNERS