How do I quickly and easy figure out which side of a sprite.

Get help using Construct 2

Post » Fri Mar 06, 2015 3:40 pm

Try this formula and see if it works..........(2*(angle(wall.x,wall.y,ball.x,ball.y)-ball.angle)+ball.angle)+180
What I know would fill a Book , what I don't know would fill a Library
B
37
S
12
G
29
Posts: 331
Reputation: 16,499

Post » Fri Mar 06, 2015 5:19 pm

@granpa I used your formula like this:
code.jpg


And the ball bounces off in strange angles, not what I would like.

I am using this for the blocks and not the walls as I use plugin Boundary for the walls and there I could determine the direction thanks to the top, bottom, left, right side.
You do not have the required permissions to view the files attached to this post.
B
58
S
18
G
13
Posts: 447
Reputation: 10,740

Post » Fri Mar 06, 2015 5:25 pm

I edited the formula to replace the ball.angle to ball.custommovement.movingangle but still irregular angles. The angle result is quite high too, 389 degrees, 455 degrees, etc


I edited the formula and until I removed the 2* in beginning and the +180 at the end the ball started to bounce and not pass through blocks, and the angle result is in more "realistic" range, but still not perfect angles.
Last edited by helena on Fri Mar 06, 2015 5:34 pm, edited 1 time in total.
B
58
S
18
G
13
Posts: 447
Reputation: 10,740

Post » Fri Mar 06, 2015 5:32 pm

The formula I gave you was a derivative of a standard bounce formula when you know the angle......sorry it did not work . The original formula is 2*(out.angle-in.angle)+in.angle...... where out angle is what you want to bounce off and in angle is the ball .
What I know would fill a Book , what I don't know would fill a Library
B
37
S
12
G
29
Posts: 331
Reputation: 16,499

Post » Fri Mar 06, 2015 5:36 pm

@granpa thanks, I edited my post, read the last paragraph... Or better yet:

2*((angle(Sprite.x,Sprite.y,ball.x,ball.y)-ball.CustomMovement.MovingAngle)+ball.CustomMovement.MovingAngle)

This is "closing" but not perfect, bouncing and not passing through blocks, but in wrong angle.
B
58
S
18
G
13
Posts: 447
Reputation: 10,740

Post » Fri Mar 06, 2015 5:42 pm

Also I have a feeling this formula doesn't know which side the sprite is, really. hmm?

Got suggested this post by @r0j0hound I will examine the code it might be the solution.
viewtopic.php?f=147&t=121599&p=867906&hilit=bouncing#p867906

(at first glance I see the ball is not even having any behavior, all coded in events!)
B
58
S
18
G
13
Posts: 447
Reputation: 10,740

Post » Fri Mar 06, 2015 5:46 pm

Doing it with just events is simple enough. Give the ball two variables vx and vy for the x and y velocity. Motion is done by appling the formula speed*time=distance. You can also set it from a speed and direction if you so please with a simple convertion from polar coordinates to rectangular: vx=speed*cos(direction) and vy=speed*sin(direction).

Then instead of using a plugin or behavior, just check the position against the four walls and bounce. See attached capx.

That's just for outside walls. For inside walls you'll need push the ball out of the wall before using overlaps at offset to bounce. In the following link I implemented the push out with just events and it works well enough. If you make the ball too fast so that the jump of positions completely misses the wall then this still could fail.
viewtopic.php?f=147&t=121599&p=867906&hilit=bounce#p867906

To solve passing through walls you'll want to check the positions in between or do a raycast.
For the first you could do the same as this bouncing laser example:
viewtopic.php?f=147&t=65738&p=560745&hilit=laser#p560745
You do not have the required permissions to view the files attached to this post.
B
94
S
33
G
118
Posts: 5,393
Reputation: 75,847

Post » Fri Mar 06, 2015 5:57 pm

@R0j0hound

Thank you for your help! Lots of good examples there. I will try to wrap my head around this one.
Looks quite elegant too.

Thanks again.

Now off to do the evening out but tomorrow it is code time!!
B
58
S
18
G
13
Posts: 447
Reputation: 10,740

Post » Sat Mar 07, 2015 5:59 pm

@R0J0hound
Here is my take of your examples, I combined the two different capx into one.'
No custom plugins, yay!

It seems to work nicely. Even in quite high speeds. Of course on my iphone 4 it is at 10-15 fps. ;) (but it could be the every ticks-change text so nevermind for now)

I have new questions.

1. How do I change the angle, if I like want to nudge it 1-2 degrees? Good to have when ball is stuck in endless loop bounce in levels with intricate solid walls.

2. If I want to make a paddle where it would change angle depending on where it is hit on the paddle itself. Or if I add round objects.

Thanks!!
You do not have the required permissions to view the files attached to this post.
B
58
S
18
G
13
Posts: 447
Reputation: 10,740

Post » Sat Mar 07, 2015 7:45 pm

1.
You could either just nudge vx and vy around when bouncing. For example for a horizontal bounce:
set vx to -self.vx
add random(-1,1) to vy

A vertical bounce can be done similarly.

Or you can actually nedge the angle. To do that takes two steps. First calculate speed and angle from vx and vy, then convert it back to vx and vy from that after nudging the angle.
ang = angle(0,0,ball.vx,ball,vy)
speed = distance(0,0,ball.vx,ball,vy)
vx = speed * cos(ang+random(-2,2))
vx = speed * sin(ang+random(-2,2))


2.
For changing the angle depending on where on the paddle was hit you could just add the horizontal distance between the ball and paddle when hitting the top of the paddle (event 16). So basically:
add (ball.x-paddle.x) to vx

You could multiply it by 0.5 or something to make it more subtle.

For actual round objects you'd need something better for contact normal detection.

This may work, you may want to add 1 or so to the radius check.
Code: Select all
local number NormalAngle=0

For each roundWall
system compare: distance(roundWall.x,roundWall.y,ball.x,ball.y) <= ball.radius+roundWall.radius
--- set normalAngle to angle(roundWall.x,roundWall.y,ball.x,ball.y)


Basically to bounce you need to know the normal of the surface hit and then you can bounce using either the math event 19 in your capx or with the normal angle using that formula from granpa:
set angleOfMotion to 2*normalAngle-ball.AngleOfMotion+180
B
94
S
33
G
118
Posts: 5,393
Reputation: 75,847

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests