Jump to content


Photo

Collision problem


  • Please log in to reply
7 replies to this topic

#1 Dylan93

Dylan93

    GMC Member

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 29 December 2010 - 12:07 PM

Hello GMC,

I currently use a collision method which just forces the object to move away from the thing its in collision with, here's the code:
var dir, xcol, ycol;
dir=point_direction(x,y,other.x,other.y)
xcol = lengthdir_x(1,dir);
ycol = lengthdir_y(1,dir);

while (place_meeting(x,y,other))
{
    x -= xcol;
    y -= ycol;
}

Well, here's what happens:
Posted Image

The enemy's collide with the wall at the start of the arrow and they just 'glide' until they are at the point where the two blocks are aligned to each other and they move through that spline...

Can anyone help me solving this problem ?
I already tried other (basic) collision things like bounce and move_contact but neither works.
The enemies move with mp_potential_step codes towards the target.

btw, putting the walls more 'in' each other wont work either, as they still go through the little line where the sprite of the walls are colliding.
  • 0

#2 nukemaster

nukemaster

    GMC Member

  • New Member
  • 268 posts

Posted 29 December 2010 - 01:05 PM

Can I just ask what it is that you are specifically trying to achieve? Do you want the object to hit the other one and then stand still, or do you want it to continue moving away, or do you want it to go around?

And what objects are going to be moving? Just the object with the code (which will have a simple solution) or the wall object as well? (which may be a little trickier)

If possible, could you please upload the .gmk file so that we can see exactly what you are doing. Small things can make sometimes make a big difference, for example the mp_potential_step code which you mentioned almost as an afterthought.
  • 0

#3 Dylan93

Dylan93

    GMC Member

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 29 December 2010 - 01:19 PM

I want the AI to go around it for sure, the thing is it goes through it at those points.
Here is the game engine, note: I stripped all the functions so only the enemy and the player is left, only thing you can do here is spawn them with the right mouse button so you can see it :).

Link
  • 0

#4 nukemaster

nukemaster

    GMC Member

  • New Member
  • 268 posts

Posted 29 December 2010 - 05:28 PM

Okay, I think I may have found a solution.

Try replacing your code with:
var dir, xcol, ycol;
dir=point_direction(x,y,xprevious,yprevious)
xcol = lengthdir_x(3,dir);
ycol = lengthdir_y(3,dir);

while (place_meeting(x,y,other))
{
    x += xcol;
    y += ycol;
}

This moves the object in the opposite direction of its current movement, rather than away from the origin of the wall. I just hope it doesn't give any unforseen problems. Tell me how it works for you.

NOTE: Only replace the static collision code, the dynamic collision seems to be working fine.

Edited by nukemaster, 29 December 2010 - 05:42 PM.

  • 0

#5 nukemaster

nukemaster

    GMC Member

  • New Member
  • 268 posts

Posted 29 December 2010 - 05:40 PM

Perhaps it would be better to replace the code with the following:

var dir, xcol, ycol;

if(x-xprevious != 0 && y-yprevious != 0) 
{
dir=point_direction(x,y,xprevious,yprevious)
xcol = lengthdir_x(3,dir);
ycol = lengthdir_y(3,dir);
}
else
{
xcol = 3
ycol = 3
}


while (place_meeting(x,y,other))
{
    x += xcol;
    y += ycol;
}


This is to prevent it getting stuck just because it wasn't moving.

There is still an occasional bug where the zombie will get stuck when its 'inside' the wall and another zombie is blocking it, so it keeps jumping between trying to get out of the wall and trying to get away from the other zombie, but it shouldn't be too much of a problem.
  • 0

#6 Dylan93

Dylan93

    GMC Member

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 29 December 2010 - 07:37 PM

Perhaps it would be better to replace the code with the following:

var dir, xcol, ycol;

if(x-xprevious != 0 && y-yprevious != 0) 
{
dir=point_direction(x,y,xprevious,yprevious)
xcol = lengthdir_x(3,dir);
ycol = lengthdir_y(3,dir);
}
else
{
xcol = 3
ycol = 3
}


while (place_meeting(x,y,other))
{
    x += xcol;
    y += ycol;
}


This is to prevent it getting stuck just because it wasn't moving.

There is still an occasional bug where the zombie will get stuck when its 'inside' the wall and another zombie is blocking it, so it keeps jumping between trying to get out of the wall and trying to get away from the other zombie, but it shouldn't be too much of a problem.


Thanks for your code, it works even better then mine, I benchmark'd it and the FPS difference was 21.
Only bad side is, the player itself seems to move through walls, when I keep walking to the left for example I will go through the wall.

Thanks for helping :)

EDIT:

When I'm walking close to a wall and I move left and also press move up, I'm going diagonally to the up left through the wall...

Edited by Dylan93, 29 December 2010 - 07:52 PM.

  • 0

#7 nukemaster

nukemaster

    GMC Member

  • New Member
  • 268 posts

Posted 29 December 2010 - 09:05 PM

Glad I was able to help.

As for the second problem: strangely I can't seem to recreate the effect you are referring to. My man doesn't go through walls at all. Then again, I don't have registered GM 8, so I had to take out all the rotation code, I don't know if it has anything to do with that, but I doubt it.

The other thing is that the mp_potential_step(target.x,target.y,speed,1); isn't actually doing anything with the way the code is at the moment, the zombies aren't going around the obstacles. I would replace it with mp_linear_step(target.x,target.y,speed,1); which will cause them to stop when they reach an object rather than try to find a way around (which they aren't doing anyway).

I don't understand why the zombies aren't trying to go around the objects. I tried different combinations of code but it doesn't seem to work. If I do find a solution, I'll be sure to let you know.

EDIT: Here is an example that uses basically the same code, but the objects seem to go around the walls instead of shaking in one place. See if you can spot any difference between the codes that might be the cause of the problem. I can't seem to spot it: http://www.mediafire...hc0002ycjw06wvb

Edited by nukemaster, 29 December 2010 - 09:22 PM.

  • 0

#8 Dylan93

Dylan93

    GMC Member

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 30 December 2010 - 01:17 PM

Hmm then I guess it has to do with the rotation sprites, but then I still don't get it as the objects all have a circle mask that controls the collisions...

Edited by Dylan93, 01 January 2011 - 01:14 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users