# Bullet sliding against walls instead of bouncing ?

1 favourites
• 7 posts
From the Asset Store
Bouncing ball
\$9.99 USD
Template for a bouncing ball game, fully documented in comments and video
• Hello,

I have an enemy with the bullet behaviour.

When he sights the player, his angle goes toward the player.

But if the distance between the player & enemy < 200, his angle changes to angle(playerXY,EnemyXY)+90 or -90, this way he starts to randomly turn around the player.

My problem is when he collides with a wall...I'm preventing collision by using the "bounce off solid" but this gives a pretty bad result as the eneymy keep colliding itself against the wall (until randomly changing his angle again).

So I was wondering, can I make it so the enemy slide against the wall (in the appropriate direction regarding his current angle of motion) instead of colliding with it ? How would I do that ?

Or if you have any other solution to make it so the enemy avoids walls, I'm interrested.

• Sounds like you would need to get the vector of the wall facing and use that to calculate the new angle on collision, but I'm not sure if/how you can get that information.

You may want to investigate the custom movement behavior's "push out solid" action.

• Straight walls or how does all that look? For straight walls it shouldn't be a big problem.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• Yes they are straight walls, with atm only 90�angles (for floor, side wall & ceiling), but ideally I would also like to have some more fancy angles later..

I've found a formula on this forum about Cosinus & sinus (which I dont really understand..but it's working so far). Im applying it with the help of 2 detectors for X & Y pinned to my ennemy and the event looks like this:

Detector X overlapping wall

--> Enemy set X to self.x-cos(self.Bullet.AngleOfMotion)*self.Bullet.Speed*dt

Detector Y overlapping wall

--> Enemy set Y to self.Y-sin(self.Bullet.AngleOfMotion)*self.Bullet.Speed*dt

That works...but there is still a problem when this happens:

(orange rectangle is Y detector, blue is X)

<img src="http://i.imgur.com/Qh79369.jpg" border="0" />

There, the enemy will just get stuck (if not worst)...as the wrong detector got triggered first.. :/

And if I make both detectors the same size to cover the 4 corners "holes" then it doesnt work at all as they are both triggered at the same time...

I dont know what to do here, I've tried many things but unsuccesfully so far..and as I dont really understand that cosinus formula It's hard for me to "tweak" it, so if anyone could help me on this that would be great, thanks.

Maybe breaks at faster bullet speeds and positioning of more crowded angle compositions can be tricky, but other than that works. Ofc you'd need to do that stuff for each enemy.

• Well thanks for your help mindfaQ

But unfortunately that doesnt work for what I want to do as I keep modifying the bullet angle of motion, that's why I was going for a "set X,Y" event instead..this way the bullet behaviour is not altered and can continue doing his stuff..

Also there is a problem in your example as the enemy still goes through walls from time to time..

I think the detectors I'm using was a bad idea, and maybe I can trigger the "set X,Y" with something else (like the overlapping at offset ?) but as I said, Im kinda lost with how to change the cosinus formula..

If anyone got an idea, I'd be glad to hear it ! :)

• Well I can explain the formula for you:

cos(angle) gives the x-portion of the bullet movement back to you (going right on screen gives a positive value, going left a negative value)

sin(angle) gives the y-portion of the bullet movement back to you (going up = negative, going down = positive)

dt*bullet.speed just is the distance the bullet would travel without interference

so all it does is setting the movement on the overlapped axis back to where it was before the bullet movement of the current tick while retaining the bullet movement of the other axis.

Result: bullet speed is not retained, if you want that you probably need to add the movement you've taken away to the other axis (need a condition construction to not get the signs (+-) wrong)

Result 2: once both overlap, you get no movement at all (in your version) or weird movement (when keeping bullet speed constant); can't really prevent that no matter how the dimensions of the boxes are; only thing you could do is check if movement is frozen and then do a turnaround or slight change in coordinates to allow a fresh approach to the wall (this is probably prefered for you)

so I can imagine that something like this could work:

Detector X overlapping wall

--> Enemy set X to self.x-cos(self.Bullet.AngleOfMotion)*self.Bullet.Speed*dt

--> enemy add "1" to enemy.variable

Detector Y overlapping wall

--> Enemy set Y to self.Y-sin(self.Bullet.AngleOfMotion)*self.Bullet.Speed*dt

--> enemy add "1" to enemy.variable

for each enemy

-system compare

--> if enemy.variable greater of equal 2: enemy set y to self.y -sin(self.bullet.angleofmotion)*self.bullet.speed*dt

• no condition

--> set enemy.variable = 0

instead of modifying the angle you can also add -sin(self.Bullet.AngleOfMotion) to y to make the enemy take a new approach

• 7 posts