Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 28 Apr 2007
Offline Last Active May 27 2016 09:54 AM

#5277995 best way to write clean code

Posted by HappyCoder on 24 February 2016 - 09:13 PM

A few tips of mine

1. Don't use inheritance just to reuse code. Be sure your base class and sub class have a strong is a relationship

2. If you use class A inside class B, don't require that class B can only be used inside class A. As an example, if you have a Car class and an Engine class, don't require that the Engine can only be used in a Car. In other words, don't have cyclical dependencies.

3. Never use singletons or global state. Global variables are usually used because they an easy short term solution to problems, but you will pay for it later. Once you remove global variables you are forced to think about the design of your code more and come up with better designs.

#5276188 How could I solve edge-case death animation?

Posted by HappyCoder on 17 February 2016 - 02:49 PM

You will probably need an additional animation where the character bends over the edge. You would have to detect when the animation would put them hanging over the edge, and play the other animation in that case. As Orymus suggested, you would have to shift the position of the animation slowly to line up correctly. However, there really isn't a correct answer in this case. It really comes down to how much time you want to put into polishing details like this. Overhanging death animations is fairly common in video games and players aren't going to care that much, but those little details can add up to really improve the experience.

#5275817 HTML5 canvas game pure javascript preloader ?

Posted by HappyCoder on 15 February 2016 - 04:27 PM

I would look into javscript Promises

If you use native javascript promises you will limit your browser support. You can use this library if you want wider browser support.

At this point you can write a preloader function
function preloadImage(url)
  return new Promise(function(resolve, reject) {
    var result = new Image();
    result.src = url;
    result.onload = resolve;
Then to preload all images you can use the 'all' feature of promises

function preloadAllImages(imageUrls) {
  var loadPromises = [];
  for (var i = 0; i < imageUrls.length; ++i) {
  return Promise.all(loadPromises);
Then to wait for everything, you simply do the following
preloadAllImages(allImages).then(function() {
  // game code that depends on preload goes here

#5275342 How do i solve this specific scene graph problem

Posted by HappyCoder on 11 February 2016 - 04:25 PM

If you need b to not scale when a scales I wouldn't parent B to A, I would just transform the anchor point on A to world coordinates every frame and place B at that spot.

#5275304 How to calculate local velocity if you know it's world velocity and local...

Posted by HappyCoder on 11 February 2016 - 12:09 PM


FVector localVelocity = Rotation.UnrotateVector(worldVelocity);

#5275228 Clinical studies on overlooking stupid bugs

Posted by HappyCoder on 11 February 2016 - 12:18 AM

This bug once took me hours to spot. I thought I was going crazy.
if (condition);
   // this runs even if condition is false

#5275058 Physics problem

Posted by HappyCoder on 09 February 2016 - 04:44 PM

With air resistance it wont be symmetrical. Once you include air resistance into the equations it really complicates things especially if you want to take into account the projectiles shape and if it is spinning.

#5273953 Weather simulation

Posted by HappyCoder on 02 February 2016 - 04:14 PM

First off, simulating weather for a game sounds awesome. I really like that idea.

Second, I wouldn't worry too much about being super precise and super realistic. As long as the weather patterns look reasonable and aren't doing anything crazy you should be fine. Your goal should be a weather simulation that makes the game more fun. If the weather patterns make sense to the player and can be used strategically then your simulation is good.

IMHO I wouldn't start with air temperature, pressure zones, and velocities in a tight grid and try to model it like the simulations used to predict weather. I would start by identifying what you want the weather to do and write a simulation that mimics that behavior.

#5273944 Advice for a "decoupled" game engine

Posted by HappyCoder on 02 February 2016 - 03:37 PM

Switching game engines mid development will never be an easy thing to do, even if you write your code in a way to do it. I would recommend picking an engine from the start and sticking with it.

That being said, to write your game logic game engine agnostic, you will need to build out lots of interfaces. You would implement your logic using the method available on the interfaces and have concrete implementations for each environment you want to support. This would add a lot of work to the game, so again, I wouldn't recommend it. Although one benefit to this approach would be that your code with be testable. You could write unit tests around your game logic using a test implementation of the game interface. The same applies for server side logic, you could run the same game logic code server side outside the game engine.

#5273934 Gameworld structure for platformer/rpg?

Posted by HappyCoder on 02 February 2016 - 02:42 PM

I would make an list of priorities of what you want the player to experience. These may not be your priorities, but I am just putting some out as an example.

1. Exploration - the player should be excited to discover and is rewarded for exploring
2. Character progression - the player should be seeking ways enhance and improve their character
3. Mastery of the mechanics - the gameplay should have enough depth and challenge to engage even the best gamers

Your list may be longer than this, spend some time to figure what priorities you want for your game then compare them to world options or any other decision for you game.

If exploration is your top priority I would choose option #2 since an open world lets you better hide secrets and gives more freedom
If character progression is most important then #3 seems to make sense.
If mastery of the mechanics is most important then option #1 makes sense since it avoids crossing an overworld and gets straight to the action

So it really depends on what experience you want the player to have.

#5273403 How can I make a gameObject follow a path in Unity C#?

Posted by HappyCoder on 30 January 2016 - 03:56 PM

I wanted to add a few more tidbits.

First you can also have your EnemyPath component draw gizmos to make the path more visible
class EnemyPath : MonoBehavior
  public Transform[] path;

  public void OnDrawGizmos()
    Gizmos.color = Color.green;

    for (int i = 1; path != null && i < path.Length; ++i)
      Gizmos.DrawLine(path[i - 1].position, path[i].position);
To follow a path you have two options. You write your AI to be able to move towards a point. You follow a path by having the enemy goto the first point in the path, once it gets within a certain radius it moves to the next point.

You could also simply write a function that returns the point x distance along the path and updating enemy positions would look something like this

enemy.transform.position = enemyPath.getPointAtDistance(enemy.distance);
enemy.distance += enemy.speed * Time.deltaTime;

#5273368 How can I make a gameObject follow a path in Unity C#?

Posted by HappyCoder on 30 January 2016 - 11:33 AM

I would use a list of transforms as the waypoints. That way you can edit the path in the scene editor pretty easily.

class EnemyPath : MonoBehavior
  // create a list of transforms, each represent a point in the path. You can create empty game objects
  // in your scene then add them to this array to create the waypoints in your path
  public Transform[] path;

If you want to make the path a prefab, you can simply make all of the waypoints children of the EnemyPath object

#5272865 Multi UV in WebGl

Posted by HappyCoder on 27 January 2016 - 01:26 PM

WebGL has no connection to any file format. So you have complete freedom on what data you load and how you display it. Multiple UV is pretty straight forward with WebGL. You simply bind another attribute to your shader but you will need to load a file that supports multiple UVs when storing the mesh. Obj does not support multiple UVs. DAE is a good format. It is widely supported, easy to parse, and supports multiple UVs.

#5272742 Need help with basic frustum culling

Posted by HappyCoder on 26 January 2016 - 11:46 AM

Put 1 for the w component before the multiply, then divide the resulting vector by the w value after the multiply

// first you find the clipping space matrix
clippingSpacePoint = MVP_matrix * vec4(x, y, z, 1)
// then you normalize the point by dividing by w
clippingSpacePoint.x /= w
clippingSpacePoint.y /= w
clippingSpacePoint.z /= w
// now any point that is on screnn will have a clippingSpacePoint x and y coordinate between -1 and 1
To do this accurately you will need to transform 4 corners of the tile and determine if any point lies inside the -1 to 1 range. You will also need to determine if a tile edge intersect the frustrum. It is possible for a tile edge to be on screen with none of the corners on screen.

#5271307 Understanding cross product without delving too much on Linear algebra

Posted by HappyCoder on 15 January 2016 - 11:33 AM

Given a plane, pick two unique points that lie on the plane. Imagine a vector that starts at one point and points towards the other. That is what the author means by a vector parallel to the surface of the plane. A triangle has three unique points on a plane. So staring at one of the points on the triangle and creating a vector towards each of the remaining two points gives two unique vectors that are each parallel to the plane. Doing the cross product on these two vectors results in a vector perpendicular to the plane. You could pick any three points on the plane, it is just convenient that a triangle comes with exactly what you need.