Dobrý deň, Ako tie trojuholníky získať som už našiel, pracujem na nájdení vzdialenosti bodu od trojuholníka pod ním. Ako ale vypočítam výšku bodu v trojuholníku(nie štvorci ako vo vašom článku) keď viem XZ súradnice a viem že to je nad trojuholníkom alebo pod ním? Ak Vás zaujíma doterajší kód:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace _3DBall
{
public struct Triangle
{
public Vector3 I1;
public Vector3 I2;
public Vector3 I3;
public Triangle(Vector3 i1, Vector3 i2, Vector3 i3)
{
I1 = i1;
I2 = i2;
I3 = i3;
}
/// <summary>
/// Checks if point is above or below or inside triangle.
/// Works if triangle is rectangular and two sides which are perpendicular each to other are each parallel to X or Z axis.
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Contains(Vector3 position)
{
float minx = Math.Min(I1.X, Math.Min(I2.X, I3.X));
float maxx = Math.Max(I1.X, Math.Max(I2.X, I3.X));
float minz = Math.Min(I1.Z, Math.Min(I2.Z, I3.Z));
float maxz = Math.Max(I1.Z, Math.Max(I2.Z, I3.Z));
return minx < position.X & position.X < maxx & minz < position.Z & position.Z < maxz;
}
private float Lerp(float min, float max, float coef)
{
return min + (max - min) * coef;
}
public float YAt(float dx, float dz)
{
throw new NotImplementedException("");
}
}
public class Extractor
{
Matrix[] transform;
Triangle[] triangle;
public Extractor() { }
private void EMMP(ModelMesh mm, ModelMeshPart mmp, ref Matrix xform,
List<Triangle> triangles)
{
Vector3[] a = new Vector3[mmp.NumVertices];
mm.VertexBuffer.GetData<Vector3>(mmp.StreamOffset + mmp.BaseVertex * mmp.VertexStride,
a, 0, mmp.NumVertices, mmp.VertexStride);
for (int i = 0; i != a.Length; ++i)
Vector3.Transform(ref a[i], ref xform, out a[i]);
if (mm.IndexBuffer.IndexElementSize == IndexElementSize.ThirtyTwoBits)
{//32bit indices
int[] indices = new int[mmp.PrimitiveCount * 3];
mm.IndexBuffer.GetData<int>(mmp.StartIndex * 4, indices, 0, mmp.PrimitiveCount * 3);
for (int i = 0; i != mmp.PrimitiveCount;++i)
triangles.Add(new Triangle(a[indices[i * 3]],a[indices[i * 3 + 1]],a[indices[i * 3 + 2]]));
}
else
{//16bit indices
short[] indices = new short[mmp.PrimitiveCount * 3];
mm.IndexBuffer.GetData<short>(mmp.StartIndex * 4, indices, 0, mmp.PrimitiveCount * 3);
for (int i = 0; i != mmp.PrimitiveCount; ++i)
triangles.Add(new Triangle(a[indices[i * 3]], a[indices[i * 3 + 1]], a[indices[i * 3 + 2]]));
}
}
private void EMM(ModelMesh mm, ref Matrix xform,
List<Triangle> triangles)
{
foreach (ModelMeshPart mmp in mm.MeshParts)
EMMP(mm, mmp, ref xform, triangles);
}
public void ExtractModel(Model m)
{
transform = new Matrix[m.Bones.Count];
}
}
}
|