java - How to calculate a rectangular prism using min/max of X, Y, and Z vertices -
i want make game player on 3d plane, able collide objects around him. plan collisions wrapping 3d models in game invisible rectangles cannot entered. have written code calculate min/max of x,y, , z of each vertex find highest , lowest point of each vertex. how make rectangular prism?
here code far:
public collisionmodel(list<vector3f> vert) { float xlow = 1000; float xhigh = 0; float ylow = 1000; float yhigh = 0; float zlow = 1000; float zhigh = 0; for(vector3f v : vert) { if(v.x > xhigh) { xhigh = v.x; } else if(v.x < xlow) { xlow = v.x; } if(v.y > yhigh) { yhigh = v.y; } else if(v.y < ylow) { ylow = v.y; } if(v.z > zhigh) { zhigh = v.z; } else if(v.z < zlow) { zlow = v.z; } } }
- the initial value of
min,maxshould first vertex not hard-coded0,1000!!! - you find
min,maxgives bounding box called prism.
now need collision testing. problem object can move/rotate ... need apply transformations box first. let construct bounding box vertexes first. in 3d 8 points:
p0 = ( xlow , ylow , zlow ) p1 = ( xhigh , ylow , zlow ) p2 = ( xhigh , yhigh, zlow ) p3 = ( xlow , yhigh, zlow ) p4 = ( xlow , ylow , zhigh ) p5 = ( xhigh , ylow , zhigh ) p6 = ( xhigh , yhigh, zhigh ) p7 = ( xlow , yhigh, zhigh ) now apply object transformations each of them. , lastly need add collision test. want test if 2 bounding boxes collide or not. need test if edge line of bbox1(q0..q7) intersects face bbox2(p0..p7). faces are:
p0,p1,p2,p3 p4,p5,p6,p7 p0,p1,p5,p4 p1,p2,p6,p5 p2,p3,p7,p6 p3,p0,p4,p7 and edge lines are:
q0,q1 q1,q2 q2,q3 q3,q0 q4,q5 q5,q6 q6,q7 q7,q4 q0,q4 q1,q5 q2,q6 q3,q7 for intersection need google equation (i lazy derive or search you) here first google hit found:
yes can use triangle vs. line intersection face 2 joined triangles ... there option , convert lines coordinate system of other bbox , compute points each line 1 of x,y,z = min , max , test if other 2 coordinates in range or not ..
Comments
Post a Comment