1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS2.Issue with moving circles collision

Discussion in 'Programming' started by Petrik33, Nov 5, 2019.

  1. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    I have an issue with collision checking of circle instances, actually it just doesn't work and collide_object=noone in any case, also importatnt thing is that i don't know why but x always= chobj.x
    Important thing:Object doing this script is also a child of Pobj_circle
    So this is my GMS 2 script to check for collision between two circle instances:
    ///@desc CheckForCCollision
    for(var t=0;t<instance_number(all);t++)
    {
    if(t==0) var list=ds_list_create();
    var iobj=instance_find(Pobj_circle,t); !!!!!!!!! Actually I think problem is here or down below <------
    inst[t]=iobj;
    ds_list_add(list,inst[t]);
    }
    for(var u=0;u<ds_list_size(list);u++)
    {

    var chobj=ds_list_find_value(list,u)//Object we are checking now

    //Include Dynamic-Dynamic Collision
    //Make One Circle Stationary (V1-V2) creating new Velocity vector
    var scriptV_length=point_distance(chobj.Vx,chobj.Vy,Vx,Vy);
    var scriptV_direction=point_direction(chobj.Vx,chobj.Vy,Vx,Vy);


    // Early Escape test: if the length of the movevec is less
    // than distance between the centers of these circles minus
    // their radii, there's no way they can hit.
    var dist=point_distance(x,y,chobj.x,chobj.y) !!!!!! Using debugger I found that even When i Have checked Object as a different from checking object it's X and Y coordinates still don't change and have x=chobj.x
    when chobj is not the same as the object doing this script; <-------
    var R_Sum=R+chobj.R;
    if(scriptV_length<(dist-R_Sum)) collide_with=noone;
    ............
    ..................
    ................
    }
     
  2. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,867
    instance_number(all) is illegal since all isn't an object ID. Try using instance_count instead (which is the number of all instances in the room, regardless of type).
     
  3. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    Doesnt help because my for cicle needs to be checked instance_numebr(Pobj_circle) times.which is a number not an instance_id
     
  4. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    But still thank you)
     
  5. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    65
    I'm having a difficult time following your code and I feel like you could probably doing this in a lot simpler way. But, if your set on going about it this way and not just doing normal collisions then you should add some debugging to see what is your issue exactly. show_debug_message() for each value, each step of the way, and you should be able to trace it back and find the issue. Also, you should probably change that all to just the name of the object your testing collisions on instance_number(Pobj_circle)
     
  6. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    Has already done the thing you wrote in the end, I had it previously but changed trying to fix an issue, about debugging: I used it and found that X of object doing check and X of object being checked is the same , whatever the object is, but thank you very much, and i hadn't known about show debug message
     
  7. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    Replied below
    Sorry please but i really need help, maybe you know what to do with this problem, knowing that the main problem are variables of different objects which are equal without a reason, I mean i cant understand why X=Cobj.x and Y=cobj.y , cant talk about other variables because they are all equal to 0 while debugging because i Cant give my circles a speed while debugging
     
  8. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    Forget everything I wrote before. You really helped me, the variables are all correct, the script itself is wrong, but actually it would be great if you helped me with it, because i dont want to make a new post. But if you cant please reply, and I will make a new post
     
  9. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    65
    Well you can post your issue here and I can give it a shot, but no promises
     
  10. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    So now i have everything clear, but... I dont know how to make last part of my collision code:
    1.how to move circles until the point where they will meet accurately, after script detected collision;
    2.How to change their speed and moving direction after collision, knowing their masses,velocities,and direction before collision.
     
  11. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    So, to understand first problem better, my script detects collision and then does this code:
    //Dynamic-Static
    if(chobj.V=0)
    {
    x+=lengthdir_x(distance,scriptV_direction);
    y+=lengthdir_y(distance,scriptV_direction);
    }

    //Dynamic-Dynamic
    if (chobj.V!=0)
    {
    var i=scriptV_length/V; //represents when over the course of their movement the circles collided.

    distance=V*i;//Checking object distance
    var other_distance=chobj.V*i;//Distance of object being checked

    //Making an accurate collision
    chobj.x+=lengthdir_x(other_distance,chobj.destination);
    chobj.y+=lengthdir_y(other_distance,chobj.destination);

    x+=lengthdir_x(distance,destination);
    y+=lengthdir_y(distance,destination);
    }
    return true;

    scriptV_length is a vector subtraction of moving vector of one circle and another(V-cobj.V);
    destination is direction of moving vector;
     
  12. Petrik33

    Petrik33 Member

    Joined:
    Nov 5, 2019
    Posts:
    12
    About the second problem: It si just physics and the main problem is to add an momentum lose to avoid infinity movement for example if my circle hit the ball into the wall, the ball woud bounce back to circle the back to wall and it lasts infinity. To avoid these I need to add momentum lose, but i Dont know how to calculate it.
    Also I think about adding rotation to movement but its other story.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice