TGC Codebase Backup



Check if an object is infront of another by Chris Tate

25th May 2012 16:14
Summary

Check if an object is in front of another using collision followed by intersection check. (Or vise-versa if you wish)



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `%Project Title%
`CheckIfInFrontOfObject.dba
`======================
` Make Player
        PlayerObject = 1
        Make Object Cube PlayerObject, 3.0
        Color Object PlayerObject, Rgb(0, 255, 255)
        Set Object Emissive PlayerObject, Rgb(0, 255, 255)
        Position Object PlayerObject, -10,0,-10
        
` Make level object to collide into
        LevelObject = 2
        Make Object Cone LevelObject, 3.0
        Set Object Cull LevelObject, 0          ` Now we can see the back of the cone
        XRotate Object LevelObject, 90
        Fix Object Pivot LevelObject    ` Now the X angle is zero, but the cone mesh has been rotated
        Color Object LevelObject, Rgb(255, 0,0)
        Set Object Emissive LevelObject, Rgb(255, 0, 0)
        
` Make matrix guide
        Make Matrix 1, 40,40,10,10
        Position Matrix 1, -20, 0, -20

Position Camera 0,20,-20
Point Camera 0,0,0

Sync On : Sync Rate 60  

Do
        
        ` Make sure we can see the object
        If Object In Screen( PlayerObject ) = 0
                Point Camera Object Position X( PlayerObject ), Object Position Y( PlayerObject ), Object Position Z( PlayerObject )
        Endif
        
        ` Provide keyboard movement
        ControlObject( PlayerObject, UpKey() - Downkey(), RightKey() - LeftKey(), 0.25, 5.0)
        
        ` Rotate the level object slowly
        ControlObject( LevelObject, 0, 0.25, 0, 1.0)
        
        If ObjectInFrontOf( PlayerObject, LevelObject, 5 )
                Color Object LevelObject, Rgb(255,0,0)
                Set Object Emissive PlayerObject, Rgb(255,0, 0)
        Else
                Color Object LevelObject, Rgb(0,255,0)
                Set Object Emissive PlayerObject, Rgb(0, 255, 255)              
        Endif
        
        Text 0,0,"Collide in front of this cone to make it green"
        Sync
Loop

Function ControlObject( iObject, fForwardForce#, fClockwiseForce#, fMovementSpeed#, fRotateSpeed#)
        Move Object iObject, fForwardForce# * fMovementSpeed#
        Turn Object Right iObject, fClockwiseForce# * fRotateSpeed#
Endfunction

Function ObjectInFrontOf( iObject, iInFrontOfObject, fDistance# )
        
        ` Check if object is colliding before checking if it is in front
        If Object Collision( iObject, iInFrontOfObject )
                
                x1# = Object Position X( iInFrontOfObject )
                y1# = Object Position Y( iInFrontOfObject )
                z1# = Object Position Z( iInFrontOfObject )
                
                ` Cast a ray infront
                x2# = NewXValue( x1#, Object Angle Y( iInFrontOfObject ), fDistance# )
                y2# = y1#       ` In this scenario, the player cannot move up or down
                                ` Use this otherwise: Object Position Y( iObject )
                                                        
                                ` If you wanted to check if the object is pointing upwards towards the player
                                ` aswell, use this: NewYValue( y1#, Object Angle X( iInFrontOfObject ), fDistance# )
                z2# = NewZValue( z1#, Object Angle Y( iInFrontOfObject ), fDistance# )
                
                If Intersect Object( iObject, x1#, y1#, z1#, x2#, y2#, z2# ) > 0 Then ExitFunction 1
        Endif
Endfunction 0