Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


smr

Member Since 16 Jul 2004
Offline Last Active Nov 20 2014 10:53 PM

Topics I've Started

Vector math help

15 May 2014 - 01:34 PM

So, after about twenty years of hobby game development experience and fourteen years as a professional developer, I've finally decided it was time really understand the math. I don't consider myself good at math, and I haven't studied math in school since I graduated in 1999. So I'm rusty.

 

I've been working through the exercises at the end of the chapter of this book and I'm struggling with cross product. I understand how to calculate it, and I understand what it's used for, but not necessarily the math behind it. I understand it like a tool, but not in theory. I'm trying to use the cross product to determine the angle between two vectors.

 

The first part of the book's exercise:

 

Given the two vectors:

a = (0, 1, 1) 
b = (0, -1, 0)

Compute the angle between them using the scalar product. That's easy: 

2.356

The second part is what I'm struggling with:

 

Using those same two vectors, calculate the angle between them using the cross product. The book seems to indicate that the following formula is used:

|| a x b || = ||a|| ||b|| sin(theta)

It goes on to explain that's the same as this:

|| a x b || = ||a|| ||b|| sqrt(1 - (a dot b)**2)

But this isn't what I'm coming up with. I'm calculating the magnitudes of the two vectors as:

||a|| = sqrt(0*0 + 1*1 + 1*1) = 1.414
||b|| = sqrt(0*0 + -1*-1 + 0*0) = 1

Cross product:

a x b = (a lot of typing) = (1, 0, 0)
|| a x b || = 1

Dot product:

a dot b = 0*0 + 1*-1 + 1*0 = 1

Then to plug the values into the formula:

1 = 1.414 * 1 * sqrt(1 - 1) = 0

Obviously this cannot be correct. I know there's some piece I'm missing, but I can't quite figure it out from the text. Could someone help me out here?

 

Wikipedia stated that the magnitude of the cross product of the unit vectors yields the sine, but even this isn't giving me the correct angle:

â = (0, 1, 1) / sqrt(0*0 + 1*1 + 1*1) = (0, 0.707, 0.707)
b̂ = (0, -1, 0) / sqrt(0*0 + -1*-1 + 0*0) = (0, -1, 0)
â x b̂ = (.707, 0, 0)
sin(theta) = ||â x b̂|| = .707

So I thought inverse sine of .707 yield the actual angle, but it doesn't reproduce the same value I calculated with the scalar product (2.356):

arcsin(.707) = 0.785

But I have noticed that

arccos(-0.707) = 2.356

So what am I missing here? Please help!

 
 
 

Moving toward a point /without/ trig or floating point

22 August 2013 - 03:24 PM

I'm working on a mobile game and I'd like to avoid using floating point math. I'm having trouble coming up with an algorithm for moving a point a specific distance toward another point. I have no issues doing this with floating point using trig functions or vector math.

 

Does anyone recall how this was done in those long-gone days before we had FPUs to lean on?

 

Thanks!


How to specialize template for method

21 June 2013 - 01:55 PM

I am back to C++ after many, many years' absence. I'm trying to create a simple vector class (non-interesting stuff removed):

template <class T>
class Vector3
{
public:
	T length();
public:
	T x, y, z;
};

I want to specialize the length method so that I can select the correct version of sqrt to call based on type of T. The best I can come up with is the following. "Best" is really a misnomer, since it doesn't compile. Can someone enlighten me what the best way to do this would be?


template <>
void Vector3<int>::length()
{
	return (T) sqrtl((long)(this->x * this->x) + (long)(this->y * this->y) + (long)(this->z * this->z));
}
template <>
void Vector3<long>::length()
{
	return (T) sqrtl((this->x * this->x) + (this->y * this->y) + (this->z * this->z));
}
template <>
void Vector3<float>::length()
{
	return (T) sqrtf((this->x * this->x) + (this->y * this->y) + (this->z * this->z));
}
template <>
void Vector3<double>::length()
{
	return (T) sqrt((this->x * this->x) + (this->y * this->y) + (this->z * this->z));
}
template <class T>
T Vector3<T>::length()
{
	return (T) sqrt((double)(this->x * this->x) + (double)(this->y * this->y) + (double)(this->z * this->z));
}

Thanks in advance!

 

Signed,

Clueless about C++


So I'm preparing to ask my girlfriend to marry me

13 June 2013 - 03:10 PM

Would it be inappropriate to Rickroll her by engraving "Never gonna give you up" into the ring?


Avoiding the C++ chicken or egg problem

05 June 2013 - 04:22 PM

How do I avoid having to forward declare (and thus violating the DRY principle) given the following:

 

World.h

#include "Entity.h"

class Entity;
typedef std::shared_ptr<Entity> Entity_ptr;
typedef unsigned int entity_id;

class World
{
public:
	void add_entity(Entity_ptr entity);
	void remove_entity(entity_id entityId);
};

typedef std::shared_ptr<World> World_ptr;

Entity.h

#include "World.h"

typedef unsigned int entity_id;

class World;
typedef std::shared_ptr<World> World_ptr;

class Entity
{
public:
	World_ptr world() const { return world_; }
	void set_world(World_ptr world) { world_ = world; }
};

typedef std::shared_ptr<Entity> Entity_ptr;

 

Because each file depends on the other, neither can compile unless I forward declare the class and the typedef for the shared_ptr.

 

I've not coded with C++ in, oh, about eight years. So I'm a little rusty.

 

Thanks!


PARTNERS