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

Get help using Construct 2

Post » Sun Sep 04, 2016 12:29 pm

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.

Image

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?

Image

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.
Last edited by heyguy on Sun Sep 04, 2016 10:24 pm, edited 2 times in total.
B
28
S
14
G
21
Posts: 179
Reputation: 13,548

Post » Sun Sep 04, 2016 1:41 pm

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.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Sun Sep 04, 2016 1:51 pm

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.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Sun Sep 04, 2016 2:23 pm

This is how to check the angle of motion. :mrgreen:

https://www.dropbox.com/s/whc6ptt0h21m1 ... .capx?dl=0
B
11
S
6
G
15
Posts: 256
Reputation: 9,207

Post » Sun Sep 04, 2016 3:30 pm

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?

Image

Here's a capx of what I'm trying to do using Gmoney's modified capx :D 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
B
28
S
14
G
21
Posts: 179
Reputation: 13,548

Post » Sun Sep 04, 2016 4:27 pm

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?
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Sun Sep 04, 2016 4:43 pm

It seems to be working. :mrgreen:

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. :mrgreen:

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.
B
11
S
6
G
15
Posts: 256
Reputation: 9,207

Post » Sun Sep 04, 2016 5:30 pm

@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 :D 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.
B
28
S
14
G
21
Posts: 179
Reputation: 13,548

Post » Sun Sep 04, 2016 7:05 pm

Fixed. :mrgreen:

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. :mrgreen:

https://www.dropbox.com/s/479t5rl9658vh ... .capx?dl=0
B
11
S
6
G
15
Posts: 256
Reputation: 9,207

Post » Sun Sep 04, 2016 9:54 pm

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 :oops:!

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?
B
28
S
14
G
21
Posts: 179
Reputation: 13,548

Next

Return to How do I....?

Who is online

Users browsing this forum: SputnikCZ and 3 guests