Sign in to follow this  
Headkaze

Simple isometric path

Recommended Posts

I am writing a basic isometric game and currently have room to room movement working. The problem is the characters are moving directly from room to room. I am using the following code:
float direction = atan2(xDoorEnd - xDoorStart, yDoorEnd - yDoorStart);

m_x += cos(direction) * 0.5f;
m_y += sin(direction) * 0.5f;
Simple enough, but I need the characters to walk in straight lines along the centre of the room and off in a diagonal direction. I'm just wondering if there is a simple algorithm to calculate such a path. [Edited by - Headkaze on September 11, 2009 1:08:56 AM]

Share this post


Link to post
Share on other sites
If you characters are walking on odd angles you may want to try something more like code below for getting your angle of direction.

This code is for C# but should not be to hard to change to C++. I needed an angle of two vectors and well ^_^ this is fairly precise.

One thing also to note is that the angle you pass into bolth of the math functions should be in radius format. This could be differn't for the language you are using since i see no mention of what language you are useing but here is my angle of two vectors function



public float fAngleBetween(Vector2 vPointA, Vector2 vPointB)
{
vPointA.Normalize();
vPointB.Normalize();
float fAngle = (float)Math.Acos((double)Vector2.Dot(vPointA,vPointB));
return fAngle;
}






Some important things to note is that you must normalize bolth vectors and inside of the Acos function i am useing the Dot Product of bolth the vectors this is more accurate.

this return value is not of the type of radius and you still would need to convert it to such.

Edit:

One thing that may help is if you tell us how the map data is stored is it in a 2d Array ? or something else.

Regards Jouei.

Share this post


Link to post
Share on other sites
I'm coding this in C (with C++ classes) for the DS so I don't have all the namespaces of C# unfortunately. I tried writing a normalize function and used acos from stdio.h but the movement is completely off. Could just be my bad interpretation of the functions.

Did you take a look at the picture linked in the first post? It's basically a 2d game which is slightly isometric. The maps are simply done in photoshop and the collision maps only show where the doors are located so they will be no help with the movement.

Anyway here is my code interpreted from what you posted. I have no idea of the kind of movement it's supposed to produce.

void normalize(int* x, int* y)
{
float den;

if((den = sqrt(*x * *x + *y * *y)) != 0.0)
{
*x /= den;
*y /= den;
}
}

normalize(&xSrc, &ySrc);
normalize(&xDest, &yDest);

float direction = acos(xSrc * xDest + ySrc * yDest);

m_x += cos(direction) * 0.5f;
m_y += sin(direction) * 0.5f;


I'm starting to think I need to break up the path into sections since each movement will need 3 parts; moving out from the door on a slight angle, moving across the floor horizontally and then moving into the door on a slight angle. I was just hoping there would be a simple algorithm to calculate it, because it's kinda like a bezier curve with straight lines.

Share this post


Link to post
Share on other sites
Well i am not sure about an algorithm but here is an idea that may work.

i am unsure as to how the player moves so i am going to assume that they uses some kind of d-pad here.

So check the angles to the doors in view if the angle is less then say 10 degrees in the left and right direction and the player is in that range then they are able to move up then you do what you were doing before and this should make the player move towards the door in the way you want.

if the angle is not within range lock them on the x axis until the reach the desired area.

I am not sure how difficult or not something this would be but i do not think it would be to bad.

Besides that its all i can tell you give what i know as to how the player moves.

Regards Jouei.

Edited Made it a bit more understandable.

Share this post


Link to post
Share on other sites
I already have the player movement working fine. Moving up and down is on an angle like the image I posted, and left and right is just left and right.

The algorithm I'm working on is for the computer AI which is characters walking around the mansion from room to room and door to door. I already have the room to room path finding routine done, now I just need a simple door to door routine. Currently it has the characters moving directly door to door but I need it more like in this picture.

So perhaps I need to write a function that takes a value from 0 to 1 and calculates the following type of path:

/________
/


Share this post


Link to post
Share on other sites
I think my biggest question is how are you determining the door that is chosen.

if you can tell me that it may help out.

If the door is pre-chosen and you just want the Object to move from one spot to it that could be done in a simple way.

Basicaly speaking if the the postion of the object is x50 and the y 50 and the door you want is x300 y 100 then simply move positive on the x axis until you are within a give range of pixels to the door eg



public void move(Vector Object,Vector Location)
{
int Difference = Object.x - Location.x;
if(Differnce < 20 || Differnce > -20)
{
if(Differnce > 0)
{Object.x += 5;}
else
{Object.X -= 5}

}
else
{
//Do you angle caculation to the door
Angle = GetAngle(Location);
Object.X += cos(Angle) * 0.5f;
Object.Y += sin(Angle) * 0.5f;
}

}




Call this update every frame and you should get a result to what i think you want.

Simply put head towards the door on the X axis and when you within say 20 pixels of the center of the door head towards the door.

this oveusly is not tested but the idea should be sound.

Now if you get there and you want another door just change the location vector.

let me know if this helps at all.

Regards Jouei.

Share this post


Link to post
Share on other sites
Thanks for your help Jouei I nearly have it working perfectly apart from when a character is moving from a top door towards a top right door. He will slow down gradually until he completely stops before he reaches the door.

if(abs(xDist) < 20)	 		// Near the door
{
if(xDist > 0) // Move directly towards it
m_x += 0.6f; // right
else
m_x -= 0.6f; // left

if(yDist > 0) // Move directly towards it
m_y += 0.3f; // down
else
m_y -= 0.3f; // up
}
else
{
if(yPos > 160) // Below centre of room so move up diagonally
{
m_x += 0.6f;
m_y -= 0.3f;
}
else if(yPos < 160) // Above centre of room so move down diagonally
{
m_x -= 0.6f;
m_y += 0.3f;
}
else
{
float direction = atan2(yDist, xDist);
// Move directly towards door
m_x += cos(direction) * 0.5f;
m_y += sin(direction) * 0.5f;
}
}




EDIT: Okay nevermind seems the problem is I needed to comment out the following line

m_y += sin(direction) * 0.5f;


Now I just need to tweak it a bit more but overall it seems to be working great! Thanks for your help :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this