PDA

View Full Version : 3d Engine Collision



Memphis
08-03-2009, 08:32 PM
hi there,

i've been working on a new directx9 3d engine and have a small amount complete, however i do not know what direction to look or even how to begin with collision detection inside of a model..

for example - i have built a simple room (.x) model file, now lets say i put in a player model (human model) again a .x model file, how would i check the collision with floor and walls (basically how to seperate the floor and walls to be checked for collision with the player collision box).. i'm not totally sure how to explain.. hope you understand lol

My Engine Example (http://www.meka-meka.com/projects/accel/Accel.rar)

thanks in advance

;MM;

noeska
08-03-2009, 08:45 PM
First imagine your model to be a sphere. (and your player/camera too)
As a sphere has a radius it is easy to detect collision.

However that is not verry precies. But it is a starter.
So once you detected a sphere collision it is time to move on to perhaps detect collision at triangle level. Leaving you to compare all triangles of both meshes. So for multiple part meshes it could be adiviseable to represent eache part as an sphere before going in deeper.

Also do some research on (axis aligned) bounding box collisions as they are more presice then spheres.

Memphis
08-03-2009, 08:53 PM
hi thanks for quick reply

i used collision box for my 2d engine, i understand using a collision/bounding box, however i am unsure how to even begin todo that in 3d with x models, in my 2d engine my models were a custom struct with collision params etc, so now with .x in 3d i am lost.

thanks

Memphis
09-03-2009, 02:27 PM
ok i came up with an idea whilst messing around,



stModel = record
pMesh: ID3DXMesh; // mesh object
pMeshMaterials: PD3DMaterial9Array; // Materials for mesh
pMeshTextures: PIDirect3DTexture9Array; // Textures for mesh
NumMaterials: Integer; // Number of mesh materials
x,y,z,a,s: Single;

//bounding box, collision data here. ?
//a linked list of co-ords to check against other stmodels for colli sions ?
end;
PstModel = ^stModel;


ok so this is my model struct, holds info on each model i load into my engine, my idea is to have a linked list of co-ords that can be checked against other models in my engine for collision (obviuously once i get this to work i will add some sort of bounding range so no collision is checked if not in range (bounding box)) but my question now would be for when my model is loaded, how can i get a list of triangles and positions from the mesh interface?

by doing this, the position will not have to be changed unless the model is moved which then i can just update the co-ords, however most models will have a static positioning anyways.

hope someone can help.

;MM;

noeska
09-03-2009, 05:06 PM
going from 2d boundix boxes to 3d should be equal to doing 2d bounding boxes except that you now also have a z axis ;-)

But here's an tutorial: http://www.toymaker.info/Games/html/collisions.html

chronozphere
09-03-2009, 05:14 PM
Do you use ID3DXMesh to load the .x files? Then it's just a matter of locking it's vertex-buffer by calling LockVertexBuffer() and reading data from that buffer. The same can be done with the indexbuffer, if you want to know how the different vertices are used to form triangles. Check MSDN for more information about this:

http://msdn.microsoft.com/en-us/library/bb205749(VS.85).aspx

But i still do not understand your way of collision checking using points. Are you just checking whether any point lies at the other side of let's say a wall?

If you check your meshes against planes and bounding spheres it's okay. But if you want to check collision between two meshes that are both made up of these points, it'll never work. It is extremely unlikely that any point of Mesh A lies on the exact same location as a point of Mesh B. Moreover, it can happen that a small cube (made up of 6 points) lies completely inside a bigger one. A collision check using your points approach will tell us that there is no collision because none of the points of the two meshes are on the same location. It should tell us that a collision has occured because they obviously intersect.

I suggest to think in terms of volumes rather than points. Volumes consist of an infinit ammount of points and will be reliable. Checking two volumes against eachother is like checking whether two collections of an infinit ammount of points share some points. you can use spheres (the easiest way)... Or planes, axis-aligned boxes, oriented boxes (they have rotation)... You could also check out the seperating axis theorem that describes how to check for collision between two convex volumes but that's slightly more advanced.

Hope this helps you. :)

Memphis
09-03-2009, 05:30 PM
Hi thanks both for replies.

yes i use ID3DXMesh to load the x files, the way i was thinking is. if i have a list of points, i can check if any points pass between the points of other models. (that are in range of collision checking)

as for a simple bounding box, i would love just todo this for now, but the problem i have is, one of my models is a room. (walls, floor, etc) if i calculate a bounding box then it covers the whole x model, so my other models (such as a human model) cant enter inside of the room due to collision even on a door way for example. If you have a better solution, i'd love to hear it. thanks in advance


a small example of my problem with bounding box.

model 1 = Room, with no ceiling.

http://www.meka-meka.com/projects/accel/eg1.PNG

ok so room is loaded up with bounding box, and now camera has a bounding box also. and now the problem, below you will see camera cant enter into the room because a bounding box around the room is useless....

http://www.meka-meka.com/projects/accel/eg2.PNG

hope you can help. Thanks again.

;MM;

chronozphere
09-03-2009, 05:58 PM
That's quite a complicated problem. ???

If your room-models are very low poly, you could check your player-model's sphere against every triangle of the current room he's in.

An article on collision-detection:


http://www.peroxide.dk/papers/collision/collision.pdf (http://www.peroxide.dk/papers/collision/collision.pdf)

If your room is high poly, doing this would probably be too CPU-intensive. Then you'd have to make a collision-mesh out of it with a reduced ammount of triangles and do the same. You could also scan your triangles to find unique planes and check them against the sphere of your player-object, but you'd need to limit each plane to it's own area because otherwise you wouldn't be able to enter a door. I've never done this, but i can tell that especially the latter really tests your math skills.

noeska
09-03-2009, 06:16 PM
can you break up your room in parts? E.g. make the floor a part and each wall a part. That way each part gets its own bounding box making things a lot simpler.

You bounding box around the complete world is a starter to go looking for collisions with bounding boxes inside it. So you go deeper and deeper inside the structure of your world model.

Memphis
09-03-2009, 06:26 PM
yes quite complicated lol, what i was planning was some sort of polygon collision



can you break up your room in parts? E.g. make the floor a part and each wall a part. That way each part gets its own bounding box making things a lot simpler.

You bounding box around the complete world is a starter to go looking for collisions with bounding boxes inside it. So you go deeper and deeper inside the structure of your world model.


do you mean to load the walls etc as seperate mesh / models? i could possibly, only problem being when it comes to larger scale buildings with more floors or so.

thanks,

noeska
09-03-2009, 07:29 PM
Yes exporting your model requeres some more effort then. Also it allows you to make parts dynamic that way (e.g. think of elevators) Also it pays back when checking collisions especially on larger worlds.

PS your cell.x is a real challenge for my glmodel .x loader. ???

Memphis
09-03-2009, 07:31 PM
i have been looking into the dx function 'D3DXLoadMeshHierarchyFromX' i'm wondering if it is possible to load a hierarchy of frames, atm i have a floor frame, and i could create all walls as frames etc, keeping everything in 1 x file as 1 model, and then creating a bounding box around the frames.


btw what is your glmodel x loader? :o lol

;MM;

noeska
09-03-2009, 08:01 PM
btw what is your glmodel x loader? :o lol


http://www.noeska.com/dogl/glModel.aspx

Memphis
09-03-2009, 08:35 PM
interesting project you have there 8)

ok so.. i have done some looking around and only thing i think will be best is polygon collision, i can optimise meshes at a later stage. so by each stModel struct having a linked list of polygon/vertices and then any moving object can have their vertices checked to see if they intersect the stModels vertices. i can do this for specific types of models loaded (in this case the room/building only), and then do bounding box collision checks for other objects added into the room.

does anyone have an example of how to get a list of all the vertices per polygon from a mesh? sorry for trouble. thanks in advance for any more help.

;MM;

noeska
09-03-2009, 09:00 PM
Now that is not easily explained.

Basicly a mesh consists of a list of vertices and a list of indices.

If a polygon/face consists of 3 vertices for the first face you would need to look up the vertices for indice[0], indice[1] and indice[2]. So if indice[0] returns 10 you need to read out vertice[10]. etc.
You should be able to read this info from ID3DXMesh .

I cannot tell how to do this specific for ID3DXMesh as i do not use D3D.

chronozphere
10-03-2009, 05:52 PM
I have used ID3DXMesh and it works like you described. :)

Lock the vertexbuffer to retrieve a pointer to video-memory. This pointer points to an array of vertex structs/records. Lock the indexbuffer and read the indices in groups of three (each group is a triangle/face).. each of the indices refers to a vertex in the vertexbuffer. That's how it's done. :)

Memphis
10-03-2009, 06:51 PM
hi yes i am trying, however i dont seem to be able to get it to work



pVertices, pIndices: Pointer;

SetLength(tModel.lstTri, pNewMesh.GetNumFaces);

pNewMesh.LockVertexBuffer(D3DLOCK_READONLY, pVertices);
pNewMesh.LockIndexBuffer(D3DLOCK_READONLY, pIndices);

for i := 0 to pNewMesh.GetNumFaces do begin
tModel.lstTri[i].X := // ... lost here..
tModel.lstTri[i].Y :=
tModel.lstTri[i].Z :=
end;


i am having trouble using these both. do you have any examples of this routine? thanks.

Dan
11-03-2009, 04:39 AM
type
PVec3 = ^TVec3;
TVec3 = record x, y, z: Single; end;
TFace = array[0..2] of DWord;
TMeshPositions = array of TVec3;
TMeshFaces = array of TFace;
PIndex2 = ^TIndex2;
TIndex2 = array[0..0] of Word;
PIndex4 = ^TIndex4;
TIndex4 = array[0..0] of DWord;
...
var
Positions: TMeshPositions;
Faces: TMeshFaces;
Mesh: ID3DXMesh;
Vertices: Pointer;
VertexStride: Word;
Indices2: PIndex2;
Indices4: PIndex4;
MeshOptions: DWord;
i: Integer;
...
MeshOptions := Mesh.GetOptions;
SetLength(Positions, Mesh.GetNumVertices);
SetLength(Faces, Mesh.GetNumFaces);
VertexStride := Mesh.GetNumBytesPerVertex;
Mesh.LockVertexBuffer(D3DLOCK_READONLY, Vertices);
for i := 0 to High(Positions) do
Positions[i] := PVec3(
Pointer(Cardinal(Vertices) + i * VertexStride)
)^;
//Unless you use a custom vertex declaration in your mesh
//it is safe to assume that the first 12 bytes are vertex positions.
Mesh.UnlockVertexBuffer;
//Depending on the size of indices (16 or 32 bit) use an
//appropriate typed pointer (Indices2 or Indices4).
if MeshOptions and D3DXMESH_32BIT = D3DXMESH_32BIT then
begin
Mesh.LockIndexBuffer(D3DLOCK_READONLY, Pointer(Indices4));
for i := 0 to High(Faces) do
begin
Faces[i][0] := Indices4^[i * 3];
Faces[i][1] := Indices4^[i * 3 + 1];
Faces[i][2] := Indices4^[i * 3 + 2];
end;
end
else
begin
Mesh.LockIndexBuffer(D3DLOCK_READONLY, Pointer(Indices2));
for i := 0 to High(Faces) do
begin
Faces[i][0] := Indices2^[i * 3];
Faces[i][1] := Indices2^[i * 3 + 1];
Faces[i][2] := Indices2^[i * 3 + 2];
end;
end;
Mesh.UnlockIndexBuffer;

After that you can easily find your triangles like this:
Positions[Faces[0][0]]; Positions[Faces[0][1]]; Positions[Faces[0][2]]; //for the first triangle
Positions[Faces[n][0]]; Positions[Faces[n][1]]; Positions[Faces[n][2]]; //for n'th triangle

There may be some minor errors in the code since I just typed it in the forum and haven't checked it myself.

Memphis
11-03-2009, 12:18 PM
great ;) thanks Dan, i will check this out ASAP. and post back.

tpascal
12-03-2009, 01:31 AM
Nice topic, there are lot stuf to talk about 3d collision.

You currently have a .X file for draw your world, in this case the only info you have available to test collision is the same you have for render your word which is called "a triangle soup".

Doing some kind collision with triangles soups can be done, not any kind collision but some, for FPS type games for example is good enough where the collision your are doing is testing if you can go forward or not, backward or not, upward or not, downward or not etc, but identifing some kind geometry in your world for doing for example climbing is hard.

With triangle soup basically what you do is to use the normal (the vector that tell where the triangle is pointing to) for identify which triangles can be considered as wall, floor, ceiling or even slants, for example you can know that any triangle with normal (0,1,0) is a standable floor cos it is pointing stray UP, and any triangle with normal (0,-1,0) is a ceiling cos it is pointing stray down, for any triangle where the "Y" component is 0 you can say it is a stray wall.

Along with the normal there is also some usefull data like the "plane EQ" witch is used with the normal and one vertex triangle coordinates to calc is any position is in fornt or behind that triangle; there is also the distance formula which calc the distance between the triangle and any desired position.

Now, if your .x file has lets say 2,000 triangles then testing collision every frame is is very slower, so it is best that your soup triangle have to be pre-processed some how to allow to reject triangles as much as posible before doing test with the triangles that are really near to the player position.

A popular pre-procces is the so called BSP trees, which sort the triangle soup in a very complicated binary tree using the normal and the plane EQ of every triangle, that will allow to check very fast for any position you want which triangles are closer in front, back, up and down.

The problem i found with BSP trees is that this algorith is hard to understand and implement when one is a newbie.

More easier algorithms are for example the quadtrees and octreess which sort the triangle soup in recursive sectors so using the distance formula and calculating in which sector the player currently is you can test only the traingle that belong to that sector.


Where there are more techniques where is not involved a triangle soup, but most the time that will mean you have to do the word geometry for rendering (triangle soup) and the collison data separated but that is stuff for another post, :)

Memphis
12-03-2009, 11:04 AM
hi, interesting post, i have been having bother with the collision still, so i will definatly look into what you have mensioned. if you have any articles or tutorials on .x based maps in FPS i'd love to read them. thanks.

;MM;

Memphis
19-03-2009, 11:52 AM
for posting sake, i thought i'd post how far i am with my 3d engine.

i have now implemented map/world collision, it maybe is not best atm but it works for what i need. and it seems quick. also bare in mind i have not done any optimisations yet.

a couple shots >

http://www.meka-meka.com/projects/accel/accel1.PNG

http://www.meka-meka.com/projects/accel/accel2.PNG

to test it out, download it here > http://www.meka-meka.com/projects/accel/18032009.rar

first to load is the room. to try the land rename the cell.x and rename _cell.x to cell.x then re-open engine.

let me know what you think :)

;MM;

WILL
19-03-2009, 03:47 PM
Nice grass! :)

Memphis
19-03-2009, 06:29 PM
thanks, it is not the best atm, it is just as example, once i add variance to the land and add some plantation etc then it will look nice, however the main priority right now is indoors, which now my engine can load any .x model (static) as a map/world. but i have tons of plans for the engine.

noeska
19-03-2009, 07:05 PM
Textures do not work in the room example.

In the landscape example colision works but when moving forward and the terain goes down the camera goes floating, maybe it is time to add some gravity. (could be as simple as to always try to move the camera downwards until a colision is encountered.

Just curious how to you handle collsion checking now?

Memphis
19-03-2009, 07:56 PM
Textures do not work in the room example.

In the landscape example colision works but when moving forward and the terain goes down the camera goes floating, maybe it is time to add some gravity. (could be as simple as to always try to move the camera downwards until a colision is encountered.

Just curious how to you handle collsion checking now?


strange, textures work for me and my beta team, and can you tell me how to replicate the floating problem you found? thanks. baring in mind this is a fly by camera, it is not intended to be walking on the surface. also for the room, the textures are in the inside of the room, the camera is loaded outside, you need to move the camera up and back down into the room.........

gravity will never be applied to the camera, it will be applied to objects or actors, the camera can then be attached to an actor for example. anyways first i need to concentrate on effects now :>

Memphis
21-03-2009, 06:29 PM
after doing my own collision methods, i have read about newton, that it is not only physics but collision detection also. this has probably already been answered, i have searched the forum a little and found nothing good for me to read on implementation on newton 2.... so to try wake the forum up a little....

so again i will be using my 2 .x models as my map files, and would like to add collision detection and physics to drop in models.. does anyone had some examples of loading a mesh or so and using it with newton?

thanks in advance.

;MM;

JernejL
21-03-2009, 10:55 PM
after doing my own collision methods, i have read about newton, that it is not only physics but collision detection also. this has probably already been answered, i have searched the forum a little and found nothing good for me to read on implementation on newton 2.... so to try wake the forum up a little....

so again i will be using my 2 .x models as my map files, and would like to add collision detection and physics to drop in models.. does anyone had some examples of loading a mesh or so and using it with newton?

thanks in advance.

;MM;


You really should ask on newton forum, there's quite a few delphi newton users there, and you can get all sorts of help related to this there as not many of those users visit PGD forum.

noeska
22-03-2009, 11:44 AM
http://www.saschawillems.de/?page_id=75

Memphis
22-03-2009, 01:49 PM
after doing my own collision methods, i have read about newton, that it is not only physics but collision detection also. this has probably already been answered, i have searched the forum a little and found nothing good for me to read on implementation on newton 2.... so to try wake the forum up a little....

so again i will be using my 2 .x models as my map files, and would like to add collision detection and physics to drop in models.. does anyone had some examples of loading a mesh or so and using it with newton?

thanks in advance.

;MM;


maybe, but 1... i dont like the newton forum, 2 i'm trying to make this forum a little more active, (that is the reason i also do not like those 'google it' people - as they let forums die)

and thanks noeska, i will take a look at that :)

You really should ask on newton forum, there's quite a few delphi newton users there, and you can get all sorts of help related to this there as not many of those users visit PGD forum.

JernejL
24-03-2009, 05:33 PM
maybe, but 1... i dont like the newton forum


RANT:

What a excellent excuse :\ you are going to ignore the original forum that has newton developers that are more qualified to answer your questions more than anyone else because you don't like it.

Helping mode:

Anyway, if you need, i can give you my newton 2.0 pascal header, i think there's one on forum from koom or sury but i am not sure, if you can't find one let me know and i'll give you mine.


does anyone had some examples of loading a mesh or so and using it with newton

creating a newton collision object from a GLSCENE mesh (newton 1.35 but will probably work with 2.0 with no changes):


terrain:= NewtonCreateTreeCollision(nWorld, nil);

NewtonTreeCollisionBeginBuild(terrain);

test:= street.MeshObjects.Items[0].ExtractTriangles(nil, nil);

for i:= 0 to test.Count div 3 do begin
faces[0]:= test.Items[i*3 + 0];
faces[1]:= test.Items[i*3 + 1];
faces[2]:= test.Items[i*3 + 2];
NewtonTreeCollisionAddFace(terrain, 3, @faces[0], 12, 1);
end;

NewtonTreeCollisionEndBuild(terrain, 1);

Remember to serialize the trimesh and cache it, as it'll load much faster from serialized format.

Memphis
24-03-2009, 06:46 PM
thanks, i managed to implement version 1.5(something here) which seems to work, but if there is pascal headers for version 2, i'd be happy if you can point me in the right direction. however my new problem is still related to the newton engine... i have created a box (.x) when i load the box i have created a newton collision box, which works in my engine, however i cant get the box to be right (realistic) size around the box, and if the box size changed for example, if i loaded a differ .x mesh, i thought i could use getboundingbox with directx but i can't seem to find any info as to a way to perfect that whilst also getting the size of the box to input into newton...




ModelMan.InitModel('box.x');
Box := TNewtonBox.Create(V3({X},{Y},{Z}), V3(10, 10, 0), 10);
Box.Model := ModelMan.liModels[1];


so basically it renders and even drops to the ground, simulates a box dropping, but the size i dont know how to get, which is the {X, Y, Z} of the mesh.

JernejL
24-03-2009, 10:09 PM
thanks, i managed to implement version 1.5(something here) which seems to work, but if there is pascal headers for version 2, i'd be happy if you can point me in the right direction. however my new problem is still related to the newton engine... i have created a box (.x) when i load the box i have created a newton collision box, which works in my engine, however i cant get the box to be right (realistic) size around the box, and if the box size changed for example, if i loaded a differ .x mesh, i thought i could use getboundingbox with directx but i can't seem to find any info as to a way to perfect that whilst also getting the size of the box to input into newton...




ModelMan.InitModel('box.x');
Box := TNewtonBox.Create(V3({X},{Y},{Z}), V3(10, 10, 0), 10);
Box.Model := ModelMan.liModels[1];


so basically it renders and even drops to the ground, simulates a box dropping, but the size i dont know how to get, which is the {X, Y, Z} of the mesh.


Try calculating the AABB.

For newton 2.0 header try this header:

http://mathpudding.com/temp/Newton.pas

Memphis
24-03-2009, 10:17 PM
hi thanks dude for the header.

ok yes i use



D3DXComputeBoundingBox(@Positions[0], tModel.pMesh.GetNumVertices(), D3DXGetFVFVertexSize(tModel.pMesh.GetFVF()), tModel.minBounds, tModel.maxBounds);


this at the moment, but how would i get the size in a (x y z) format? thanks again.

JernejL
24-03-2009, 10:47 PM
this at the moment, but how would i get the size in a (x y z) format? thanks again.


Just calculate the length of the aabb along each axis.

Memphis
25-03-2009, 12:53 AM
for some reason D3DXComputeBoundingBox... this returns an x and z, but y is always zero... for example



D3DXComputeBoundingBox(PD3DXVector3(pVertices), tModel.pMesh.GetNumVertices(), D3DXGetFVFVertexSize(tModel.pMesh.GetFVF), tModel.minBounds, tModel.maxBounds);


gives me for min and max bounds

x,x = -0.516241977918882
y,y = 0
z,z = -0.609047940488495

Memphis
25-03-2009, 04:28 PM
is there a way to draw lines around the physics box, to see the difference in actual box physic size and render size?

JernejL
25-03-2009, 05:46 PM
is there a way to draw lines around the physics box, to see the difference in actual box physic size and render size?


Yes, implement newton debug rendering.

JernejL
25-03-2009, 09:14 PM
Here's my version of debug rendering routines:


procedure DebugShowGeometryCollision_Wire(const body: PNewtonBody; vertexCount: integer; const FaceArray: PFloat; faceId: integer); cdecl;
var
i: integer;
vn: integer;

procedure rendervertex(const num: integer);
begin
glVertex3fv(pointer(integer(FaceArray) + num * 12));
end;

begin
vn := vertexCount - 1;

for i := 0 to vertexCount - 1 do
begin
rendervertex(vn);
rendervertex(i);
vn := i;
end;
end;

// show rigid body collision geometry
procedure DebugShowBodyCollision_wire(const body: Pnewtonbody); cdecl;
var
tempmatrix: TMatrix4f;
begin
//NewtonBodyForEachPolygonDo(body, @DebugShowGeometryCollision_Wire);
NewtonBodyGetMatrix(body, @tempmatrix);
NewtonCollisionForEachPolygonDo(NewtonBodyGetColli sion(body), @tempmatrix, @DebugShowGeometryCollision_Wire, nil);
end;

procedure NewtonWorldForEachBodyDo(const newtonWorld: PNewtonWorld; callback: PNewtonBodyIterator);
var
thebody: PNewtonBody;
begin

thebody := NewtonWorldGetFirstBody(newtonWorld);
while thebody <> nil do
begin
callback(thebody);
thebody := NewtonWorldGetNextBody(newtonWorld, thebody);
end;

end;

// show all collision geometry in debug mode
procedure DebugShowCollision_wire;
begin
NewtonWorldForEachBodyDo(nWorld, DebugShowBodyCollision_wire);
end;

and call:


glBegin(gl_lines);
DebugShowCollision_wire;
glend;

Memphis
25-03-2009, 10:33 PM
hi thanks, atm i have

after rending box's etc



NewtonWorldForEachBodyDo(NewtonWorld, @DebugCallback);




procedure DebugCallback(const body : PNewtonBody); cdecl;
var
matrix: TMatrix4f;
begin
NewtonBodyGetMatrix(body, @matrix[0,0]);

//i don't know how to draw the lines here with dx in 3d space.
end;


^ this being my problem, i duno how to draw the lines in 3d space with dx. i've been 'googling' for ages, checked even newton forum 3-4 times. all info i find is always opengl, and i dont understand opengl at all tbh.

chronozphere
25-03-2009, 10:59 PM
I think this will interest you:

http://msdn.microsoft.com/en-us/library/bb147291(VS.85).aspx

Take a look at LineLists and LineStrips. It shows you how to draw lines by using vertexbuffers and a DrawPrimitive call. :)

Hope it helps. :)

Memphis
26-03-2009, 02:30 AM
is already late, 4.29am.. almost falling asleep :D

but anyways



procedure Polycallback(const body: PNewtonBody; vertexCount: Integer; const FaceArray: PFloat; faceId: Integer); cdecl;
var
i: Integer;
p01: array[0..1] of TD3DXVector3;
begin
i := vertexCount - 1;
p01[0] := D3DXVector3( PSingle(Cardinal(FaceArray) + i * 3 )^, PSingle(Cardinal(FaceArray) + i * 3 + 1 )^, PSingle(Cardinal(FaceArray) + i * 3 + 2)^);

for i := 0 to vertexCount-1 do begin
p01[1] := D3DXVector3( PSingle(Cardinal(FaceArray) + i * 3 )^, PSingle(Cardinal(FaceArray) + i * 3 + 1 )^, PSingle(Cardinal(FaceArray) + i * 3 + 2)^);

Engine.ResMan.DX_Device.DrawPrimitiveUP(D3DPT_LINE LIST, 1, p01, SizeOf(TD3DXVector3));

p01[0] := p01[1];
end;
end;

procedure DebugCallback(const body : PNewtonBody); cdecl;
begin
NewtonBodyForEachPolygonDo(body, @Polycallback);
end;


this is what i've tried but i can't get it to work, all lines all over the place... lol

Memphis
26-03-2009, 02:07 PM
ok i've now wrote an extra proc for testing..



procedure DrawLine(x1, y1, z1: Single; x2, y2, z2: Single);
type
TVertex = record
x: Single;
y: Single;
z: Single;
color: DWord;
tu: Single;
tv: Single;
end;
var
triData: array[1..2] of TVertex;

procedure SetVertex(ver: TVertex; _x,_y,_z: Single; _col: DWord; _tu, _tv: Single);
begin
with ver do begin
x := _x; y := _y; z := _z;
color := _col;
tu := _tu; tv := _tv;
end;
end;
begin
SetVertex(triData[1], x1, y1, z1, $FFFFFF, 0, 0);
SetVertex(triData[2], x2, y2, z2, $FFFFFF, 0, 0);

Engine.ResMan.DX_Device.DrawPrimitiveUP(D3DPT_LINE LIST, 1, triData, SizeOf(TVertex));
end;

procedure Polycallback(const body: PNewtonBody; vertexCount: Integer; const FaceArray: PFloat; faceId: Integer); cdecl;
var
i: Integer;
p0, p1: TD3DXVector3;
pList: array of TD3DXVector3;
begin
SetLength(pList, vertexCount);
CopyMemory(pList, FaceArray, vertexCount * SizeOf(TD3DXVector3));

p0 := pList[0];
for i := 1 to VertexCount - 1 do begin
p1 := pList[i];

DrawLine(p0.x, p0.y, p0.z, p1.x, p1.y, p1.z);
p0 := p1;
end;
end;

procedure DebugCallback(const body : PNewtonBody); cdecl;
begin
NewtonBodyForEachPolygonDo(body, @Polycallback);
end;


so this is what im doing now, but this draws a point in the sky, as the boxes fall lines are drawn towards the box... i just cant work it out...

VilleK
26-03-2009, 03:00 PM
One thing you should to is change SetVertex:

SetVertex(var ver: TVertex; ...

Otherwise it is only modifying a copy of the record.

Memphis
26-03-2009, 03:06 PM
ah damn i didnt realise i did that, thanks very much for spotting that, that has kind of fixed it, now i see a cube, but it is drawn no where near my actual cube lol

here is example....

Download Here (http://www.overhertz.co.uk/26032009.rar)

Memphis
26-03-2009, 04:25 PM
ok i think i have solved it, i forgot to - the matrix



DrawLine(matrix._41-p0.x, matrix._42-p0.y, matrix._43-p0.z, matrix._41-p1.x, matrix._42-p1.y, matrix._43-p1.z);



so now this is sorted, the actual physical cube is drawn different to the rendered .x model... also the physics cube goes slighty through the ground/grass

http://www.overhertz.co.uk/strange.PNG

Sample Here (http://www.overhertz.co.uk/26032009.rar)

JernejL
26-03-2009, 05:43 PM
Draw the debug geometry with no transformation (original plain world matrix).

Memphis
26-03-2009, 05:58 PM
sorry i dont understand.

after rendering my land i do



for v := Low(boxs) to High(boxs) do begin
//for v := 1 to 1 do begin
boxs[v].Render;
DebugCallback(boxs[v].NewtonBody);
end;

NewtonUpdate(NewtonWorld, PerfCount.DeltaTime);




procedure TNewtonBox.Render;
var
v: Integer;
matWorld: TD3DMatrix;
begin
NewtonBodyGetMatrix(NewtonBody, @matWorld.m[0,0]);
Engine.ResMan.DX_Device.SetTransform(D3DTS_WORLD, matWorld);

v := 0;
while (v < Model.NumMaterials) do begin
// Set the material and texture for this subset
Engine.ResMan.DX_Device.SetMaterial(Model.pMeshMat erials[v]);
Engine.ResMan.DX_Device.SetTexture(0, Model.pMeshTextures[v]);

// Draw the mesh subset
Model.pMesh.DrawSubset(v);

Inc(v);
end;
end;

Memphis
26-03-2009, 10:00 PM
ok i found what the problem was, the model (.x) had to be centered... so what i mean is...

if the model is 1, 1, 1 in 3dsmax

i had to move the z co-ordinate to -0.5 why is this? and is there a way to set the mesh even if it has not been centered in 3ds max?

thanks in advance.

---

another problem.. on my computer. the box's drop at a steady rate, on my wifes laptop, they drop very fast and even bounce.

which looks like it has something todo with newton update? when my FPS is low they drop fast. when it is high, they drop slow.

JernejL
26-03-2009, 11:18 PM
ok i found what the problem was, the model (.x) had to be centered... so what i mean is...

if the model is 1, 1, 1 in 3dsmax

i had to move the z co-ordinate to -0.5 why is this? and is there a way to set the mesh even if it has not been centered in 3ds max?

thanks in advance.

---

another problem.. on my computer. the box's drop at a steady rate, on my wifes laptop, they drop very fast and even bounce.

which looks like it has something todo with newton update? when my FPS is low they drop fast. when it is high, they drop slow.


1. mesh may be transformed by a matrix or by vertices.

2. you are sending fixed update time with a dynamic fps simulation.. send the proper deltatime to newton!

Memphis
27-03-2009, 12:14 AM
ok thanks for reply..

ok so..



if PerfCount.PhysicsElapsedTime >= (1 / 60) then begin
NewtonUpdate(NewtonWorld, 1 / 60);
PerfCount.PhysicsElapsedTime := PerfCount.PhysicsElapsedTime - (1 / 60);
end;


i have added this for newton update (which seems to be working)



procedure TNewtonBox.Render;
var
v: Integer;
matWorld: TD3DMatrix;
begin
NewtonBodyGetMatrix(NewtonBody, @matWorld.m[0,0]);

Engine.ResMan.DX_Device.SetTransform(D3DTS_WORLD, matWorld);

v := 0;
while (v < Model.NumMaterials) do begin
// Set the material and texture for this subset
Engine.ResMan.DX_Device.SetMaterial(Model.pMeshMat erials[v]);
Engine.ResMan.DX_Device.SetTexture(0, Model.pMeshTextures[v]);

// Draw the mesh subset
Model.pMesh.DrawSubset(v);

Inc(v);
end;
end;


i am using this for rendering the box, how would i go about transforming via vertices rather then matrix? thanks again you have been very helpful.

Memphis
28-03-2009, 10:10 PM
still cant find any methods, i think i will just make sure models are aligned before loading...

whilst im posting, just wondering if anyone uses the newton 2 engine or still newton 1.53 ?

reason i ask is... i was using 1.53, i could create 3500 newton box's and render them with collision and physics, throw them around etc, and get around 70fps on large land, then i tried with newton2 engine and i cant get a max of 700 box's and it runs very slow and laggy, is this because it is beta, will it get better? baring in mind the code i used is almost identical.

oh another problem i just found, small box's like tiny box's always fall through the ground or walls...

JernejL
29-03-2009, 01:45 PM
still cant find any methods, i think i will just make sure models are aligned before loading...

whilst im posting, just wondering if anyone uses the newton 2 engine or still newton 1.53 ?

1.53 is no longer supported


reason i ask is... i was using 1.53, i could create 3500 newton box's and render them with collision and physics, throw them around etc, and get around 70fps on large land, then i tried with newton2 engine and i cant get a max of 700 box's and it runs very slow and laggy, is this because it is beta, will it get better? baring in mind the code i used is almost identical.

Your graphics is probably using up most of cpu power, try without rendering and see what performance you get.. remember that physics share cpu with the rest of your game logic, and the cpu power is also important here.


oh another problem i just found, small box's like tiny box's always fall through the ground or walls...


This "tunneling" is a common issue with all physics engines caused by floating point imprecision and large size difference ratios.

Memphis
29-03-2009, 05:39 PM
1.53 is no longer supported


ok fair enough reason to move to newton 2. lol



Your graphics is probably using up most of cpu power, try without rendering and see what performance you get.. remember that physics share cpu with the rest of your game logic, and the cpu power is also important here.


since 1.53 i could render 3500 box's incomparison to newton 2 only 700. i didnt think it could be a render problem, so i removed rendering, and tried again, and it was still the same problem. i got about an extra 100 box's




This "tunneling" is a common issue with all physics engines caused by floating point imprecision and large size difference ratios.


what would be your sugestion for me?

and thanks for your reply.

JernejL
29-03-2009, 10:30 PM
1.53 is no longer supported


ok fair enough reason to move to newton 2. lol



Your graphics is probably using up most of cpu power, try without rendering and see what performance you get.. remember that physics share cpu with the rest of your game logic, and the cpu power is also important here.


since 1.53 i could render 3500 box's incomparison to newton 2 only 700. i didnt think it could be a render problem, so i removed rendering, and tried again, and it was still the same problem. i got about an extra 100 box's




This "tunneling" is a common issue with all physics engines caused by floating point imprecision and large size difference ratios.


what would be your sugestion for me?

and thanks for your reply.


#1 i have no idea why you get worse performance, every project i migrated to newton 2 shown much greater performance.. ask on newton forum for experts help.

#2: try continous collision