Jump to content


Photo

How does GM handle this?


  • Please log in to reply
5 replies to this topic

#1 Lightang3l

Lightang3l

    GMC Member

  • GMC Member
  • 855 posts

Posted 12 July 2012 - 12:44 PM

1
Let's say I have this code (messy code, but not the point):

if instance_exists(obj_one)
{
if x > 500
    {
     if shoot = 2
        {
         if obj_two.speed < 5
             {
              ....................................... (and so on)
             }
        }
    }
}

You get the idea... lot's of "ifs".

My question is:

If obj_one does not exist... does GM still go through the rest of the code?
or if shoot is not =2 does GM still continue to check all the "ifs" past that point or does it stop there?

In my opinion it seems a waste of CPU to process all the code after the first "if" statement that is not satisfied.

2

Turning off precise collision in a sprite fills the entire sheet with a collision mask. In all situations the precise collision mask is a lot smaller than the mask that covers the entire sprite.

What is faster? turning off precise collision or using the big mask?
  • 0

#2 Koaske

Koaske

    GMC Member

  • GMC Member
  • 383 posts
  • Version:GM8

Posted 12 July 2012 - 12:51 PM

1. If a single "if" condition is false, the program won't even go inside the block. In your case, if obj_one does not exist, the rest of the block won't be dealt with. You can try this easily with show_question, for example.
if show_question("Yes or no?")
{
    if show_question("Again: yes or no?")
    {
    }
}

2. I believe what matters more than size in this situation is the complexity of the mask. Size is also an issue, since bigger objects have larger collision masks, but that's not the main thing here.

Let's say you have two sprites and you use a simple shapes as their collision mask. If the masks are rectangles or circles, doing a collision check with them will be fairly simple, since basically we're just checking if certain points of the other mask are inside a rectangle or circle. Now, if you change the collision masks to be a precise and the masks have a complicated and irregular shape, the check won't be as easy, and will take more time.

I haven't programmed a collision check algorithm that would check if there's a collision between two objects that have collision masks of an arbitrary shape. What I did make a few days ago was an algorithm that checks a collision between two rotated rectangles, and even this is already a slower operation than checking if two non-rotated rectangles would collide. I could imagine that using shapes that may not even be convex takes even more time.

In your question, choosing the bounding box (a box that contains the full sprite) as the collision mask would be faster than the precise one, despite the fact that the box is bigger than the precise mask. It's just that much faster to check the rectangle collision.

Edited by Koaske, 12 July 2012 - 01:09 PM.

  • 0

#3 psycho666

psycho666

    GMC Member

  • GMC Member
  • 849 posts
  • Version:GM8

Posted 12 July 2012 - 12:51 PM

2. You are so wrong. You can modify your mask and make it as large or small as you wish, you can shape it as rectangle, disk or diamond shape, you can place it wherever you want...
It should be the same thing, to answer the question. You can set mask from another image if you wish, so i guess it stores the mask and whenever it needs it - it uses it. So it should be the same thing...
1.
if(something){
some_action
}
so if something is not, you miss the rest and go directly to the } symbol...
Now you can simplify your code a lot...
if(something1)&&(aomething2)&&something3){
some_action
}
then if any of somethings is not, it will skip all else...
  • 0

#4 Lightang3l

Lightang3l

    GMC Member

  • GMC Member
  • 855 posts

Posted 12 July 2012 - 12:58 PM

@ psycho666

I am well aware that you can modify collision masks any way you want, except for having them segmented... I am working with sprites for almost 2 years now :biggrin:

It's not a question of being wrong. I'm just asking... Is precise collision slower (processing power) than a full size mask?
  • 0

#5 TsukaYuriko

TsukaYuriko

    GMC Member

  • GMC Member
  • 510 posts
  • Version:GM:Studio

Posted 12 July 2012 - 01:06 PM

Yes, because precise collisions check for collisions on a pixel per pixel basis while a standard mask only checks whether two regions overlap, to put it simply. The latter is one check, the former can be hundreds.
  • 0

#6 dannyjenn

dannyjenn

    GMC Member

  • GMC Member
  • 2239 posts
  • Version:Mac

Posted 12 July 2012 - 04:00 PM

One thing to add to #1...
As Koaske said, doing this
if instance_exists(obj_one)
{
if x > 500
    {
     if shoot = 2
        {
         if obj_two.speed < 5
             {
              ....................................... (and so on)
             }
        }
    }
}
will only result in one comparison if obj_one does not exist.
However, doing this
if(instance_exists(obj_one) && x>500 && shoot==2 && obj_two.speed<5){
    ....................................... (and so on)
}
will always make 4 comparisons, even if the first one is false.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users