Topic: Collision
Here is my code so far:
Strict
Framework Irrlicht.Core
Local receiver:IEventReceiver=IEventReceiver.Create(MyEventReceiver.generate)
Global device:IrrlichtDevice = ..
IrrlichtDevice.Create(EDT_DIRECT3D9, _DIMENSION2DI(640, 480), 16, ..
False, False, False, receiver)
Local driver:IVideoDriver = device.getVideoDriver()
Local smgr:ISceneManager = device.getSceneManager()
Local guienv:IGUIEnvironment = device.getGUIEnvironment()
Rem
We add a hello world label To the window, using the GUI environment.
EndRem
Local s:Iguistatictext=guienv.addStaticText("Hello World! This is the Irrlicht renderer!", ..
_RECTI(10,10,260,22),True)
device.setWindowCaption("FPS")
Local white:scolor=_scolor(255,255,255,255)
s.setBackgroundColor(white)
Local mesh:IMesh = smgr.getMesh("ship_models/11.b3d")
's.SetoverrideColor(white)
Type ship
Field node:imeshscenenode
Global list:TList=New TList
Field selector:Itriangleselector
EndType
For Local x=1 To 50
Local s:ship=New ship
s.node=smgr.addmeshscenenode(mesh)
ship.list.addlast(s)
Next
'Global node:ImeshSceneNode = smgr.addmeshSceneNode(mesh)
'Global node2:Imeshscenenode=smgr.addmeshscenenode(mesh)
For Local s:ship=EachIn ship.list
s.selector = smgr.createTriangleSelector(Mesh, s.node)
s.node.setTriangleSelector(s.selector)
s.selector.drop()
Next
Local num=0
For Local s:Ship=EachIn ship.list
s.node.setposition(_vector3df(0,num,0))
num=num+10
Local anim:ISceneNodeAnimator = smgr.createCollisionResponseAnimator( s.selector, s.node, _VECTOR3DF(1.5,1,1.5),_VECTOR3DF(0,0,0),_VECTOR3DF(0,0,0))
s.node.addAnimator(anim)
anim.drop()
Next
Global globnode:Ilightscenenode=smgr.addlightscenenode()
Local ship_tex:itexture=driver.gettexture("ship_models\blueship-low.png")
For Local s:ship=EachIn ship.list
If (s.node)
s.node.setMaterialFlag(EMF_LIGHTING, False)
'node.setMD2Animation ( EMAT_STAND )
s.node.setMaterialTexture( 0,ship_tex )
EndIf
Next
Global prev_mouse_X:Int=-1
Global prev_mouse_y:Int=-1
Type MyEventReceiver Extends IEventReceiver
Method OnEvent:Int(event:SEvent)
Local x:Int=event.getmousex()
Local y:Int=event.getmousey()
If event.geteventtype()= EET_MOUSE_INPUT_EVENT
If x<640 And y<480
If prev_mouse_x>-1
Local new_x=prev_mouse_x-x
Local new_y=prev_mouse_y-y
For Local S:ship=EachIn ship.list
nodeturn(s.node,new_y,-new_x,0)
Exit
Next
EndIf
prev_mouse_x=x
prev_mouse_y=y
EndIf
EndIf
For Local s:ship=EachIn ship.list
If (s.node And event.getEventType()=EET_KEY_INPUT_EVENT And ..
Not event.getKeyPressedDown())
Local key:Int=event.getKeyInputKey()
Select key
Case EKEY_KEY_W
Local v:Vector3df=s.node.GetRotation()
v.setY(v.getY()+20.0)
s.node.SetRotation(v)
Return True
Case EKEY_KEY_S
Local v:Vector3df=s.node.GetRotation()
v.setY(v.getY()-20.0)
s.node.SetRotation(v)
Return True
Default
Return False
EndSelect
Exit
EndIf
Next
Return False
EndMethod
' we must override the generate function in order for instantiation to work properly
Function generate:IEventReceiver()
Return New MyEventReceiver
EndFunction
EndType
Global camera:Icamerascenenode=smgr.addCameraSceneNode(Null, _VECTOR3DF(0,30,-40), _VECTOR3DF(0,5,0))
'Camera.bindTargetAndRotation(True)
'For Local s:ship=EachIn ship.list
'camera.setParent(s.node)
'Exit
'Next
'--- set camera To behave as cockpit camera of ship ---
Function makeCockpit(camera:ICameraSceneNode,node:ISceneNode, offset:vector3df) '//relative position of camera To node (ship)
Local m:matrix4=matrix4.Create()
m.setRotationDegrees(node.GetRotation())
Local frv:vector3df = _vector3df (0.0, 0.0, 1.0)
m.transformVect(frv)
Local upv:vector3df =_vector3df (0.0, 1.0, 0.0)
m.transformVect(upv)
m.transformVect(offset)
camera.setPosition((node.getPosition()).plus(offset))'; //position camera in front of the ship
camera.setUpVector(upv)'; //set up vector of camera
camera.setTarget((node.getPosition()).plus( frv))' //set target of camera (look at point) (thx Zeuss For correcting it)
' camera.updateAbsolutePosition()
EndFunction
Local i#
'node.SetRotation(_vector3df(0,90,0))
While(device.run())
driver.beginScene(True, True, _SCOLOR(0,0,0,0))
For Local S:ship=EachIn ship.list
s.node.setposition(TFormPoint(0,0,.1,s.node,globnode))
makecockpit(camera,s.node,_vector3df(0,2,-5))
'Local nodepos:vector3df=TFormPoint(0,0,10,s.node,globnode)
'Local noderot:vector3df=s.node.GetRotation()
'camera.setposition(_vector3df(0,3,-4))
'camera.SetRotation(noderot)
Exit
Next
device.setWindowCaption("FPS: "+driver.getfps())
smgr.drawAll()
guienv.drawAll()
driver.endScene()
Wend
device.drop()
Function TFormPoint:Vector3df(x:Float,y:Float,z:Float,source:ISceneNode,dest:ISceneNode)
Local lastTForm:Vector3df=_VECTOR3DF(x,y,z)
' first convert to world from the source
If source Then source.getAbsoluteTransformation().transformVect(lastTForm)
' now from world to local
If dest Then dest.getRelativeTransformation().transformVect(lastTForm)
Return lastTForm
EndFunction
Function NodeMove(node:ISceneNode,x:Float,y:Float,z:Float)
' from: http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=4680
Local dest:Vector3df=Vector3df.createFromVals(x,y,z)
node.getRelativeTransformation().transformVect(dest)
node.setPosition(dest)
EndFunction
Function NodeTurn(node:ISceneNode,pitch:Float,yaw:Float,roll:Float)
' from: http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=4680
Local m:Matrix4=Matrix4.Create()
Local n:Matrix4=Matrix4.Create()
Local t:Matrix4=Matrix4.Create()
m.setRotationDegrees(node.GetRotation())
n.setRotationDegrees(_VECTOR3DF(0,0,roll))
t.setRotationDegrees(_VECTOR3DF(pitch,0,0))
n.MultEq(t)
t.setRotationDegrees(_VECTOR3DF(0,yaw,0))
n.MultEq(t)
m.MultEq(n)
node.SetRotation(m.getRotationDegrees())
EndFunction
Function NodePointTo:Vector3df(node:ISceneNode, x:Float, y:Float, z:Float)
Local direction:Vector3df = _VECTOR3DF(x,y,z).Minus(node.getPosition())
node.SetRotation(direction.getHorizontalAngle())
EndFunction
collision doesnt work as expected, it seems ok on the quake level demo that comes with irrlicht, but here there is weird behaviour such as being able to go throw a mesh if you wiggle the mouse and sometimes the collisions are made no-where near the ships.