How do I get the exact position of a collision?

0 favourites
  • 6 posts
From the Asset Store
Particles support animations, collisions, effects and etc.
  • I want to when the sword hits the enemy, spawns particles where the colision happens, is it possible?

  • A solution that comes to my mind, would be adding an image point to the position where the sword hits on the attack animation's last frames and sticking a small invisible sprite in it, which would then spawn particles on enemy collision.

    Go to the hero's sprite editor and add an image point, set it in the position you want, in this case select the frame where the sword hits and set the image point to the sword's tip.

    Then create a small sprite and make it invisible from its settings.

    Every time you hit, make the hero spawn the invisible sprite to the image point you made earlier. If you want to be exact about the timing, compare the frame and select the one where the sword is supposed to touch the enemy. Start a timer, to destroy the invisible sprite some time after the hit is finished.

    Check if the invisible sprite is in collision with the enemy and if it is, make it spawn the particles.

    So basically the blood comes from the hero sprite, not from the enemy. I'm not sure how well this solution works and I haven't used particles myself before, but this is what came to my mind first.

  • A solution that comes to my mind, would be adding an image point to the position where the sword hits on the attack animation's last frames and sticking a small invisible sprite in it, which would then spawn particles on enemy collision.

    Go to the hero's sprite editor and add an image point, set it in the position you want, in this case select the frame where the sword hits and set the image point to the sword's tip.

    Then create a small sprite and make it invisible from its settings.

    Every time you hit, make the hero spawn the invisible sprite to the image point you made earlier. If you want to be exact about the timing, compare the frame and select the one where the sword is supposed to touch the enemy. Start a timer, to destroy the invisible sprite some time after the hit is finished.

    Check if the invisible sprite is in collision with the enemy and if it is, make it spawn the particles.

    So basically the blood comes from the hero sprite, not from the enemy. I'm not sure how well this solution works and I haven't used particles myself before, but this is what came to my mind first.

    thanks dude, it's not perfect at all (sometimes a bit strange) but for now a can use it

  • I would really appreciate if someone has a better way to do it

  • I may be overthinking it but when two objects overlap there isn't typically a single contact point but an overlapping area. If the collision shapes are convex there may even be multiple areas. A way to find those areas would be to take the collision polygons and applying some clipping algorithm to that, but that's not very trivial to do.

    As a second phase you could then find the midpoint of those areas to give you a single point.

    Alternately you could utilize an algorithm called the Separating Axis Theorem (SAT) to push the objects apart to just be touching and find that point. If the shapes are concave then it would be more complex to handle. This is also a bit involved to make.

    Another idea is to utilize something like a raycast. Check the points from the hilt to the tip of the blade to see if any collide. As soon as one does, stop checking more and use that point. You could use the condition "pick object overlapping point" to do the point collision checks, or you could use a small detector sprite that you place along the sword.

    Anyways here's the point example way to do it. The sword has it's origin at the hilt and an imagepoint at the tip.

    on sword collides with enemy
    repeat 10 times
    pick enemy overlapping point (lerp(sword.x, sword.imagepointX(1), loopindex/9), lerp(sword.y, sword.imagepointY(1), loopindex/9)
    --- stop loop
    --- create particles at (lerp(sword.x, sword.imagepointX(1), loopindex/9), lerp(sword.y, sword.imagepointY(1), loopindex/9)
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I may be overthinking it but when two objects overlap there isn't typically a single contact point but an overlapping area. If the collision shapes are convex there may even be multiple areas. A way to find those areas would be to take the collision polygons and applying some clipping algorithm to that, but that's not very trivial to do.

    As a second phase you could then find the midpoint of those areas to give you a single point.

    Alternately you could utilize an algorithm called the Separating Axis Theorem (SAT) to push the objects apart to just be touching and find that point. If the shapes are concave then it would be more complex to handle. This is also a bit involved to make.

    Another idea is to utilize something like a raycast. Check the points from the hilt to the tip of the blade to see if any collide. As soon as one does, stop checking more and use that point. You could use the condition "pick object overlapping point" to do the point collision checks, or you could use a small detector sprite that you place along the sword.

    Anyways here's the point example way to do it. The sword has it's origin at the hilt and an imagepoint at the tip.

    on sword collides with enemy
    repeat 10 times
    pick enemy overlapping point (lerp(sword.x, sword.imagepointX(1), loopindex/9), lerp(sword.y, sword.imagepointY(1), loopindex/9)
    --- stop loop
    --- create particles at (lerp(sword.x, sword.imagepointX(1), loopindex/9), lerp(sword.y, sword.imagepointY(1), loopindex/9)

    OH MY GOD, I would never figure out it alone, thank you so much!!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)