Page 1 of 2

How do I force the sprite to bounce with right angle?

PostPosted: Tue Oct 20, 2015 1:22 pm
by sivricmarijan
Hello guys and girls!

I have 2 sprites: a player and enemy. Both of them are just simple circles. When they collide, I want the enemy sprite to bounce off the player sprite but not in the opposite direction of the place where it touched the player sprite. I want it to bounce with the right angle! How can I do it?

Thanks in advance!

Re: How do I force the sprite to bounce with right angle?

PostPosted: Tue Oct 20, 2015 2:20 pm
by jojoe
A right angle is 90 degrees. so on collision have it set the angle.

Collide with sprite------> set angle: Choose (-90+Self.angle,90+self.angle)

That will randomly pick weather to veer left or right and keep a localized orientation. It will clamp it to a 90 degree turn left or right.


Also change the sprites collision polly , and make it more sphere like, it will make so it will bounce more like a ball. Not sure about C2 but most physics engines support a simplified collision. You can choose Collision poly, box, circle, pill. If C2 uses this, then it is better to use.

Re: How do I force the sprite to bounce with right angle?

PostPosted: Tue Oct 20, 2015 4:05 pm
by LittleStain
Or do you mean something like This?

Re: How do I force the sprite to bounce with right angle?

PostPosted: Tue Oct 20, 2015 7:19 pm
by R0J0hound
While 90 is a "right angle", in this situation right angle means the correct angle. LittleStain's example visualizes it perfectly but you can simplify it to just this:

projectile: on collision with target:
--- projectile: set angle of motion to 2*(angle(Self.X,self.Y,target.X,target.Y)+90)-Self.Bullet.AngleOfMotion

Re: How do I force the sprite to bounce with right angle?

PostPosted: Tue Oct 20, 2015 7:31 pm
by LittleStain
R0J0hound wrote:While 90 is a "right angle", in this situation right angle means the correct angle. LittleStain's example visualizes it perfectly but you can simplify it to just this:

projectile: on collision with target:
--- projectile: set angle of motion to 2*(angle(Self.X,self.Y,target.X,target.Y)+90)-Self.Bullet.AngleOfMotion


I think I'll always keep thinking visually..
Thanks for the maths!

Re: How do I force the sprite to bounce with right angle?

PostPosted: Tue Oct 20, 2015 8:06 pm
by R0J0hound
The math was visual initially too. Although I only roughly recall how I derived it.

Re: How do I force the sprite to bounce with right angle?

PostPosted: Tue Oct 20, 2015 8:15 pm
by jojoe
@LittleStain Thanks for the solution :)

@R0J0hound Very elegant. I learned something new today. Thank you!

Re: How do I force the sprite to bounce with right angle?

PostPosted: Wed Oct 21, 2015 6:11 am
by sivricmarijan
Thanks guys but I want the sprite to bounce with 0, 90, 180 or 270 angle...it depends on the angle of collision. I want it to go left, right, up or down. I hope you understand what I want :)

Re: How do I force the sprite to bounce with right angle?

PostPosted: Wed Oct 21, 2015 8:30 am
by R0J0hound
Ah, so I was wrong about the type of right angle.

so the ball only ever goes 0, 90, 180 or 270 degrees?
You can just brute force it and make an event for each case. Here's one example:

Ball on collision with circle
Ball.angle = 0
Ball.y < circle.y
--- ball set angle to 270

Just make seven more like that for every posibility. It could then be simplified using sub-events and else's.

Re: How do I force the sprite to bounce with right angle?

PostPosted: Wed Oct 21, 2015 9:21 am
by sivricmarijan
R0J0hound wrote:Ah, so I was wrong about the type of right angle.

so the ball only ever goes 0, 90, 180 or 270 degrees?
You can just brute force it and make an event for each case. Here's one example:

Ball on collision with circle
Ball.angle = 0
Ball.y < circle.y
--- ball set angle to 270

Just make seven more like that for every posibility. It could then be simplified using sub-events and else's.


I did that and it didn't work. It started working after I set the "Set angle" property of the Bullet behavior to YES. Thanks guys ;)