Angle and speed on an isometric map.

Started by
6 comments, last by reppinfreedom 11 years, 5 months ago
Can someone verify that this math is correct? If your traveling up or down on an isometric map the speed of the character should be half that of traveling horizontal. What I am not sure about is traveling at an angle. The character speed should be the horizontal and vertical speeds divided together correct? So 0.75. This will be multiplied times the actual character speed.

Hmmm... Going to go see if I can find some information on eight directional isometric character movement speeds...

[source lang="java"]
public static function animationAndSpeedToNextRowCol(currentRowCol:Object, nextRowCol:Object, tileSizeX:int, tileSizeY:int):Object {
var angleSpeed:Object = new Object();
var Angle:int=Math.floor(gameMath.angleBetweenPoints(rowColToPoint(currentRowCol,tileSizeX,tileSizeY),rowColToPoint(nextRowCol,tileSizeX,tileSizeY)));

if (Angle == 0) {
angleSpeed.Animation="l";
angleSpeed.Speed=1;
} else if (Angle == 26) {
angleSpeed.Animation="ul";
angleSpeed.Speed=0.75;
} else if (Angle == 90) {
angleSpeed.Animation="u";
angleSpeed.Speed=0.5;
} else if (Angle == 153) {
angleSpeed.Animation="ur";
angleSpeed.Speed=0.75;
} else if (Angle == 180) {
angleSpeed.Animation="r";
angleSpeed.Speed=1;
} else if (Angle == 206) {
angleSpeed.Animation="dr";
angleSpeed.Speed=0.75;
} else if (Angle == 270) {
angleSpeed.Animation="d";
angleSpeed.Speed=0.5;
} else if (Angle == 333) {
angleSpeed.Animation="dl";
angleSpeed.Speed=0.75;
} else {
angleSpeed=null;
}
return angleSpeed;
}
[/source]
Advertisement
Here is something you should copy from the 3d world: world space and view space.
You should do all your game logic in world space, which should not differ between moving up or left. The view space is what you currently see and you need two functions to convert between the two spaces.
world2View(x,y) := (x-y*0.5,x+y*0.5) + (screenoffset_x,screenoffset_y)
view2World(x,y) := ...homework...

The world2View transformation is only needed for rendering your scene, please remember, that your world is somewhat turned by 45 degree, therefore you just can't make 1:1 conversion. The other way around (view2World) is needed whenever you want to convert input (e.g. mouse input) back to your world space.

There's some math involved which isn't hard, but makes the handling much easier. Once you work in world space independently of the screen lot of issues just vanish, like your movement example :D
No, that math of mine was definitely not correct... I just checked the math...

Here are the tile distances...
Horizontal:92
Vertical: 46
Angular: 51.5 Rounded, Actually: (51.42956348249516)

According to my calculations angular speed should be... 0.55978260% of Horizontal. So a multiplier of 0.56 would be correct? Hmmm... That doesn't sound right... I think I need to calculate out non isometric tile distances first to solve this easier...
I have conversion classes for converting between isometric and normal coordinates... I just want to keep the code lighter and faster.

If I wanted perfect accuracy I would calculate out everything on a non isometric coordinate grid. I would just set a velocity and angle... But, I want things to run faster and not require many conversions and calculations.
[source lang="java"]
public static function getXyVelocity(Velocity:Number, Angle:Number):Point {
var newPoint = new Point();
newPoint.x = - Velocity * Math.cos(Angle * Math.PI / 180);
newPoint.y = - Velocity * Math.sin(Angle * Math.PI / 180);
return newPoint;
}
[/source]

Then I would run a conversion to isometric velocity and angle from normal.

[source lang="java"]
package Michael{

public class iso3dTransformer {

static var Ratio:Number=2;
public static function convertToIso(screenPoint:Object):Object {

var zPos:Number=screenPoint.z;
var yPos:Number=screenPoint.y-screenPoint.x/Ratio+screenPoint.z;
var xPos:Number=screenPoint.x/Ratio+screenPoint.y+screenPoint.z;
return new Object(x:xPos,y:yPos,z:zPos);
}

public static function transformForDisplay(displayPoint:Object):Object {

var zPos:Number=displayPoint.z;
var yPos:Number=(displayPoint.x+displayPoint.y)/Ratio-displayPoint.z;
var xPos:Number=displayPoint.x-displayPoint.y;

return new Object(x:xPos,y:yPos,z:zPos);
}

}
}
[/source]
When you already got your conversion code, just enter the unit vectors for your 8 directions wink.png , the unit vector for diagonal world movement (=horizontal/vertical screen movement) would be
x ~= 0.707, y ~= 0.707 (change sign accordingly).
The problem is I only have one of the cartesian to polar/polar to cartesian functions I need both functions to solve this...

I need to convert the x/y velocity from the normal to isometric conversion function into speed and angle... Hmmm... *Searching.*
Okay... So I created a function to convert a x/y velocity into an angle...

Now, I need to solve for the velocity... It something as simple as Math.sqrt(xVel + yVel)?<br />
[source lang=&amp;amp;quot;java&amp;amp;quot;]<br />
public static function xyVelToAngle(xyVel:Point):Number {<br />
   var Angle:Number = Math.atan2(xyVel.y,xyVel.x);<br />
   var Degrees:Number = 360 * (Angle / (2 * Math.PI));<br />
   if (Degrees &amp;amp;lt; 0) {<br />
    Degrees = Degrees + 360;<br />
   }<br />
   return Degrees;<br />
  }<br />
[/source]
Aha... Viola! Now this should be correct in converting xyVel into a velocity and an angle... Now I should be able to solve it.
[source lang="java"] public static function xyVelToVelAngle(xyVel:Point):Object {
var velAngle:Object = new Object();
var Angle:Number = Math.atan2(xyVel.y,xyVel.x);
var Degrees:Number = 360 * (Angle / (2 * Math.PI));
if (Degrees < 0) {
Degrees = Degrees + 360;
}
velAngle.Angle = Degrees-180;
velAngle.Vel = Math.sqrt(xyVel.x * xyVel.x + xyVel.y * xyVel.y);
return velAngle;
}[/source]

Edit: Changed "velAngle.Angle = Degrees" to "velAngle.Angle = Degrees-180"... Now correct!


Those functions were a little funky... This should be more proper and readable.

[source lang="java"]
public static function xyVelToVelAngle(xyVel:Point):Object {
var velAngle:Object = new Object();
var Angle:Number = Math.atan2(xyVel.y,xyVel.x);
var Degrees:Number = radToDeg(Angle);
velAngle.Angle = Degrees;
velAngle.Vel = Math.sqrt(xyVel.x * xyVel.x + xyVel.y * xyVel.y);
return velAngle;
}

public static function angleBetweenPoints(Location:Point, Target:Point):Number {
var dx:Number = Location.x - Target.x;
var dy:Number = Location.y - Target.y;

var Angle:Number = Math.atan2(dy,dx);
var Degrees:Number = radToDeg(Angle);
return Degrees;
}

public static function radToDeg(Radian:Number):Number {
return Radian * 180 / Math.PI;
}
public static function degToRad(Degree:Number):Number {
return Degree * Math.PI / 180;
}
[/source]


Ok, this is what I came up with...

[source lang="java"]
public static function animationAndSpeedToNextRowCol(currentRowCol:Object, nextRowCol:Object, tileSizeX:int, tileSizeY:int):Object {
var angleSpeed:Object = new Object();
var Angle:int=Math.floor(gameMath.angleBetweenPoints(rowColToPoint(currentRowCol,tileSizeX,tileSizeY),rowColToPoint(nextRowCol,tileSizeX,tileSizeY)));

if (Angle == 0) {
angleSpeed.Animation="l";
angleSpeed.Speed=1;
} else if (Angle == 26) {
angleSpeed.Animation="ul";
angleSpeed.Speed=0.707;
} else if (Angle == 90) {
angleSpeed.Animation="u";
angleSpeed.Speed=0.5;
} else if (Angle == 153) {
angleSpeed.Animation="ur";
angleSpeed.Speed=0.707;
} else if (Angle == 180) {
angleSpeed.Animation="r";
angleSpeed.Speed=1;
} else if (Angle == -154) {
angleSpeed.Animation="dr";
angleSpeed.Speed=0.707;
} else if (Angle == -90) {
angleSpeed.Animation="d";
angleSpeed.Speed=0.5;
} else if (Angle == -27) {
angleSpeed.Animation="dl";
angleSpeed.Speed=0.707;
} else {
angleSpeed=null;
}
return angleSpeed;
}
[/source]



Edit: Ok... I finally managed to run a test that generated the numbers for me... These are the final multipliers simplified to four decimals of accuracy.

Sideways: 1.4142
Angle: 1.118
Up/Down: 0.7071

This topic is closed to new replies.

Advertisement