Hi,
thank you both for the infos
I have a 2 dim Array where right now each obstancle has a value>0, all empty fields have 0.
My houses may be larger or smaller than one field, and trees are smaller in every case I guess.
That's why I thought I'd combine a pathfinding with a bounding box collision detection so they always do the next step and when they collide, they got set back to last position with a new pathfinding with this field marked somehow I don't know exactly. There can also be collisions with other moving meshes/units...
This is why I have some problems here.
The moving is simple, there are some fields the villagers are randomly sent to to 'work' there. they are just moving between those fields.
The monsters later should run after the villagers and kill/eat them.
Those both should not move 'trough' houses trees etc and also not 'through' other moving units.
An approach would be enough if they make a 'large' way round the house it is okay, they just should not walk right 'through' it.
Here some code of the villagers, they now always walk the shortest way set by the starting angle to the target point.
Code:
MeshBauer = class(TDJXSprite)
boolstand: boolean;
nsx,nsy,nsz,nspeed,nHOff: single;
Radius: single;
ntyp: integer;
nanimpos,nrotatey: single;
ntargetx,ntargetz: single;
procedure Process(SpeedFactor: single); override;
procedure Draw; override;
end;
procedure TForm1.add_bauer(nx,nz: single);
begin
with MeshBauer.Create(Engine) do
begin
nspeed:=0.025;
ntyp:=0;
nsx:=0.19;
nsy:=nsx;
nsz:=nsx;
x:=nx;
z:=nz;
get_bauerntarget(ntargetx,ntargetz);
Radius:=15;
nrotatey:=GetAngle(x,z,ntargetx,ntargetz);
end;
end;
procedure MeshBauer.Draw;
var mesh: tdjxmesh;
nanim: integer;
begin
inherited;
if boolstand then nanim:=0 else nanim:=1;
mesh:=form1.MeshListBauer.Meshes[nanim];
Form1.DanJetX1.Matrices3D.ClearWorldMatrix;
Form1.DanJetX1.Matrices3D.ScaleWorld(nsx,nsy,nsz);
Form1.DanJetX1.Matrices3D.MoveWorld(x,y,z);
Form1.DanJetX1.Matrices3D.ApplyWorldMatrix;
if mesh.IsInFrustum then mesh.render(x,y,z,0,-nrotatey-pi/2,0,nsx,nsy,nsz,round(nanimpos));
nanimpos:=nanimpos+nSpeed*time_elapsed;
end;
procedure MeshBauer.Process(SpeedFactor: single);
var tx,tz: double;
begin
inherited;
if not boolstand then begin
//Falls Entfernung geringer als Geschwindigkeit direkt ans Ziel setzen
if Distance(x,z,ntargetx,ntargetz) < time_elapsed/36 then
begin
boolstand:=true;
form1.get_innomtarget(ntargetx,ntargetz);
nrotatey:=GetAngle(x,z,ntargetx,ntargetz);
end else begin
//Eigentliches Bewegen
tx:=x;
tz:=z;
form1.move_forward(tx,tz,nrotatey,time_elapsed/36);
x:=tx;
z:=tz;
end;
Y:=Landscape.Altitude[X,Z]+0.65;
end;
end;
Understandable? Don't care about the draw method, it is working great,
it is just how I move them now.
get_Bauerntarget just delievers the Target location to walk to.
Code:
procedure TForm1.move_forward(var X, Z: double; nRotate,nSpeed: single);
var Dir: TD3DXVector3;
begin
Dir.X := cos(nRotate);
Dir.Z := sin(nRotate);
X:=X+nSpeed*Dir.X*time_elapsed;
Z:=Z+nSpeed*Dir.Z*time_elapsed;
end;
//Berechnung der Entfernung zweier Punkte:
function Distance(x,y,x2,y2: single): Real;
begin
Result := sqrt( sqr(y2-y) + sqr(x2-x) );
end;
//Winkel einer Strecke zwischen zwei Punkten:
function GetAngle(x, y, x2, y2: single): Real;
begin
Result:=arctan2(y2-y, x2-x);
end;
@Huehnerschaender: I am not good at C++ so I think I will not read that sample, or is it so good?
Right now they don't care about any stuff in my 2dim array, it looks like this:
TLevel=Array[0..levelb,0..levelh] of byte;
where Levelb and Levelh both are 256 currently.
Thanks a lot,
firle
Bookmarks