How do I check direction of a bouncing bullet and Set anim.

  • Hey all! Pretty simple question (I think) but I can't figure it out. I'm trying to create this super simple AI. It's basically just this creature from Metroid.

    What I've created is close enough. So in C2, it's just a sprite with a bullet behavior with bounce of solids set to Yes. In the event sheet all I have is On Start of Layout|AntiGravity Bullet Angle of Motion set 45 degrees. So very simple. How can I check when the creature is moving left or right so I can mirror/flip the animation?

    I couldn't find any info using search and I couldn't deduce how to work something out using debug. Any help would be appreciated! Also, notice how when the sprite collides with an edge of a tile map, the angle of motion changes drastically. How can prevent this? I want it to bounce diagonally at the same angle all the time. Thanks!

    EDIT: Sorry! I forgot to mention this! I'm creating a retro style project so most of the environment is made out of a tilemap. There will be some other solids like boxes and what not. I guess I'll need my creature to bounce left and right when the creature hits a tilemap wall. This is getting more complicated than I thought I would be.

  • [quote:3at8ndbj]How can I check when the creature is moving left or right so I can mirror/flip the animation?

    Since you are using Bullet behavior, how about checking for the angle of Bullet behavior of the creature by using the expression Creature.Bullet.AngleOfMotion ? If it is less than 90 OR more than 270, it is moving right. Else it is moving left.

  • [quote:1dxoa72z] How can prevent this? I want it to bounce diagonally at the same angle all the time. Thanks!

    When you set Bounce on Solid, sometimes the angle might be a bit arbitrary. If you only want certain angles, you better set angle of motion on your own when the creature collides with solid objects.

  • This is how to check the angle of motion. <img src="{SMILIES_PATH}/icon_mrgreen.gif" alt=":mrgreen:" title="Mr. Green">

    https://www.dropbox.com/s/whc6ptt0h21m1 ... .capx?dl=0

  • Thanks Gmoney! Thanks TwinBlazar! So when you said " Creature.Bullet.AngleOfMotion ? If it is less than 90 OR more than 270", is this what you meant?

    Here's a capx of what I'm trying to do using Gmoney's modified capx <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy"> I'm ok with the creature abritary changing angles when it hits a corner. I can't imagine how I'd change angle on collision with tilemap. I found a similar behavior in a tutorial and tried to modify the code to fit my character. It almost works. My math sucks. It flips direction when it collides with the top or bottom solids, not the left and right solids.

    https://www.dropbox.com/s/az5f6inrk0nm7 ... .capx?dl=0

  • your else is in the wrong position and you don't need another for loop after the else. Else should be in the for loop. It should be like this:

    for each object,

    • if object.bullet.angleofmotion < 90 OR object.bullet.angleofmotion > 270 : Object set mirrored.
    • Else Object set not mirrored.

    EDIT: FYI, I haven't checked your capx yet cuz I am on mobile outside.

    Perhaps somebody here could give him a hand for now?

  • It seems to be working. <img src="{SMILIES_PATH}/icon_mrgreen.gif" alt=":mrgreen:" title="Mr. Green">

    https://www.dropbox.com/s/fbitk47n9vs85 ... .capx?dl=0

    I found 'mirroring an object via obj_gravity.X > (some X doordinates)' method didn't work, because the object had a bullet behavior.

    So I added barriers to overlap and manually mirror the obj_gravity.. and it worked. <img src="{SMILIES_PATH}/icon_mrgreen.gif" alt=":mrgreen:" title="Mr. Green">

    In a game, I would make those barriers invisible and maybe shrink their width to 1.

    After that I'd line them up against the wall to give players an illusion of flying objects bouncing off the wall, and automatically changing where they're facing.

  • Gmoney Thanks for the suggestions. Adding barriers is definitely something I could do to get my desired effect. The problem is though that not even room is going to be a perfect square or rectangle like the capx example. There will be boxes, floating solids, elevated grounds, sloped tilemaps and more. I can't go through every room and place a barrier on every wall surface. I mean, I can...but I don't want to That's why I was trying to get the math correct on this.

    @TwinBlazar Maybe because I'm using a bullet behavior, I'm shooting myself in the foot like Gmoney says? Maybe I should figure out a way to do this using the 8 way direction behavior and a sine wave? Would that be another way to create this simple Metroid enemy "AI" pattern/behavior? I edited the code like you said and I still can't get the bullet creature to behave similarly to the original Metroid creature above.

  • Fixed. <img src="{SMILIES_PATH}/icon_mrgreen.gif" alt=":mrgreen:" title="Mr. Green">

    Now the enemy objects can bounce off and face the correct direction without having to overlap the barriers.

    What I've done was, I've created another object named 'Player' and pinned 'obj_gravity' on it.

    (Oh, I picked a 'Position only' setting for the pin, by the way.)

    Then I made the obj_gravity to face the correct direction upon encountering a situation of 'between 90 and 270' or 'not between 90 and 270' -- of Bullet Angle of Motion.

    This was one tough one to resolve. <img src="{SMILIES_PATH}/icon_mrgreen.gif" alt=":mrgreen:" title="Mr. Green">

    https://www.dropbox.com/s/479t5rl9658vh ... .capx?dl=0

  • I'm so sorry Gmoney! I've made a mistake! I wasn't very clear to begin with, my fault. So I'm making a retro pixel art game and the environments are made mostly entirely from tilemaps. I forgot the mention that and I thought that the code we'd brainstorm on here would probably be applicable as "On collision with Tilemap" somehow. Obviously, that's not the case here and would need entirely different code taking into account the tilemap . Sorry for your trouble !

    I guess I would need some sort of code that says Creature:On Collision with Tilemap and specifically checks if it's hitting the "side" of an individual tile on the tilemap? Is that something that's possible to do? I think I need to use some sort of bounding box expression to check if the player collides with side wall, yes? I just had a thought but I'm not sure if it'll work. Need to actually try it. I can give the creature an 8 way direction behavior(or platform behavior) and when the creature is left or right from a wall, mirror the animation?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'm so sorry Gmoney! I've made a mistake! I wasn't very clear to begin with, my fault. So I'm making a retro pixel art game and the environments are made mostly entirely from tilemaps. I forgot the mention that and I thought that the code we'd brainstorm on here would probably be applicable as "On collision with Tilemap" somehow. Obviously, that's not the case here and would need entirely different code taking into account the tilemap . Sorry for your trouble <img src="{SMILIES_PATH}/icon_redface.gif" alt=":oops:" title="Embarrassed">!

    I guess I would need some sort of code that says Creature:On Collision with Tilemap and specifically checks if it's hitting the "side" of an individual tile on the tilemap? Is that something that's possible to do? I think I need to use some sort of bounding box expression to check if the player collides with side wall, yes? I just had a thought but I'm not sure if it'll work. Need to actually try it. I can give the creature an 8 way direction behavior(or platform behavior) and when the creature is left or right from a wall, mirror the animation?

    Oh, no problem at all, heyguy. <img src="{SMILIES_PATH}/icon_mrgreen.gif" alt=":mrgreen:" title="Mr. Green">

    Tilemap can be solid, so it won't be a problem using this code.

    I tested it with a tilemap based wall, and it seems to be working.

    But occasionally the bullet's bounce pattern broke and the object seemed to have bounced around with a mind of its own. <img src="{SMILIES_PATH}/icon_e_confused.gif" alt=":?" title="Confused">

    https://www.dropbox.com/s/x5rktv3kkbt4h ... .capx?dl=0

    I guess I would need some sort of code that says Creature:On Collision with Tilemap and specifically checks if it's hitting the "side" of an individual tile on the tilemap? Is that something that's possible to do? I think I need to use some sort of bounding box expression to check if the player collides with side wall, yes?

    Yes, that's possible 100%.

    Please look into the capx file I made above. The file has two tilemaps; Tilemap and Tilemap2.

    Notice the bounce is only affecting Tilemap2 as per events, meaning the ceiling and the floor can have Tilemap1 with a solid behavior.

    The objects will still bounce, but they'll only turn their faces around when they're hitting the side walls (Tilemap2).

    I can give the creature an 8 way direction behavior(or platform behavior) and when the creature is left or right from a wall, mirror the animation?

    I'll experiment with that as well, because bullet's unpredictable bounce is randomly breaking the bounce patterns.

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