Game Programming Challenge: Write a reusable 2D vector class

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 25081 Views - Last Post: 17 January 2013 - 03:47 AM

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 25 September 2011 - 10:06 AM

Final C++ version. With the idea of reusable code, broken up a little with namespace.

Spoiler


Test code can be fun. You can program in the abstract all day long, but actually using the code for something points out what you really need.

I needed a getIntersect. I'm not thrilled with it and it could be buggered, but it worked for the demo. The demo is common; bounce ball. Because, even showing all the methods doesn't really tell a student what they're good for.

This isn't the usual ricochet called bounce, we have gravity here. I think it's a pretty good test of any basic 2D physics framework.

#include <iostream>
#include "plane.h"

using namespace std;
using namespace Plane;

struct Ball : public BoundVector {
	double bounceConserve;
	Ball(double x, double y, const Vector &speed, double B)/> : BoundVector(speed, Point(x,y)), bounceConserve(B)/> { }
	void bounce() {
		Vector v(magnitude()*bounceConserve, -degrees());
		x = v.x;
		y = v.y;
	}
};




int main() {
	Point maxCorner(77, 21);
	const Vector gravity(Point(0, 1));
	const Vector ground(Point(0, maxCorner.y));

	Ball ball(0, maxCorner.y, Point(2, -7), 0.8);
	while(ball.origin.x<=maxCorner.x && ball.magnitude() > 1) {
		cout << ball.origin << endl;
		ball += gravity; 
		ball++; 
		if(ball.origin.y>maxCorner.y) { 
			ball.origin = ball.getIntersect(ground);
			ball.bounce();
		}
	}

	return 0;
}



Results:
(0,21)
(2,15)
(4,10)
(6,6)
(8,3)
(10,1)
(12,0)
(14,0)
(16,1)
(18,3)
(20,6)
(22,10)
(24,15)
(26,21)
(26,21)
(27.6,16.4)
(29.2,12.8)
(30.8,10.2)
(32.4,8.6)
(34,8)
(35.6,8.4)
(37.2,9.8)
(38.8,12.2)
(40.4,15.6)
(42,20)
(42.2963,21)
(43.5763,17.68)
(44.8563,15.36)
(46.1363,14.04)
(47.4163,13.72)
(48.6963,14.4)
(49.9763,16.08)
(51.2563,18.76)
(52.0354,21)
(53.0594,19.056)
(54.0834,18.112)
(55.1074,18.168)
(56.1314,19.224)
(57.016,21)
(57.8352,20.3552)



I know, hard to follow. But if you were to plot those pixels, you'd get something like:
            * *                                           
          *     *                                         
                                                          
        *         *                                       
                                                          
                                                          
      *             *                                     
                                  *                       
                                *  *                      
                                     *                    
    *                 *       *                           
                                                          
                             *        *                   
                                               *          
                                              * *         
  *                     *               *   *             
                           *                     *        
                                           *              
                                                   *  **  
                                          *          *  *
                                                         *
*                         *               *         *    *



If the screen wraps that too much, it will probably look like ass, but hopefully you get the idea.
Was This Post Helpful? 1
  • +
  • -

#17 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 25 September 2011 - 01:58 PM

I tried and implement it in C#.

I had a little fun with operator overloading :D funny what you can do with these!
== and != compares x and y
It is possible to write Vector2D v = new Point(x, y); (I don't think this will ever be useful :P )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2 {
    public class Vector2D {
        private double x, y;

        private Vector2D(double x, double y) {
            this.x = x;
            this.y = y;
        }

        public static Vector2D Create(double x, double y) {
            return new Vector2D(x, y);
        }

        public static Vector2D CreateFromLengthAndAngel(double length, double angel) {
            double cosv = RadianToDegree(Math.Cos(DegreeToRadian(angel)));
            double sinv = RadianToDegree(Math.Sin(DegreeToRadian(angel)));
            return new Vector2D(length * cosv, length * sinv);
        }

        public static Vector2D CreateFromLine(double x1, double y1, double x2, double y2) {
            return new Vector2D(x2 - x1, y2 - y1);
        }

        private void Add(Vector2D other) {
            x += other.x;
            y += other.y;
        }

        private void Substract(Vector2D other) {
            x -= other.x;
            y -= other.y;
        }

        public double GetAngelBetweenVector(Vector2D other) {
            double cosv = GetDotProduct(other) / (GetLength() * other.GetLength());
            return RadianToDegree(Math.Acos(cosv));
        }

        public double GetDotProduct(Vector2D other) {
            return x * other.x + y * other.y;
        }

        public double GetLength() {
            return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
        }

        public static implicit operator Vector2D(Point p) {
            return new Vector2D(p.X, p.Y);
        }

        public static Vector2D operator +(Vector2D v1, Vector2D v2) {
            v1.Add(v2);
            return v1;
        }

        public static Vector2D operator -(Vector2D v1, Vector2D v2) {
            v1.Substract(v2);
            return v1;
        }

        public static bool operator ==(Vector2D v1, Vector2D v2) {
            return v1.x == v2.x && v1.y == v2.y;
        }

        public static bool operator !=(Vector2D v1, Vector2D v2) {
            return !(v1.x == v2.x && v1.y == v2.y);
        }

        public override int GetHashCode() {
            return base.GetHashCode();
        }

        public override bool Equals(object obj) {
            if (obj is Vector2D) {
                Vector2D other = (Vector2D) obj;
                return x == other.x && y == other.y;
            }
            return base.Equals(obj);
        }

        public static double DegreeToRadian(double angel) {
            return Math.PI * (angel / 180);
        }

        public static double RadianToDegree(double angel) {
            return angel * (180 / Math.PI);
        }

        public double X {
            get { return x; }
        }

        public double Y {
            get { return y; }
        }
    }
}



Edit: forgot to add the + and - operators

This post has been edited by CasiOo: 25 September 2011 - 02:05 PM

Was This Post Helpful? 1
  • +
  • -

#18 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 25 September 2011 - 02:15 PM

Quote

It is possible to write Vector2D v = new Point(x, y); (I don't think this will ever be useful )


Actually, that looks pretty cool to me. Overloading the assignment operator to make type conversion easier is something you cannot do in Java that I would fine useful sometimes.
Was This Post Helpful? 0
  • +
  • -

#19 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 26 September 2011 - 11:24 AM

*
POPULAR

Why let the class based OO languages have all the fun? Time for some prototypes...

Spoiler


Yes, it's Javascript. Why? Because HTML5 is gonna take over the world! :P

Ok, maybe not, but it does offer the importunity to share a demo with everyone. If your browser doesn't support HTML5... WTF is wrong with you, it's going to take over the world. ;)

Anyway, enjoy the bouncy balls.
Was This Post Helpful? 6
  • +
  • -

#20 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 26 September 2011 - 11:39 AM

You're insane, baavgai! :rockon:
Was This Post Helpful? 0
  • +
  • -

#21 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 28 September 2011 - 01:37 PM

He's right though. Development with in Microsoft's Metro for Windows 8 is will be done mostly with HTML 5 with Javascript from what I've seen. Thankfully my browser already supports HTML 5 so I'm good there.
Was This Post Helpful? 0
  • +
  • -

#22 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 28 September 2011 - 01:42 PM

Javascript and HTML5 isn't the only support language for Metro Style apps. They can be programmed with most of the .net languages, even C++ is supports. Thanks to the Windows RunTime.

This post has been edited by AdamSpeight2008: 28 September 2011 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#23 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 28 September 2011 - 01:53 PM

Yes, though .NET and C++ are severely limited compared to pre-Metro. The whole Metro thing is more "web" style than "desktop" style in security features. It is much harder to access the local file system for instance. It was designed with tablets and other portable devices in mind. From what I've been seeing around the web Microsoft seems to be pushing HTML 5 over older technologies lately and they seem to be of the belief that it will replace older technologies.
Was This Post Helpful? 0
  • +
  • -

#24 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 28 September 2011 - 02:01 PM

MSDN: Building your first Windows Metro style app using C#, C++, or Visual Basic

Microsoft Visual Studio 11 Express for Windows Developer Preview (Needs to run on the Windows8 Developer Preview

Build 2011: Metro Style Application Video

This post has been edited by AdamSpeight2008: 28 September 2011 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#25 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 28 September 2011 - 02:05 PM

Yes, I know about all of this. I'm just saying that Metro is a paradigm shift from old school programming methodology. First look at Visual Studio 11.
Was This Post Helpful? 0
  • +
  • -

#26 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 30 September 2011 - 07:57 AM

I'm already one step ahead of you! Here is my snippet I wrote just for DIC.

http://www.dreaminco...snippet5958.htm
Was This Post Helpful? 1
  • +
  • -

#27 RSouthee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-January 12

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 16 March 2012 - 12:14 PM

I know this is an old topic but I was wondering if it would be better to have the 'x' and 'y' values as private members and use Get/Set accessors or let them be public.
Was This Post Helpful? 0
  • +
  • -

#28 samirashafir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 17-January 13

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 17 January 2013 - 03:47 AM

A good approach to this would be to have a look at the SDL library. I'm not saying it's necessarily the best library for 2D games, but it's easy to get started with and the web is flooded with tutorials and open source code samples for simple homebrew 2D games written using SDL.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2