Object not deleted when handled from collision event

This discussion was created from comments split from: Tutorial Platform not appearing.

Comments

  • I'm having another issue, with the star collision. My guy is colliding with the star, but its not disappearing, I'm assuming the collision event isnt working.

    Heres the function in the main cpp file.

    // Physics Collision event
    orxSTATUS orxFASTCALL PhysicsEventHandler(const orxEVENT *_pstEvent)
    {
        if (_pstEvent->eID == orxPHYSICS_EVENT_CONTACT_ADD) {
            orxOBJECT *pstRecipientObject, *pstSenderObject;
    
            pstSenderObject = orxOBJECT(_pstEvent->hSender);
            pstRecipientObject = orxOBJECT(_pstEvent->hRecipient);
    
            orxSTRING senderObjectName = (orxSTRING)orxObject_GetName(pstSenderObject);
            orxSTRING recipientObjectName = (orxSTRING)orxObject_GetName(pstRecipientObject);
    
            if (orxString_Compare(senderObjectName, "StarObject") == 0){
                orxObject_SetLifeTime(pstSenderObject, 0);
            }
    
            if (orxString_Compare(recipientObjectName, "StarObject") == 0){
                orxObject_SetLifeTime(pstRecipientObject, 0);
            }
        }
        return orxSTATUS_SUCCESS;
    }
    

    Then my init section:

    orxSTATUS orxFASTCALL Init()
    {
        // Display a small hint in console
        orxLOG("\n* output on start");
    
        // Create the viewport
        orxViewport_CreateFromConfig("Viewport");
    
        // Create hero and gun
        hero = orxObject_CreateFromConfig("HeroObject");
        herosGun = (orxOBJECT*)orxObject_GetChild(hero);
        orxObject_Enable(herosGun, orxFALSE);
    
        // Create platform
        orxObject_CreateFromConfig("Scene");
    
        // Event Handler Collision
        orxEvent_AddHandler(orxEVENT_TYPE_PHYSICS, PhysicsEventHandler);
    
        // Done!
        return orxSTATUS_SUCCESS;
    }
    
  • Hey @sacked, I've split this question from the original thread as it wasn't related to the original issue and didn't match the title anymore.

    I can't see any obvious issue with what you posted. Could you post the content of your config file as well?

  • Yes I would guess it's the config for StarObject and HeroObject not having their masks set correctly.

  • /**
     * @file spookyparty.cpp
     * @date 11-Jan-2020
     */
    
    #include "orx.h"
    using namespace std;
    
    /*
     * This is a basic code template to quickly and easily get started with a project or tutorial.
     */
    orxOBJECT *hero;
    orxOBJECT *herosGun;
    
    
    /** Init function, it is called when all orx's modules have been initialized
     */
    orxSTATUS orxFASTCALL Init()
    {
        // Display a small hint in console
        orxLOG("\n* output on start");
    
        // Create the viewport
        orxViewport_CreateFromConfig("Viewport");
    
        // Create hero and gun
        hero = orxObject_CreateFromConfig("HeroObject");
        herosGun = (orxOBJECT*)orxObject_GetChild(hero);
        orxObject_Enable(herosGun, orxFALSE);
    
        // Create platform
        orxObject_CreateFromConfig("Scene");
    
        // Event Handler Collision
        orxEvent_AddHandler(orxEVENT_TYPE_PHYSICS, PhysicsEventHandler);
    
        // Done!
        return orxSTATUS_SUCCESS;
    }
    
    /** Run function, it is called every clock cycle
     */
    orxSTATUS orxFASTCALL Run()
    {
        orxSTATUS eResult = orxSTATUS_SUCCESS;
    
        //Inputs
        orxVECTOR leftSpeed = { -1, 0, 0 };
        orxVECTOR rightSpeed = { 1, 0, 0 };
    
        orxVECTOR flipLeft = { -2, 2, 1 };
        orxVECTOR flipRight = { 2, 2, 1 };
    
        if (orxInput_IsActive("GoLeft"))
        {
            orxObject_SetScale(hero, &flipLeft);
            orxObject_ApplyImpulse(hero, &leftSpeed, orxNULL);
            orxObject_SetTargetAnim(hero, "HeroRun");
        }
        else if (orxInput_IsActive("GoRight"))
        {
            orxObject_SetScale(hero, &flipRight);
            orxObject_ApplyImpulse(hero, &rightSpeed, orxNULL);
            orxObject_SetTargetAnim(hero, "HeroRun");
        }
        else {
            orxObject_SetTargetAnim(hero, "HeroIdle");
        }
        //Input Jump
        orxVECTOR jumpSpeed = { 0, -600, 0 };
    
        if (orxInput_IsActive("Jump") && orxInput_HasNewStatus("Jump"))
        {
            orxObject_ApplyImpulse(hero, &jumpSpeed, orxNULL);
        }
        //Input Gun
        if (orxInput_IsActive("Shoot"))
        {
            orxObject_Enable(herosGun, orxTRUE);
        }  else {
            orxObject_Enable(herosGun, orxFALSE);
        }
    
        // Should quit?
        if(orxInput_IsActive("Quit"))
        {
            // Update result
            eResult = orxSTATUS_FAILURE;
        }
    
    
        return eResult;
    }
    
    
    // Physics Collision event
    orxSTATUS orxFASTCALL PhysicsEventHandler(const orxEVENT *_pstEvent)
    {
        if (_pstEvent->eID == orxPHYSICS_EVENT_CONTACT_ADD) {
            orxOBJECT *pstRecipientObject, *pstSenderObject;
    
            pstSenderObject = orxOBJECT(_pstEvent->hSender);
            pstRecipientObject = orxOBJECT(_pstEvent->hRecipient);
    
            orxSTRING senderObjectName = (orxSTRING)orxObject_GetName(pstSenderObject);
            orxSTRING recipientObjectName = (orxSTRING)orxObject_GetName(pstRecipientObject);
    
            if (orxString_Compare(senderObjectName, "StarObject") == 0){
                orxObject_SetLifeTime(pstSenderObject, 0);
            }
    
            if (orxString_Compare(recipientObjectName, "StarObject") == 0){
                orxObject_SetLifeTime(pstRecipientObject, 0);
            }
        }
        return orxSTATUS_SUCCESS;
    }
    
    
    /** Exit function, it is called before exiting from orx
     */
    void orxFASTCALL Exit()
    {
        // Let Orx clean all our mess automatically. :)
    }
    
    /** Bootstrap function, it is called before config is initialized, allowing for early resource storage definitions
     */
    orxSTATUS orxFASTCALL Bootstrap()
    {
        // Add a config storage to find the initial config file
        orxResource_AddStorage(orxCONFIG_KZ_RESOURCE_GROUP, "../data/config", orxFALSE);
    
        // Return orxSTATUS_FAILURE to prevent orx from loading the default config file
        return orxSTATUS_SUCCESS;
    }
    
    /** Main function
     */
    int main(int argc, char **argv)
    {
        // Set the bootstrap function to provide at least one resource storage before loading any config files
        orxConfig_SetBootstrap(Bootstrap);
    
        // Execute our game
        orx_Execute(argc, argv, Init, Run, Exit);
    
        // Done!
        return EXIT_SUCCESS;
    }
    
  • Then config

    ; spookyparty - Template basic config file
    
    [Display]
    ScreenWidth     = @MainCamera.FrustumWidth
    ScreenHeight    = @MainCamera.FrustumHeight
    Title           = spookyparty
    FullScreen      = false
    Smoothing       = false
    VSync           = false
    ShowFPS         = true
    
    [Viewport]
    Camera            = MainCamera
    BackgroundColor   = (0, 180, 255)
    
    [MainCamera]
    FrustumWidth    = 800
    FrustumHeight   = 600
    FrustumFar      = 2.0
    FrustumNear     = 0.0
    Position        = (0.0, 0.0, -1.0) ; Objects with -1 <= Z <= 1 will be visible
    
    [Resource]
    Texture         = ../data/texture
    Sound           = ../data/sound
    
    [Input]
    SetList         = MainInput
    
    [MainInput]
    KEY_ESCAPE   = Quit
    KEY_LEFT     = GoLeft
    KEY_RIGHT    = GoRight
    KEY_LCTRL    = Shoot
    KEY_LSHIFT   = Jump
    
    [FadeIn]
    SlotList        = @
    Type            = alpha
    Curve           = smooth
    StartTime       = 0
    EndTime         = 1.5
    StartValue      = -1
    EndValue        = 0
    
    [Physics]
    AllowSleep           = false
    Gravity              = (0.0, 800.0, 0.0)
    ShowDebug            = false
    
    [Scene]
    ChildList = PlatformObject # MiddlePlatformObject #TopLeftPlatformObject # TopPlatformObject # TopRightPlatformObject #StarObject
    
    [MiddlePlatformObject@PlatformObject]
    Position = (-150, 150, 0)
    Scale    = (30, 2, 0)
    Repeat   = (15, 1, 0)
    
    [TopLeftPlatformObject@PlatformObject]
    Position = (-400, 20, 0)
    Scale    = (14, 2, 0)
    Repeat   = (7, 1, 0)
    
    [TopPlatformObject@TopLeftPlatformObject]
    Position = (-100, -100, 0)
    
    [TopRightPlatformObject@TopLeftPlatformObject]
    Position = (200, -210, 0)
    
    
    [HeroObject]
    Graphic      = HeroGraphic
    Position     = (-350, 100, 0)
    Scale        = 2
    AnimationSet = HeroAnimationSet
    Body         = HeroBody
    ChildList    = HerosGun
    
    [HeroGraphic]
    Texture        = soldier_full.png
    TextureOrigin  = (0,0,0)
    TextureSize    = (32,32,0)
    Pivot          = center
    
    [HeroAnimationSet]
    Texture    = soldier_full.png
    FrameSize  = (32, 32, 0)
    HeroRun    = 6
    HeroIdle   = 1
    StartAnim  = HeroIdle
    HeroIdle-> = HeroIdle # .HeroRun
    HeroRun->  = HeroRun # HeroIdle
    Pivot      = center
    
    [HeroIdle]
    KeyDuration = 1.0
    
    [HeroRun]
    KeyDuration = 0.1
    
    [HeroBody]
    Dynamic           = true
    PartList          = HeroBodyPart
    LinearDamping     = 5
    FixedRotation     = true
    
    [HeroBodyPart]
    Type        = box
    Solid       = true
    SelfFlags   = hero
    CheckMask   = platforms # star
    
    [PlatformGraphic]
    Texture = box.png
    
    [PlatformObject]
    Graphic  = PlatformGraphic
    Position = (-400, 270, 0)
    Scale    = (54, 2, 0)
    Repeat   = (27, 1, 0)
    Body     = PlatformBody
    
    [PlatformBody]
    Dynamic  = false
    PartList = PlatformBodyPart
    
    [PlatformBodyPart]
    Type        = box
    Solid       = true
    SelfFlags   = platforms
    CheckMask   = hero
    
    [BulletGraphic]
    Texture = particle.png
    
    [BulletObject]
    Graphic  = BulletGraphic
    Speed    = (500, 0, 0)
    LifeTime = 1.0
    Scale    = 0.25
    
    [BulletSpawner]
    Object           = BulletObject
    WaveSize         = 1
    WaveDelay        = 0.1
    Position         = (0, 0, 0)
    ObjectSpeed      = (500, 0, 0)
    UseRelativeSpeed = true
    
    [HerosGun]
    Spawner         = BulletSpawner
    Position        = (0, 0, 0)
    
    [StarGraphic]
    Texture = star.png
    Pivot   = center
    
    [StarObject]
    Graphic   = StarGraphic
    Position  = (290, -260, 0)
    FXList    = StarFX
    Smoothing = true
    Body      = StarBody
    
    [StarFlashSlotFX]
    Type        = color
    Curve       = sine
    StartTime   = 0
    EndTime     = 1
    Absolute    = true
    StartValue  = (255,0,0)
    EndValue    = (255,255,0)
    
    [StarRotateSlotFX]
    Type       = rotation
    Curve      = linear
    StartTime  = 0
    EndTime    = 2
    StartValue = 0
    EndValue   = 359
    
    [StarFX]
    SlotList    = StarFlashSlotFX # StarRotateSlotFX
    KeepInCache = true
    Loop        = true
    
    [StarBody]
    Dynamic   = false
    PartList = StarBodyPart
    
    [StarBodyPart]
    Type        = box
    Solid       = true
    SelfFlags   = star
    CheckMask   = hero
    
  • Its definitely colliding with the star, as I am unable to pass it, but the event is not firing I believe. I may have put it in the wrong spot perhaps inside the CPP file?

  • Looks to be set right. Can you at least get a breakpoint in the PhysicsEventHandler funciton?

  • I've tried your code/config and it's working for me. However as I don't have your assets, I've been using box.png for the bullet, the platform and the star as well.

    I also had to move PhysicsEventHandler higher in the file so that I could compile it (at the moment it's defined/declared after being referenced).

    Have you tried running in debug to see if you get any messages?

  • Ah that was it, it must have been declared below where I was calling it from. I'm using Visual Studio code and just running make from the gmake folder to compile the project, I will look at getting a better IDE for this. Thanks for the help, sorry for having such a newbie problem.

  • Odd. Usually that would be a compile error for me. Glad it's all solved though.

  • Yep, weird indeed. And no worries for the questions, we're here to help. =)

Sign In or Register to comment.