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

0 favourites
From the Asset Store
Build your own spaceship for your side scroller space shooter games with various components.
  • 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.

    (at first glance I see the ball is not even having any behavior, all coded in events!)

  • 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.

    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:

  • 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!!

  • 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.


  • 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))


    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.

    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)[/code:1dvz1wet]
    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
  • I tried to install rex_boudary plugin but it doesn't seem to install and I cant open your project

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • TheDom

    It is ok, I have abandoned it and am going on code only now.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)