Page 1 of 1

Collisions as a conditional parameter

PostPosted: Mon Jan 21, 2013 2:36 pm
by bamby1983
Is it possible to use a collision as a conditional parameter in an if-statement? For example:

Code: Select all
if (<Actor A is colliding with Actor B>) {
    // Do this
                                                        }
else {
    // Do that
      }

Re: Collisions as a conditional parameter

PostPosted: Mon Jan 21, 2013 6:54 pm
by skydereign
Technically yes. There is a function called getAllActorsInCollision, which can be used to do that.
Code: Select all
 int n;
Actor *actors;

actors = getAllActorsInCollision("Event Actor", &n);


if(actors)
{
    int i;
    for(i = 0; i < n; i++)
    {
        if(strcmp(actors[i].name, "B")==0) // colliding with an actor B
        {
            // do this
        }
        else
        {
            // do that
        }
    }
}

Notice though you have to loop through all actors colliding with the actor. Also, you don't have to specify "Event Actor" in the getAllActorsInCollision. If I had followed your use case, I would have put "A" instead (but usually that isn't a good idea). When you can, you should be clone specific, instead of actor specific.

Re: Collisions as a conditional parameter

PostPosted: Mon Jan 21, 2013 9:30 pm
by bamby1983
Thanks for the code, sky. I'm a little confused about certain bits, though.

What do the following pieces of code do?

Code: Select all
Actor *actors;


Code: Select all
if(actors)

Re: Collisions as a conditional parameter

PostPosted: Mon Jan 21, 2013 11:13 pm
by skydereign
Code: Select all
 int n; // used to store the number of actors colliding with event actor
Actor *actors; // declares an Actor* pointer (used to point to an array of actors event actor is colliding with)

actors = getAllActorsInCollision("Event Actor", &n); // the &n passes the address of the variable n, so the function can change the value
// actors now points to an array of n actors, where n is equal to the number of actors colliding with the actor
// if no actors are colliding, actors is set to null
if(actors) // this makes sure actors is not null (there are actors colliding and getAlActorsInCollision succeeded)
{
    int i;
    for(i = 0; i < n; i++)
    {
        if(strcmp(actors[i].name, "B")==0) // colliding with an actor B
        {
            // do this
        }
        else
        {
            // do that
        }
    }
}

Re: Collisions as a conditional parameter

PostPosted: Mon Jan 21, 2013 11:23 pm
by bamby1983
Thank you, you are awesome! :D

Re: Collisions as a conditional parameter

PostPosted: Thu Mar 21, 2013 2:28 am
by bamby1983
I tried using the approach in this thread but it doesn't seem to work. Here's the code I wrote for the function that checks whether 2 actors collide with each other. The function is designed so that it can accept the actor names dynamically. I removed the if statement safeguard against zero collisions as my background ensures that at least 1 actor always collide with the object.

Code: Select all
int checkCollision(char actor_1[30], char actor_2[30]) {
    int n; // Used to store the number of actors colliding with event actor
    int i;
    Actor *actors; // Declares an Actor* pointer (used to point to an array of actors event actor is colliding with)
 
    actors=getAllActorsInCollision(actor_1, &n); // The &n passes the address of the variable n, so the function can change the value
    // Actors now points to an array of n actors, where n is equal to the number of actors colliding with the actor
    // if no actors are colliding, actors is set to null
    for(i=0;i<n;i++) {
        if(strcmp(actors[i].name, actor_2)==0) // Check whether actor_1 collides with actor_2
            return 1;
                     }
       return 0;
                               }



Here is how I call this function when trying to check whether two objects collide with each other:

Code: Select all
if (checkCollision("Hover_Table_Right", "Key")) {
            // Some code here
                                              }
        else {
            // Additional code here
             }


The code in the else statement always gets executed instead of that in the if statement, regardless of whether or not the two objects collide. Could someone please let me know whether I've missed something and have gone wrong somewhere?

Re: Collisions as a conditional parameter

PostPosted: Thu Mar 21, 2013 4:34 am
by skydereign
That's because getAllActorsInCollision requires a clonename, not an actor name. So it needs to be more specific than just "Hover_Table_Right". I'll fix this though, as you would think it would work like other gE functions and default to the lowest indexed. For now though, you'll have to use "Hover_Table_Right.0" instead.

Re: Collisions as a conditional parameter

PostPosted: Thu Mar 21, 2013 2:16 pm
by bamby1983
Thank you, Sky! This fixed the problem! :)

I also learnt that using "Event Actor" in place of the clone name when the same clone calls the function yields successful results as well.

Re: Collisions as a conditional parameter

PostPosted: Thu Mar 21, 2013 6:32 pm
by skydereign
Right, Event Actor is normally what you would use. There is also "Collide Actor" for the actor you are colliding with, "Parent Actor" for the event actor's parent, and "Creator Actor" for the actor that created the event actor. Though some may not be implemented quite correctly for some functions in 1.4.

Re: Collisions as a conditional parameter

PostPosted: Fri Mar 22, 2013 2:58 am
by bamby1983
Thanks, that's useful to know.