I've been playing around with this a few days and I have to say, great job. However, I tried collision detection without any success. I then partially converted example 7 (c# collision - in Irrlicht 1.1) to see if something was wrong in my code. The new example 7 didn't work either.
Code: Select all
Vector3D intersection;
Triangle3D tri;
if (smgr.CollisionManager.GetCollisionPoint(
line,selector,out intersection, out tri))
{
Console.WriteLine("intersection : " + intersection);
Console.WriteLine("tri A: " + tri.PointA);
Console.WriteLine("tri B: " + tri.PointB);
Console.WriteLine("tri C: " + tri.PointC);
counter++;
bill.Position=intersection;
tracer2.Position = intersection;
//driver.SetTransform(TransformationState.WORLD, new Matrix4());
//driver.SetMaterial(material);
driver.Draw3DTriangle(tri,new Color(0,255,0,0));
}
The code went into the if statement but the intersections and triangle vectors were all still (0,0,0).
A portion of the output:
Code: Select all
intersection : "Type = IrrlichtNETCP.Vector3D; X = 0; Y = 0; Z = 0"
tri A: "Type = IrrlichtNETCP.Vector3D; X = 0; Y = 0; Z = 0"
tri B: "Type = IrrlichtNETCP.Vector3D; X = 0; Y = 0; Z = 0"
tri C: "Type = IrrlichtNETCP.Vector3D; X = 0; Y = 0; Z = 0"
I don't know if there is something with my code or the wrapper so here is the code I used:
Code: Select all
using System;
using System.Text;
using System.IO;
using IrrlichtNETCP;
namespace _07._Collisions
{
class Program
{
int counter;
string path="";
IrrlichtDevice device;
/// <summary>
/// Main entry point for the program.
/// </summary>
/// <param name="args">Arguments to pass the software.</param>
[STAThread]
static void Main(string[] args)
{
Program prog = new Program();
prog.run();
}
public void run()
{
DriverType driverType;
StringBuilder sb = new StringBuilder();
sb.Append("Please select the driver you want for this example:\n");
sb.Append("\n(a) Direct3D 9.0c\n(b) Direct3D 8.1\n(c) OpenGL 1.5");
sb.Append("\n(d) Software Renderer\n(e) Apfelbaum Software Renderer");
sb.Append("\n(f) Null Device\n(otherKey) exit\n\n");
driverType = DriverType.Direct3D9;
// Create device and exit if creation fails:
device = new IrrlichtDevice(driverType, new Dimension2D(800, 600), 32, false, true, false,false);
if (device == null)
{
//tOut.Write("Device creation failed.");
return;
}
SceneManager smgr=device.SceneManager;
VideoDriver driver=device.VideoDriver;
device.FileSystem.AddZipFileArchive(path+"map-20kdm2.pk3",true,true);
AnimatedMesh q3levelmesh = smgr.GetMesh("20kdm2.bsp");
SceneNode q3node = null;
if (q3levelmesh!=null)
q3node=smgr.AddOctTreeSceneNode(q3levelmesh.GetMesh(0),null,0,128);
TriangleSelector selector = null;
if (q3node!=null)
{
q3node.Position= new Vector3D(-1370,-130,-1400);
selector=smgr.CreateOctTreeTriangleSelector(
q3levelmesh.GetMesh(0),q3node,128);
// not implemented but not necessary
//q3node.TriangleSelector=selector;
}
CameraSceneNode camera = smgr.AddCameraSceneNodeFPS(null,100,300,false);
camera.Position=new Vector3D(-100,50,-150);
Animator anim = smgr.CreateCollisionResponseAnimator(
selector,camera,new Vector3D(30,50,30),
new Vector3D(0,-3,0),new Vector3D(0,50,0),0);
camera.AddAnimator(anim);
device.CursorControl.Visible=false;
// add billboard
BillboardSceneNode bill = smgr.AddBillboardSceneNode(
null,new Dimension2Df(20,20),0);
bill.SetMaterialType(MaterialType.TransparentAddColor);
bill.SetMaterialTexture(0,driver.GetTexture(
path+"particle.bmp"));
bill.SetMaterialFlag(MaterialFlag.Lighting,false);
bill.SetMaterialFlag(MaterialFlag.ZBuffer,false);
Material material = new Material();
material.Texture1= driver.GetTexture(
path+"faerie2.bmp");
material.Lighting=true;
/*AnimatedMeshSceneNode node = null;
AnimatedMesh faerie = smgr.GetMesh(
path+"faerie.md2");
if (faerie!=null)
{
node=smgr.AddAnimatedMeshSceneNode(faerie,null,0);
node.Position=new Vector3D(-70,0,-90);
node.SetMD2Animation(MD2AnimationType.RUN);
node.SetMaterial(0,material);
node=smgr.AddAnimatedMeshSceneNode(faerie,null,0);
node.Position=new Vector3D(-70,0,-30);
node.SetMD2Animation(MD2AnimationType.SALUTE);
node.SetMaterial(0,material);
node=smgr.AddAnimatedMeshSceneNode(faerie,null,0);
node.Position=new Vector3D(-70,0,-60);
node.SetMD2Animation(MD2AnimationType.JUMP);
node.SetMaterial(0,material);
}
material.Texture1=null;
material.Lighting=false; */
//Add a light
smgr.AddLightSceneNode(null,new Vector3D(-60,100,400),
new Colorf(1.0f,1.0f,1.0f,1.0f),600,0);
/*For not making it too complicated, I'm doing picking inside the drawing
loop. We take two pointers for storing the current and the last selected
scene node and start the loop.*/
//SceneNode selectedSceneNode =null;
//SceneNode lastSelectedSceneNode =null;
SceneNode tracer1 = smgr.AddCubeSceneNode(20, null, 0);
SceneNode tracer2 = smgr.AddCubeSceneNode(20, null, 0);
int lastFPS=-1;
counter = 0;
while (device.Run())
{
if (device.WindowActive)
{
device.VideoDriver.BeginScene(true, true, new Color(0, 200, 200, 200));
device.SceneManager.DrawAll();
Line3D line = new Line3D();
line.Start=camera.Position;
line.End=line.Start+
(camera.Target - line.Start).Normalize() * 1000;
tracer1.Position = line.Start;
//Console.WriteLine("here");
driver.Draw3DLine(line, new Color(0, 255, 0, 0));
Vector3D intersection;
Triangle3D tri;
if (smgr.CollisionManager.GetCollisionPoint(
line,selector,out intersection, out tri))
{
Console.WriteLine("intersection : " + intersection);
counter++;
bill.Position=intersection;
tracer2.Position = intersection;
//driver.SetTransform(TransformationState.WORLD, new Matrix4());
//driver.SetMaterial(material);
driver.Draw3DTriangle(tri,new Color(0,255,0,0));
}
/*selectedSceneNode=smgr.CollisionManager.
GetSceneNodeFromCameraBB(camera,0);
if (lastSelectedSceneNode!=null)
lastSelectedSceneNode.SetMaterialFlag(
MaterialFlag.Lighting, true);
if (selectedSceneNode==q3node||
selectedSceneNode==bill)
selectedSceneNode=null;
if(selectedSceneNode!=null)
selectedSceneNode.SetMaterialFlag(
MaterialFlag.Lighting,false);
lastSelectedSceneNode=selectedSceneNode; */
/*That's it, we just have to finish drawing.*/
driver.EndScene();
int fps = device.VideoDriver.FPS;
if (lastFPS != fps)
{
device.WindowCaption = "Irrlicht Engine - Quake 3 Map example [" +
device.VideoDriver.ToString() + "] FPS:" + fps.ToString();
lastFPS = fps;
}
}
}
/*
In the end, delete the Irrlicht device.
*/
// Instead of device->drop, we'll use:
GC.Collect();
}
}
}
// Retrieved from "http://www.irrforge.org/index.php/CS_Tutorial_7"
// This page has been accessed 220 times. This page was last modified 06:51, 20 Jan 2006.
Sorry the post is so long. Any solutions for this collision problem?