Standard demo (example) 11: Collision Animator
Sat Jan 21, 2023 8:05 am
How to make a collision check between two nodes? (One of simple ways)
- Code:
'' ----------------------------------------------------------------------------
'' Example made by Nikolas (WorldSim3D developer)
'' ----------------------------------------------------------------------------
'' Example 12 : Collision Animator
'' Example shows the work of the collision animator, as well as -
'' how to make a collision check between two nodes. In this example,
'' the camera and the map (the building we are moving through) interact with each other
'' as in the real physical world, so that the player controlling the camera could naturally
'' move around the building.
'' ----------------------------------------------------------------------------'
'#define __STATIC__
#Include "../Include/WorldSim3D.bi"
#include "../Include/SampleFunctions.bi"
'Variables
Dim as wMesh BSPMesh =0
Dim as wNode BSPNode =0
Dim as wNode Camera =0
Dim as wFont myFont =0
Dim as wSelector MapCollision =0
Dim as wAnimator AnimCollision =0
Dim as wFont BitmapFont =0
Dim as wVector2i fromPos =wVector2i_ZERO
Dim as wVector2i toPos =wVector2i_ZERO
Dim as wGuiObject scrollGravityY =0
Dim as wGuiObject checkCollision =0
'To switch the FPS camera/normal camera mode
Dim as Boolean isControl = false
'This will be useful for the jumping camera
Dim as wKeyCode key_JUMP =wKC_SPACE
'Maximum separation from the "earth"
Dim as Float32 jumpMax =150.0f
'Jump speed
Dim as Float32 jumpForce =1.5f
'To remember the starting position at the time of the jump
Dim as wVector3f curCameraPosition =wVector3f_ZERO
'Flag for checking pressing key_JUMP
Dim as Boolean isJumping =false
Dim as Boolean isHelp =false
Dim as String wndCaption ="Example 12: Collision "
Dim as Int32 prevFPS =0
'Start engine
Dim as Boolean init = wEngineStart(wDRT_OPENGL,wDEFAULT_SCREENSIZE,32,false,true,true,false)
IF Not init Then
PrintWithColor("wEngineStart() failed!",wCFC_RED,true)
End
End If
'Show logo WS3D
wEngineShowLogo(true)
Dim as string resPath="../../Assets/BSPmaps/ctfcomp02_hazard.pk3"
Dim as string fontPath="../../Assets/Fonts/Roboto_12.xml"
'Check resources
CheckFilePath(resPath)
CheckFilePath(fontPath)
'Load resources
wFileAddZipArchive(resPath,true,false)
BSPMesh = wMeshLoad( "maps/ctfcomp02_hazard.bsp" )
myFont=wFontLoad(fontPath)
'Create node
BSPNode = wNodeCreateFromMeshAsOctree(BSPMesh)
'Create camera
Camera=wFpsCameraCreate(100,_
0.1f,_
@wKeyMapDefault(1),_
Dim as wVector3f vec=(386,366,2726)
wNodeSetPosition(Camera,vec)
vec.x=-534 : vec.y=299 : vec.z=5345
wCameraSetTarget(Camera,vec)
wFpsCameraSetVerticalMovement(Camera,false)
wFpsCameraSetSpeed(Camera,0.2f)
'Create collision
MapCollision=wCollisionCreateFromOctreeMesh(BSPMesh,BSPNode)
'Create collision animator
AnimCollision=wAnimatorCollisionResponseCreate(MapCollision,Camera)
'Get & Set collision animator parameters
Dim as wAnimatorCollisionResponse params
wAnimatorCollisionResponseGetParameters(AnimCollision,@params)
params.gravity.y=-80.0f
params.ellipsoidRadius.x=15
params.ellipsoidRadius.y=50
params.ellipsoidRadius.z=15
params.ellipsoidTranslation.x=0
params.ellipsoidTranslation.y=50/2
params.ellipsoidTranslation.z=0
wAnimatorCollisionResponseSetParameters(AnimCollision,params)
'Create scroll For set gravityY
fromPos.x=800-40 : fromPos.y=400 : toPos.x=800-30: toPos.y=550
scrollGravityY=wGuiScrollBarCreate(false,fromPos,toPos)
wGuiScrollBarSetMinValue(scrollGravityY,-100)
wGuiScrollBarSetMaxValue(scrollGravityY,100)
wGuiScrollBarSetValue(scrollGravityY,80)
wGuiScrollBarSetLargeStep(scrollGravityY,1)
wGuiScrollBarSetSmallStep(scrollGravityY,1)
'Create checkBox For collision mode set
fromPos.x=580 : fromPos.y=550 : toPos.x=780: toPos.y=600
checkCollision=wGuiCheckBoxCreate("Check collision",fromPos,toPos,true)
Dim as wGuiObject skin=wGuiGetSkin()
wGuiSkinSetFont(skin,myFont)
wGuiSkinSetColor(skin,wGDC_BUTTON_TEXT,wCOLOR4s_WHITE)
'Hide mouse cursor
wInputSetCursorVisible(false)
Dim As wTexture frame =0
Dim as String tex_Path= "../../Assets/Sprites/blue_frame.png"
CheckFilePath(tex_Path)
frame = wTextureLoad(tex_Path)
while wEngineRunning()
wSceneBegin(wCOLOR4s_WHITE)
wSceneDrawAll()
fromPos.x=260: fromPos.y=370
toPos.x=480: toPos.y=470
wTextureDraw(frame,fromPos,true, wCOLOR4s_WHITE)
'Draw scrollbar and checkBox
wGuiObjectDraw(scrollGravityY)
wGuiObjectDraw(checkCollision)
'Reading the animator parameters
wAnimatorCollisionResponseGetParameters(AnimCollision,@params)
'Draw collision triangle
'Slightly raise/lower the collision triangle for better visibility
Dim as Float32 dY
IF params.gravity.y<0 Then
dY=0.5f
Else
dY=-0.5f
End If
params.collisionTriangle.pointA.y+=dY
params.collisionTriangle.pointB.y+=dY
params.collisionTriangle.pointC.y+=dY
w3dDrawTriangle(params.collisionTriangle,wCOLOR4s_DARKRED)
'Draw text info
fromPos.x=450 : fromPos.y=400 : toPos.x=600 : toPos.y=420
IF params.isFalling Then
wFontDraw(myFont,"Camera is falling: TRUE",fromPos,toPos)
Else
wFontDraw(myFont,"Camera is falling: FALSE",fromPos,toPos)
End If
fromPos.y+=30 : toPos.y+=30
IF params.animateTarget Then
wFontDraw(myFont,"Collision: ON",fromPos,toPos)
Else
wFontDraw(myFont,"Collision: OFF",fromPos,toPos)
End If
fromPos.x-=30 : fromPos.y+=30 : toPos.x=800 : toPos.y=500
wFontDraw(myFont,"Gravity.Y : "+str(-wGuiScrollBarGetValue(scrollGravityY)),fromPos,toPos)
'We output hints
IF Not isHelp Then
fromPos.x=10 : fromPos.y=10 : toPos.x=500 : toPos.y=40
wFontDraw(myFont,"Press F1 to Help",fromPos,toPos,wCOLOR4s_GREEN)
Else
fromPos.x=10 : fromPos.y=10 : toPos.x=500 : toPos.y=40
wFontDraw(myFont,"Movement: (W,A,S,D) or (Up,Left,Down,Right)",fromPos,toPos,wCOLOR4s_GREEN)
fromPos.y+=20 : toPos.y+=20
wFontDraw(myFont,"Jumping: SPACE (IF default value)",fromPos,toPos,wCOLOR4s_GREEN)
fromPos.y+=20 : toPos.y+=20
wFontDraw(myFont,"Exit: ESCAPE ",fromPos,toPos,wCOLOR4s_GREEN)
'We display the hint at the bottom of the screen
'with horizontal centering
Dim as string text="Press Right Mouse Button to set parameters"
fromPos.x = 1024/2 - wFontGetTextSize (myFont, text).x/2
fromPos.y=550
toPos.x=1024/2+wFontGetTextSize(myFont,text).x/2
toPos.y=570
wFontDraw(myFont,text,fromPos,toPos,wCOLOR4s_GREEN)
End If
'GUI (scrollBar and checkBox) events
IF wGuiIsEventAvailable() Then
Dim as wGuiCallerType t=wGuiReadEvent()->event
IF t=wGCT_SCROLL_BAR_CHANGED Then
params.gravity.y=-Cast(Float32,wGuiScrollBarGetValue(scrollGravityY))
wAnimatorCollisionResponseSetParameters(AnimCollision,params)
End If
IF t=wGCT_CHECKBOX_CHANGED Then
Dim as Boolean check=wGuiCheckBoxIsChecked(checkCollision)
params.animateTarget=check
wAnimatorCollisionResponseSetParameters(AnimCollision,params)
'If the checkbox is removed, we immediately switch to automatic
'review mode
IF Not check Then
wInputSetCursorVisible(false)
wCameraSetInputEnabled(Camera,true)
isControl=false
End If
End If
End If
'Make a jump
'by pressing KEY_JUMP (by default wKC_SPACE)
'All this will be available only in FPS camera mode
IF Not isControl Then
IF wInputIsKeyHit(key_JUMP) and (Not params.isFalling) Then
' Just in case, we will remove the focus from GUI objects
wGuiObjectRemoveFocus(scrollGravityY)
wGuiObjectRemoveFocus(checkCollision)
'We remember the current position of the camera
curCameraPosition=wNodeGetPosition(Camera)
isJumping=true
End If
'If the jump is activated, move the camera up
'taking into account the parameters of the jump and delta timing
IF isJumping Then
Dim as wVector3f position=wNodeGetPosition(Camera)
IF position.y<=curCameraPosition.y+jumpMax Then
position.y+=jumpForce*(-params.gravity.y)*wTimerGetDelta()
wNodeSetPosition(Camera,position)
Else
isJumping=false
End If
End If
'If the camera is on the ground and the jump key is not pressed,
'reset the jump flag
IF Not params.isFalling and (Not wInputIsKeyHit(key_JUMP)) Then isJumping=false
End If
'Keys events
'For Help
IF wInputIsKeyHit(wKC_F1) Then
isHelp= Not isHelp
End If
'Mouse events
'For control mode
IF wInputIsMouseHit(wMB_RIGHT) Then
'Just in case, we will remove the focus from GUI objects
wGuiObjectRemoveFocus(scrollGravityY)
wGuiObjectRemoveFocus(checkCollision)
isControl= Not isControl
wInputSetCursorVisible(isControl)
wCameraSetInputEnabled(Camera, Not isControl)
End If
'In the mode of no collision with the scene
'we allow the camera to move vertically
wFpsCameraSetVerticalMovement(Camera,Not params.animateTarget)
'If there's no "manual" control
'just in case, we remove the focus from GUI objects
'and draw something like a target
IF Not isControl Then
wGuiObjectRemoveFocus(scrollGravityY)
wGuiObjectRemoveFocus(checkCollision)
fromPos.x=800/2-4 : fromPos.y=600/2-4
w2dDrawPolygon(fromPos,8,wCOLOR4s_GREEN,12)
w2dDrawPixel(fromPos,wCOLOR4s_GREEN)
End If
wSceneEnd()
'Close with ESC
wEngineCloseEsc()
'Update FPS
IF prevFPS <> wEngineGetFPS() Then
prevFPS = wEngineGetFPS()
wWindowSetCaption(wndCaption+ "(FPS = "+str(prevFPS)+")")
End If
wend
'Stop engine
wEngineStop()
End
Permissions in this forum:
You cannot reply to topics in this forum