Collision Trigger
Blitz3D Forums/Blitz3D Programming/Collision Trigger
| ||
Is there a way to code a simple collision trigger with the following properties: You have an invisible cube entity (alpha = 0.0)and it is placed infront of a closed door. The player camera collides with this cube, but passes through it. In other words, there's no response other than the collision being registered. Then use 'EntityCollided()' in an 'If..Then' statement to trigger the door open event. Regards, Rogue Vector |
| ||
There are no no-response collisions in Blitz's built-in system but you can simulate them by attaching a child entity to your player entity, repositioning it at 0,0,0 each frame and checking collisions against THAT instead of the player entity (for door triggers and whatnot). The child will be displaced momentarily but the collision will register without affecting the player entity and, with the child invisible, the whole process will be transparent to the user. You don't want the camera itself able to collide with triggers. |
| ||
If the cube isn't rotated then you should be able to do a simple check to see if the player is near it, by basically a check to see if its x<max+r and >mix-r and y<may+r and >miy-r and z<maz+r and >miz-r . |
| ||
Yup yup... your "collision" routine could be as simple as an EntityDistance call. |
| ||
http://www.blitzbasic.com/codearcs/codearcs.php?code=792 You can use that to test whether a sphere interesects with a box. |
| ||
rogue vector, There is a way to do it. In fact I have developed a 'switch' system in Project PLASMA FPS 2K4 for the very purpose of activating events and actions. I will present some code taken directly out of the project. Then we can discuss it more detail. ;Interactive game object that trigger actions and events. Const SWITCH_MAX%=256 Dim switchId.switch(SWITCH_MAX%) <-- used for controlling switches by id ;the switch object Type switch Field id% ;used for tracking Field typeid% ;different switches for different purposes Field position.vector ;position of the switch Field scale.vector ;size of the switch Field key% ;stores key id for key-lock combo Field activation% ;used to manage various ways of activation Field activate% ;activate flag Field collision% ;collision flag Field Collisions% ;counts collisions Field state% ;stores state of switch Field clock.clock ;can be used to manage timed events Field action.action[4] ;event to activate (a huge if..then list) End TypeNow the follow function is called during the main loop to check to see if the player has activated the switch Function switchInteractive(this.switch) Select this\typeid% Case 1 ;trigger collision with player or bot For player.player=Each player ;** simple box collision ** If EntityX(player\pivotid%)>=this\position\x# And EntityX(player\pivotid%)<=this\scale\x# ;based on recalced scale If EntityY(player\pivotid%)>=this\position\y# And EntityY(player\pivotid%)<=this\scale\y# If EntityZ(player\pivotid%)>=this\position\z# And EntityZ(player\pivotid%)<=this\scale\z# collision%=True this\Collisions%=this\Collisions%+1 EndIf EndIf EndIf Next If collision% this\activate%=True Case 2 ;timer switch If this\clock\count%=0 this\activate=True End Select If this\activate% Select this\activation%;activation action Case -1 ;infinite actionCall(this\action[1]) Case 1 ;hitting If collision% actionCall(this\action[1]) Case 2 ;hitreset actionCall(this\action[1]) this\activation%=3 Case 3 ;stop If collision%=False this\activation%=this\activation%-1 this\activate%=False EndIf Case 4 ;hitonetime actionCall(this\action[1]) this\state%=0 Case 5 ;timerreset actionCall(this\action[1]) this\activate%=False clockReset(this\clock);set clock Case 6 ;timreretime actionCall(this\action[1]) this\state%=0 End Select End If End FunctionEssentially, the simple box collision does the trick for handling contact with the switch and it is very accurate. Now, I use a massive select...case to bind the switch to an action/event. See the Action System for more detail. If you have lot of switches this can be a good way to manage actions. |
| ||
I like this trigger for event code but I have a stupid question. Is the trigger an entity that has to be loaded or created alpha0? or is it simply a set of coordinates? Does that make sense? |
| ||
I just came across this code in the archives:; ID: 540 ; Author: Ken Lynch ; Date: 2003-01-07 05:31:30 ; Title: No Reponse Collisions ; Description: No response collisions. Graphics3D 800,600 light% = CreateLight() camera% = CreateCamera() pivot% = CreatePivot() EntityType pivot, 1 EntityRadius pivot, 0.1 sphere% = CreateSphere() ScaleEntity sphere, 0.1, 0.1, 0.1 EntityRadius sphere, 0.1 EntityType sphere, 2 EntityParent sphere, pivot PositionEntity camera, 0, 2, -2 PointEntity camera, pivot For i = 0 To 5 cube = CreateCube() EntityType cube, 3 PositionEntity cube, Rnd(-2, 2), 0, Rnd(-2, 2) ScaleEntity cube, 0.2, 0.2, 0.2 Next For i = 0 To 5 ball = CreateCylinder() EntityType ball, 4 PositionEntity ball, Rnd(-2, 2), 0, Rnd(-2, 2) ScaleEntity ball, 0.2, 0.2, 0.2 Next Collisions 2, 3, 2, 2 ; sphere , cube, ellipsoid-to-polygon collisions , slide1 - full sliding collision Collisions 1, 4, 2, 2 ; pivot , ball, ellipsoid-to-polygon collisions , slide1 - full sliding collision Repeat If KeyDown(200) Then TranslateEntity pivot, 0, 0, 0.1 If KeyDown(208) Then TranslateEntity pivot, 0, 0, -0.1 If KeyDown(203) Then TranslateEntity pivot, -0.1, 0, 0 If KeyDown(205) Then TranslateEntity pivot, 0.1, 0, 0 UpdateWorld PositionEntity sphere, 0, 0, 0 cyl_col% = cyl_col + CountCollisions(pivot) cube_col% = cube_col + CountCollisions(sphere) ResetEntity sphere RenderWorld Text 0, 0, "Cylinder Collisions: " + cyl_col Text 0, 16, "Cube Collisions: " + cube_col Flip Until KeyHit(1) Still, it would be good if Blitz3D implemented 'No Response Collisions'. It would make the triggering of events much more straight forward. I think I'll post something in the Feature Requests section. Like, Collisions() src_type - entity type to be checked for collisions. dest_type - entity type to be collided with. method - collision detection method. 1: ellipsoid-to-ellipsoid collisions 2: ellipsoid-to-polygon collisions 3: ellipsoid-to-box collisions response - what the source entity does when a collision occurs. 1: stop 2: slide1 - full sliding collision 3: slide2 - prevent entities from sliding down slopes 4: no response collision Regards, Rogue Vector |
| ||
Wait a minute... The Feature Request section has gone. Damn. |
| ||
Why don't you want the camera to collide with triggers??? If the entityalpha is 0 of the trigger... and you hideenity trigger as soon as it is touched... Ohhh... if your camera is moving too fast you will see a slight bump. -RZ |
| ||
Why don't you want the camera to collide with triggers???...Ohhh... if your camera is moving too fast you will see a slight bump. Not only that, the camera simply won't pass through the trigger (unless you are moving it too fast to detect the collision in the first place), which is almost certainly undesirable. It's a similar situation with water-collision. |