0 Favourites

Coded Collision

  • I'm running into gameplay mechanics that require me to design my own collision events - mainly when you hit an enemy and he gets knocked back, regardless of the collision set for the object, the enemy will be knocked INTO the object if the collision box is big enough.

    What is the best way to fix the code for something like this? I've tried setting "if enemy is overlapping bigcollisionobject, when knocked back 100 pixels, move 100 pixels in the opposite direction". But it doesn't happen instantaneously, and looks very noticeable.

    Does anyone have any solutions or ideas for this?

    Thanks!

  • If I follow you correctly.

    You have (for example a rock) which if hit on the left side (by a missile ?) the rock is pushed to the right ? But in this case you are saying the rock is moving left ?

    Post some code for us to look at please ?

  • Here is part of the code. When the enemy is hit, it moves back based on the direction the player is moving/attacking.

    <img src="http://stevemazzaro.com/OM6A/Collision.png" border="0" />

    This works fine, but if a large environment obstacle (such as a rock, hill, or something that should create a boundary for the enemies/player) is behind the enemy when he is hit, he is shot into the object with no way of getting out.

    Let me know if this is more clear.

    Steve

  • first thought is...

    instead of moving away from the hit by 100 and therefore running the risk of getting trapped in the environment - set up a loop and move away maybe 5 pixels at a time (twenty times) each time checking to see if you have hit the environment which will cause you to stop moving away and move you back the 5 pixels required to get you out of the problem ?

    probably not the best way but it should work ?

  • That's a good idea. I tried doing a loop (Repeat 20 times) > move -5 pixels, but it doesn't seem to move the enemy.

    The event is a sub-event under Else Enemies X Is Overlapping IceBlock. Any idea why this wont work?

    Thanks, Ram!

  • Something like this might work for you:

    When enemy is hit

    .. Set enemy.destinationX to enemy.X-100 (away from palyer or whatever you wish)

    for each enemy

    enemy.destinationX is not -9999

    .. set enemy.position( lerp(enemy.X, enemy.destinationX, dt) )

    .. if enemy overlaps with object ( stone or something that should block the enemy)

    OR

    .. enemy.X = enemy.destinationX (if we've reached the desired oushback position)

    ... set enemy.destinationX -9999

    Probably should have written that in a .capx instead, would have been a bit clearer.. :)

    EDIT:

    Heres the .capx:

    Example thingy

  • This works great, but how would I move the enemy based on the player's position? (so if you hit the enemy from the 45 degree angle, he goes the opposite direction of you)

    Thanks, Vee!

  • I never thought of using lerp - this , of course, could be because I have never used it before <img src="smileys/smiley1.gif" border="0" align="middle" /> (In fact I don't think I have ever hear of it before ... <img src="smileys/smiley9.gif" border="0" align="middle" /> )

    Nice example Vee41.

    Off to learn about lerping...

  • Edited the example a bit, added some angle related stuff. Commented it onto the events so ask away if there is something unclear! :)

    RamPackWobble lerp is really useful thing, ever since I learned of it's existence I've been using it on pretty much everything :P

  • Hey Vee, thanks so much for taking the time to do this. I noticed one thing in your current capx file. The move angle works perfectly, but the collision with Rock no longer works properly.

    Any idea why this is?

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Hey Vee, thanks so much for taking the time to do this. I noticed one thing in your current capx file. The move angle works perfectly, but the collision with Rock no longer works properly.

    Any idea why this is?

    Oh it seems to be if you fire to the enemy multiple times this happens as enemy does not necessarily collide with the rock again. Added a quick fix for that. :)

  • Vee - you are my hero.

    Thanks!

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