Jump to content

Experimenting with unit movement, need math help


Recommended Posts

I'm new to the forum and to hacking on 0 a.d.

Currently I'm experimenting with different unit movement logic. My trig math skills are too weak to get me through this. :( Also, I'm just too inexperienced with the code to make this work. Instead of using the formation logic of offsets, I want to write more dynamic simulation-like behaviors. This is a fun experiment for me that I've been wanting to do for a while now.

Specific Question:

=====

Given a unit with a starting position and direction/angle, how do I project to a point along that angle to be a distance of 20 units away? I understand the math formula used to do this, but when it comes down to radians, negative angles/quadrant, game's coordinate system, etc. I get all confused.

Here's some example code: (/simulation/components/UnitAI.js)


var formationTarget = this.order.data;
// Loop through each member of formation and apply the behavior
for each (var ent in cmpFormation.members)
{
var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
var entPos = Engine.QueryInterface(ent, IID_Position);
if (!entPos || !entPos.IsInWorld())
continue;
var pos = entPos.GetPosition();

//
// do stuff that modifies desired direction to take from here
//

// Project new target point to move towards.
var distanceAway = 20;
var x = pos.x + (distanceAway * Math.cos(angle));
var z = pos.z + (distanceAway * Math.sin(angle));

// Move unit towards the new point
cmpUnitAI.MoveToPoint(x, z);
}

I'm getting an angle like -1.5 which I don't know what to do with. The units are moving but more eradic and seemingly unrelated to the command location that the direction is based on.

I'm pretty sure my math skills are to blame. Would really appreciate any help.

-Mark E

Edited by brainlid
Link to comment
Share on other sites

The code you posted above will work correctly when an angle of 0 means in the positive x direction and and increasing angle moves the unit anti clockwise. Javascript uses radians for trig functions, if you prefer working in degrees just use Math.sin(angle * Math.PI / 180). The game coords have x pointing across the map to the right and z pointing up the map (based on the default view angle).

This is about as much as I can help with the information you have given.

Generally it is better to work with vectors rather than angles, this gives better performing code and is often clearer if you have a decent understanding of vector maths.

Link to comment
Share on other sites

Generally it is better to work with vectors rather than angles, this gives better performing code and is often clearer if you have a decent understanding of vector maths.

Thanks for the information. If I can get it working as a proof of concept then I'd love help refactoring it to use vectors. Not a strength for me.

Thanks,

-Mark E

Link to comment
Share on other sites

As an addition to what quantumstate said, it depends on what your angle is supposed to mean. Is it relative to the current moving direction or global? Also, again rephrasing what quantumstate said, you have to make sure your turning direction is always equal. Usually you only have the smallest angle in arbitrary direction. How do you calculate the angle?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...