• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

SOLVED precise collision problem

Wataponno

Member
So in my game, when a player hits a wall with a weapon, it makes a spark when and where the player hits wall. In the following code, everything works as intended EXCEPT that the spark is never made at the right y coordinate (i in the for loop). The sprite stops at the good time (ie it doesn't enter the wall (that code is in the alarm[0] and works well)) BUT the spark (ob_1_loop_effect) is created at the right X, but not at the right Y. Especially when hitting the lower portions of the wall, the spark is created in the upper section of the wall. The origin of my sprite wall is 0.0 and changing it from that only worsens the problem.

THIS CODE IS IN THE WEAPON OBJECT
GML:
if ob_player.stance == 1//spark wall slash on wall when in katana form

{

    if image_index > 1

    {

        if place_meeting(x,y,ob_wall)

        {

            wall = instance_nearest(x,y,ob_wall)

            if x < wall.x

            {

                var i

                for (i = wall.bbox_top-96; i < wall.bbox_bottom-96; i += 1) //I FOUND THAT ADDING -96 (sprite height) TO EACH HELPS, WITHOUT THAT THE SPARK IS ALWAYS CREATED AT THE BOTTOM OF THE WALL

                {

                    if place_meeting(wall.bbox_left,i,ob_wall) //TRIED collision_point WITH THE SAME RESULTS

                    {

                        if alarm[0] < 0

                        {

                            alarm[0] = 1.5/ob_player.image_speed

                            spark = instance_create(wall.bbox_left,i,ob_1_loop_effect)

                            spark.image_speed = 0.5

                            spark.sprite_index = spr_wall_slash_spark

                            spark.depth = ob_wall.depth-1

                        }

                    }

                }

            }

        }

    }

}


Thank you for your help
 
Last edited:

Nidoking

Member
I suspect you want position_meeting rather than place_meeting, and you want to check a collision with the weapon object rather than the wall. And get rid of the -96. You've fallen into the trap of "it looks better, therefore it must be closer to the correct solution".
 

FrostyCat

Member
instance_nearest() is also being used as a crutch here and may be returning the wrong instance. The original poster should learn the difference between collision functions.
Meeting vs. Instance
When you use a function that has meeting in its name, it just tells whether there is a collision and returns either true or false. On the other hand, functions starting with instance returns the colliding instance's id, or the special value noone (-4) when there is no collision.

You may sometimes see that the return value of instance_place() or instance_position() is used as if it is boolean.That works because Game Maker's if statement evaluates values greater than or equal to 0.5 as true and all others as false. Therefore, if (A) behaves like if (A >= 0.5), and if (!A) behaves like if (A < 0.5). It enables you to use instance_place() or instance_position() in place of place_meeting() or position_meeting(). However, note that the opposite doesn't work.
Code:
// This checks if there is an instance of object0 under the mouse,
// and store its instance id to the variable inst.
// The result is noone (-4) if there is no collision.
inst = instance_position(mouse_x, mouse_y, object0);
if (inst) { // This is equivalent to saying "if (inst > 0)"
    inst.selected = true; // Manipulate the instance's variable
}
 
Top