' ------------------------------------------------------------
' n3xt-D exemples
'
' Sample 112 : Test body with bridge
' 23/08/09 19:16 TMyke
'
' ------------------------------------------------------------
' Include files
#INCLUDE "n3xtD.bi"
DECLARE FUNCTION CreateRope(BYVAL posx AS SINGLE, BYVAL posy AS SINGLE, BYVAL posz AS SINGLE) AS LONG
DECLARE SUB JointCallBack CDECL(BYVAL joint AS NJoint, BYVAL desc AS NewtonHingeSliderUpdateDesc)
FUNCTION PBMAIN
'; Globales
DIM app AS GLOBAL LONG PTR
DIM Quit AS GLOBAL LONG
DIM cylT AS GLOBAL EntityNode
'----------------------------------------------------------
' open n3xt-D screen
app = CreateGraphics3D(800, 600, 32, 0, %TRUE, %DEVICE_TYPES.EDT_DIRECT3D9, %TRUE)
IF app = %Null THEN
EXIT FUNCTION
END IF
' create and load mesh
DIM cubemesh AS GLOBAL Mesh
cubemesh = CreateCubeMesh(1.0)
'-----------------------------------------
' create a static base
DIM floor AS EntityNode
floor = CreateEntitynode(cubemesh, 0, 0, 0, %NULL)
PositionNode(floor, 0,0,0)
LoadTextureNode(floor, "media/grille1.bmp", 0, 0)
ScaleNode(floor, 20,1,20)
' create body, no dynamique
CreateBody(floor, %BOX_PRIMITIVE, %False, 1.0, 0, 0, 0)
' link all joint with the plateform base static
DIM downNode1 AS EntityNode
downNode1 = CreateRope( -7,-2.0,-7)
CreateBallJoint( cylT, floor, -7, -0.5 , -7)
DIM downNode2 AS DWORD
downNode2 = CreateRope( -7,-2.0,7)
CreateBallJoint(cylT, floor, -7, -0.5 , 7)
DIM downNode3 AS DWORD
downNode3 = CreateRope( 7,-2.0,0)
CreateBallJoint(cylT, floor, 7, -0.5 , 0)
'-----------------------------------------
' create plateforme
DIM cube AS EntityNode
cube = CreateEntitynode(cubemesh, 0, 0, 0, BYVAL %NULL)
PositionNode(cube, 0,-14,0)
LoadTextureNode(cube, "media/metal13.jpg", 0, 0)
ScaleNode(cube, 20,1,20)
' create body
CreateBody(cube, %BOX_PRIMITIVE, %True, 1.0, 0, 0, 0)
' create 3 link for the plateform
CreateBallJoint(cube, downNode1, -7, -13.5 , -7)
CreateBallJoint(cube, downNode2, -7, -13.5 , 7)
DIM joint AS NJoint
joint = CreateBallJoint(cube, downNode3, 7, -13.5 , 0)
' juste for one link.
UserCallbackBallJoint(joint, BYVAL CODEPTR(JointCallBack()))
'-----------------------------------------
' create first camera
DIM cam AS GLOBAL CameraNode
cam = CreateCameraFPS(70,0.1, 1.0, BYVAL %NULL, 0, %False, %False, BYVAL %NULL)
PositionNode(cam, 0,-5,-30)
RotateNode(cam, -10,0,0)
'-----------------------------------
' load font png
LoadFont("media/courriernew.png", %FONT_TYPE.EGDF_DEFAULT)
DIM pbfont AS IGUIFont
pbfont = GetFont()
DIM camPos AS GLOBAL iVECTOR3
DIM camDir AS GLOBAL iVECTOR3
' ---------------------------------------
' main loop
' ---------------------------------------
WHILE Quit = 0
'set a timer, a bit slower for the physics engine.
'_TimerUpdatePhysic(100.0)
' shoot with cube
IF GetKeyUp(%KEY_CODE.KEY_KEY_O) THEN
NodePosition(cam, camPos.x)
NodeDirection(cam, camDir.x)
' create mesh to shoot
'dim cube as EntityNode
cube = CreateEntitynode(cubemesh, 0, 0, 0, BYVAL %NULL)
ScaleNode(cube, 2,2,2)
LoadTextureNode(cube, "media/wcrate.bmp", 0, 0)
PositionNode(cube, camPos.x, camPos.y, camPos.z)
RotateNode(cube, RND(0, 180), RND(0, 180), RND(0, 180))
CreateBody(cube, %BOX_PRIMITIVE, %True, 10.0, 0, 0, 0)
VelocityBody(cube, camDir.x * 20, camDir.y *20, camDir.z * 20)
END IF
' shoot with cube
IF GetKeyUp(%KEY_CODE.KEY_KEY_P) THEN
NodePosition(cam, camPos.x)
NodeDirection(cam, camDir.x)
' create mesh to shoot
'dim cube as EntityNode
cube = CreateEntitynode(cubemesh, 0, 0, 0, BYVAL %NULL)
ScaleNode(cube, 2,2,2)
LoadTextureNode(cube, "media/wcrate.bmp", 0, 0)
PositionNode(cube, camPos.x,camPos.y,camPos.z)
RotateNode(cube, RND(0, 180), RND(0, 180), RND(0, 180))
' create body
CreateBody(cube, %BOX_PRIMITIVE, %True, 1.0, 0, 0, 0)
VelocityBody(cube, camDir.x*20, camDir.y*20, camDir.z*20)
END IF
' if Escape Key, exit
IF GetKeyDown(%KEY_CODE.KEY_ESCAPE) THEN
Quit = 1
END IF
' ---------------
' Render
' ---------------
BeginScene(0, 0, 0, 255, 1, 1)
DrawScene()
NX3_DrawText(pbfont, "Use P Key for shoot light Cube", 10,10,0,0, &h0ff9999ff)
NX3_DrawText(pbfont, "Use O Key for shoot heavy Cube", 10,25,0,0, &h0ff9999ff)
EndScene()
WEND
' end
FreeEngine()
END FUNCTION
'--------------------------------------------------------------------
' create a rope with 4 Ball Joint
FUNCTION CreateRope (BYVAL posx AS SINGLE, BYVAL posy AS SINGLE, BYVAL posz AS SINGLE) AS LONG
LOCAL i AS LONG
DIM cylindermesh AS Mesh
cylindermesh = CreateCylinderMesh(0.3, 3.0, &h0ffffffff, 8, %True, 0)
' create first joint
DIM cyl1 AS EntityNode
cyl1 = CreateEntityNode(cylindermesh, 0, 0, 0, BYVAL %NULL)
PositionNode(cyl1, posx, posy, posz)
LoadTextureNode(cyl1, "media/water.jpg", 0, 0)
' create body
CreateBody(cyl1, %CAPSULE_PRIMITIVE, %True, 1.0, 0, 0, 0)
cylT= cyl1
' link 3 other joint
FOR i = 0 TO 2
DIM cyl2 AS EntityNode
cyl2 = CreateEntityNode(cylindermesh, 0, 0, 0, BYVAL %NULL)
PositionNode(cyl2, posx, posy - (i+1)*3.2, posz)
LoadTextureNode(cyl2, "media/water.jpg", 0, 0)
' create body
CreateBody(cyl2, %CAPSULE_PRIMITIVE, %True, 1.0, 0, 0, 0)
CreateBallJoint(cyl2, cylT, posx, posy - (i+1)*2.6, posz)
cylT = cyl2
NEXT
cylT = cyl1
FUNCTION = cyl2
END FUNCTION
';------------------------------------------------------------------------------------------------
'; CallBack Joint. This function call when either of the two bodies linked by the joint is active.
SUB JointCallBack CDECL(BYVAL joint AS NJoint, BYVAL desc AS NewtonHingeSliderUpdateDesc)
DIM force(2) AS LOCAL SINGLE
BallJointForce(joint, force(0))
IF force(1) > 650 THEN
' debug vertical joint force
'Debug force(1)
FreeJoint(joint)
END IF
END SUB
New Include