Monday, July 25, 2011

Platform Jumping needs vector-ing

I was, on my way to showing a simple jump mechanic for a platformer when things just got way to complex for my liking especially the different physics variables, gravity, player speeds, jumping states etc... I had a speed constant and an velocity for jumping and for gravity and for the player. I had this messed up sense of the direction the player was moving, which was a radian angle. It didn't lend itself well to player movement in a platformer. I was flip flopping sin/cos and rotating the angles all over the place. It was a mess, then I just gave in and started using vectors.

Without vectors I had the player falling through the platforms even when it detected hits. I had a bug where I could only move in the air, then I was able to only move on the platform. Had the player repeatedly flying straight up like a rocket. And some double jump code didn't work right either, I kept increasing the velocity applied for the jumping to the point where you wouldn't just smoothly jump up. You'd immediately reach your apex and slowly come back down due to gravity. So yeah there is a lot of stuff to figure out.

So here is the vector class in all it's wondrous glory, I've used it to good effect so far without needing many changes. The most important functions are the normalize, magnitude, dot, and cross product functions. Those are necessary to perform many physics calculations.

Caveats: Originally I made this an immutable type but later added mutation methods (set(x,y)). There's probably a few hundred ways to do these calculations, for instance you could just use stand-a-lone static functions, or maybe a class that uses all Point2D.Double's for the vector data, etc... The ZERO static is not usable in calculations if you use the mutator methods, it will mutate the global instance. (oops!!! yeah I found it out because I was using it, should make a subclass for ZERO as a completely immutable type.)

/**
 * File: Vector.java
 *
 * This file is free to use and modify as it is for educational use.
 * brought to you by Game Programming Snippets (http://gpsnippets.blogspot.com/)
 *
 * Revisions:
 * 1.1 Initial Revision
 *
 */
package snippets.math;

/**
 * Mathematical vector class implementation. Provides the necessary methods to
 * use vector in physics calculations, ray tracing operations, and other types
 * of directional vectoring math applications. 
 *
 * @author Nick
 */
public class Vector {

 public static Vector ZERO = new Vector(0, 0);

 private double x = 0.0d;
 private double y = 0.0d;

 public Vector(double x, double y) {
  this.x = x;
  this.y = y;
 }

 public Vector add( double c ) {
  return new Vector(this.x+c,this.y+c);
 }

 public Vector add( double x, double y ) {
  return new Vector(this.x+x,this.y+y);
 }

 public Vector add( Vector v ) {
  return new Vector(this.x+v.x,this.y+v.y);
 }

 public Vector cross( Vector v ) {
  return new Vector(this.y - v.y, this.x - v.x );
 }

 public Vector divide( double c ) {
  return new Vector(this.x/c,this.y/c);
 }

 public Vector divide( double x, double y ) {
  return new Vector(this.x/x,this.y/y);
 }

 public Vector divide( Vector v ) {
  return new Vector(this.x/v.x,this.y/v.y);
 }

 public double dot( Vector v ) {
  return this.x * v.x  + this.y * v.y;
 }

 @Override
 public boolean equals(Object o) {
  if(o instanceof Vector) {
   Vector v = (Vector)o;
   return x == v.x && y == v.y;
  }
  return false;
 }

 public double getX() {
  return x;
 }

 public double getY() {
  return y;
 }

 public double magnitude() {
  return Math.sqrt(this.x * this.x + this.y * this.y);
 }

 public Vector multiply( double c ) {
  return new Vector(this.x*c,this.y*c);
 }

 public Vector multiply( double x, double y ) {
  return new Vector(this.x*x,this.y*y);
 }

 public Vector multiply( Vector v ) {
  return new Vector(this.x*v.x,this.y*v.y);
 }

 public Vector negative() {
  return this.multiply(-1);
 }

 public Vector normalize() {
  double mag = magnitude();
  if(mag != 0) {
   return new Vector(x/mag, y/mag);
  }else {
   return new Vector(x,y);
  }
 }

 public void set(int i, int j) {
  this.x = i;
  this.y = j;
 }
 
 public void set(double x, double y) {
  this.x = x;
  this.y = y;
 }

 public void set(Vector v) {
  x = v.x;
  y = v.y;
 }

 public void setX( double x ) {
  this.x = x;
 }

 public void setY( double y ) {
  this.y = y;
 }

 public Vector subtract( double c ) {
  return new Vector(this.x-c,this.y-c);
 }

 public Vector subtract( double x, double y ) {
  return new Vector(this.x-x,this.y-y);
 }

 public Vector subtract( Vector v ) {
  return new Vector(this.x-v.x,this.y-v.y);
 }

 @Override
 public String toString() {
  return String.format("(%f,%f)", x, y);
 }

 public static Vector valueOf(float x, float y) {
  return new Vector(x, y);
 }

 public static Vector valueOf(double x, double y) {
  return new Vector(x, y);
 }
}

Thursday, July 21, 2011

I hate stagnant blogs don't you!

I want to just apologize for not having posted in a while, especially to those who frequent this blog, if anyone does yet I want to say thanks for your patience.

Excuses, Excuses
Well guys, I've been busy as usual, this economy man it's a killer! Though that seems to be a common excuse these days. And since I have a regular job it isn't really true. Let's just put it this way, things just got busy. You know life stuff hard to keep a good blogging habit with lots of ya know that "stuff".

I've also been putting more time into the Android platform, as well as trying to figure out what the hell I should post next so many ideas and nothing seems to fit. Oh and I have to fight with my lackluster to code lately, yeah I am sorry I am one of those guys. Hey, look man I'm trying!

Blog Stuff
So every now and then I like to just randomly rearrange the blog and adjust settings, I noticed with the poll that it was creating scroll-bars, I hate when it does that, so annoying, though i don't think it did it on all browsers. But it did it in chrome which is the one I like to use on a regular basis, migrated from firefox though all three IE, firefox, and chrome have lodged themselves on my computer.

So I was going through the gadgets looking for something interesting and I dunno how does one choose from the pictures of hot chicks, puppy's, and style tips. Such tough decisions, yeah don't need to put any of those here, I doubt you want to be distracted by cute puppy's while reading or do you? So I decided on showing the Total pageviews thing, it's not terribly exciting only 12,188 at the time of this post. About 10-20 average visits a day. Not enough to make a living on, maybe someday. It'd be nice to be able to work on games all day as I do most of this in my little free time. So while I wait for that to happen I will just keep working on more snippets. Also removed some extra clutter with the external links, didn't really see a need for them but if someone liked using them please let me know. Added rss feed to gamasutra which I find an extremely good source for game design articles and other industry news as well as a couple of blog writers who share insights into how studios handle particular issues.

I really hope they are useful and anything you can think of that would help you out don't hesitate to ask me to try and figure out. Still need to figure out how to make the toggle screen-mode more professional.