User Tools

Site Tools


en:guides:beginners:collision_events

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
en:guides:beginners:collision_events [2018/02/14 00:47 (7 years ago)] – external edit 127.0.0.1en:guides:beginners:collision_events [2024/11/19 02:24 (4 months ago)] (current) – Highlights sausage
Line 3: Line 3:
 In order for the game to be won by the hero by collecting the star, we need to create a Physics Event Handler in code to check for, and to process a collision between those two objects. In order for the game to be won by the hero by collecting the star, we need to create a Physics Event Handler in code to check for, and to process a collision between those two objects.
  
-We can declare one at the bottom of the Init() function:+We can declare one at the bottom of the ''Init()'' function: 
 + 
 +<code=cpp [highlight_lines_extra="3"]> 
 +orxViewport_CreateFromConfig("MainViewport");
  
-<code=cpp> 
 orxEvent_AddHandler(orxEVENT_TYPE_PHYSICS, PhysicsEventHandler); orxEvent_AddHandler(orxEVENT_TYPE_PHYSICS, PhysicsEventHandler);
 </code> </code>
  
-What this means is, every time there is a physics event like a collision, execute the  PhysicsEventHandler function. We don't have one of these, so you can add one:+What this means is, every time there is a physics event like a collision, execute the  ''PhysicsEventHandler'' function. We don't have one of these, so you can add one:
  
 <code=cpp> <code=cpp>
Line 19: Line 21:
 </code> </code>
  
-The type of event we are interested in for collisions is the orxPHYSICS_EVENT_CONTACT_ADD.+The type of event we are interested in for collisions is the ''orxPHYSICS_EVENT_CONTACT_ADD''.
  
 Add this code inside the new function: Add this code inside the new function:
  
 <code=cpp> <code=cpp>
-if (_pstEvent->eID == orxPHYSICS_EVENT_CONTACT_ADD) {+orxSTATUS orxFASTCALL PhysicsEventHandler(const orxEVENT *_pstEvent) 
 +
 +  if (_pstEvent->eID == orxPHYSICS_EVENT_CONTACT_ADD) {
     orxOBJECT *pstRecipientObject, *pstSenderObject;     orxOBJECT *pstRecipientObject, *pstSenderObject;
  
Line 40: Line 44:
         //do something         //do something
     }     }
 +  }
 +  return orxSTATUS_SUCCESS;
 } }
 </code> </code>
Line 49: Line 55:
 Replace the "do something" lines with a command that will instantly remove the star from the game: Replace the "do something" lines with a command that will instantly remove the star from the game:
  
-<code=cpp>+<code=cpp [highlight_lines_extra="2,6"]>
 if (orxString_Compare(senderObjectName, "StarObject") == 0){ if (orxString_Compare(senderObjectName, "StarObject") == 0){
     orxObject_SetLifeTime(pstSenderObject, 0);     orxObject_SetLifeTime(pstSenderObject, 0);
Line 61: Line 67:
 The one last thing to add to ensure the collision between the star and the hero works, is to add "star" to the HeroBody check mask: The one last thing to add to ensure the collision between the star and the hero works, is to add "star" to the HeroBody check mask:
  
-<code=init>+<code=init [highlight_lines_extra="5"]>
 [HeroBodyPart] [HeroBodyPart]
 Type        = box Type        = box
en/guides/beginners/collision_events.1518598078.txt.gz · Last modified: 2018/02/14 00:47 (7 years ago) by 127.0.0.1