Coded Collision

Get help using Construct 2

Post » Mon Mar 04, 2013 7:01 am

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!
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Mon Mar 04, 2013 9:12 am

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 ?

August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
59
S
30
G
34
Posts: 1,194
Reputation: 23,767

Post » Mon Mar 04, 2013 6:03 pm

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



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.

Steveome6a17172013-03-04 18:04:50
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Mon Mar 04, 2013 6:52 pm

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 ?

August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
59
S
30
G
34
Posts: 1,194
Reputation: 23,767

Post » Mon Mar 04, 2013 7:21 pm

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!ome6a17172013-03-04 19:23:31
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Mon Mar 04, 2013 7:34 pm

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 thingyvee412013-03-04 19:53:01
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Mon Mar 04, 2013 8:24 pm

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!
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Mon Mar 04, 2013 8:30 pm

I never thought of using lerp - this , of course, could be because I have never used it before (In fact I don't think I have ever hear of it before ... )

Nice example Vee41.

Off to learn about lerping...

August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
59
S
30
G
34
Posts: 1,194
Reputation: 23,767

Post » Tue Mar 05, 2013 8:12 am

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
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Tue Mar 05, 2013 10:03 am

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?
B
22
S
3
Posts: 451
Reputation: 2,135

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 0 guests