Page 1 of 2

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

Posted: Tue Oct 20, 2015 1:22 pm
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?

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

Posted: Tue Oct 20, 2015 2:20 pm
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?

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

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

Posted: Tue Oct 20, 2015 7:19 pm
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?

Posted: Tue Oct 20, 2015 7:31 pm
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?

Posted: Tue Oct 20, 2015 8:06 pm
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?

Posted: Tue Oct 20, 2015 8:15 pm
@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?

Posted: Wed Oct 21, 2015 6:11 am
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?

Posted: Wed Oct 21, 2015 8:30 am
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?

Posted: Wed Oct 21, 2015 9:21 am
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