This site requires JavaScript, please enable it in your browser!
Greenfoot back
Talrasha
Talrasha wrote ...

2021/4/3

Enemies using walls to bounce shots

Talrasha Talrasha

2021/4/3

#
Hello. I am trying to recreate my own little version of a game called "Panzerkiste" from the game gamecollection "WiiPlay". Here you can find some gameplay: "https://www.youtube.com/watch?v=XZPRgZEKzDI" .I have problems developing an AI that recognices how and when to shoot a projectile at the player by bouncing of a wall located on the map. I plan on having more Levels in the game. So I try to hardcode as less as possible. I completely miss a clue here in order to continue. Thanks for your help!
danpost danpost

2021/4/4

#
Three things would need to be true for there to be a clean bounce shot at the player. (1) both the player and the shooter would have to be on the same side of the wall AND the edge of the wall to bounce off of; (2) that same edge would need to extend to the bouncing point (use ratio of distances to edge to determine other coordinate of bounce point); (3) path between bounce point and both actors must be unobstructed.
Hull Hull

2021/4/5

#
This would require you to calculate "bounce points" in dependence of constantly changing entities. I bet there are some toolkits out there that'd do it for you, though I don't know of any. So I came up with this. If you don't care about the mathematics behind it, the formulas you'll need are at the bottom under "2 Implementation".
danpost danpost

2021/4/5

#
@Hull, the summary at the end of your presentation looks exactly like what I gave above -- except for the direct line of sight part (which was rather off-topic for this discussion). I coded this to find the bounce coordinates off a vertical surface (wall): ("wf" is wall face or edge of wall))
// int bx = wf.getX();   OR  = wall.getX() +/- wall.getImage().getWidth()/2;  x-coordinate of bounce point
int px = player.getX(); // x-coordinate of player
int wx = wall.getX(); // x-coordinate of wall
int ex = this.getX();  // x-coordinate of enemy
int dpf = bx-px; // length of horizontal (distance) from player to face-line of wall
int def = bx-ex; // length of horizontal from this enemy to face-line of wall
int dpw = wx-px; // length of horizontal from player to center vertical of wall
int dew = wx-ex; // length of horizontal from enemy to center vertical of wall
if (dpw*dew < 0 || dpf*def < 0 || dpw*dpf < 0 || dew*def < 0) continue; // ensuring both actors are on same side of wall face and not behind wall
int py = player.getY(); // y-coordinate of player
int ey = this.getY(); // y-coordinate of enemy
int totV = py-ey; // total vertical distance between actor
int totH = Math.abs(def+dpf); // total horizontal distance shot is to travel between actors
// totV times fractional part of totH require from enemy to bounce point gives vertical distance to bounce point from enemy
int by = ey+totV*Math.abs(def)/totH; // y-coordinate of bounce point
I tried to break the lines down to allow better documentation. This does not include checking if wall edge extends to bounce point along face line. The "continue;" on line 9 is from me having this code in a loop checking all open wall faces.
Hull Hull

2021/4/7

#
danpost wrote...
@Hull, the summary at the end of your presentation looks exactly like what I gave above -- except for the direct line of sight part (which was rather off-topic for this discussion).
Conceptionally, it is the same, yeah. The direct line of sight part is rather obvious: there is no point bouncing off a wall if you can just directly shoot at the enemy - usually anyway. It doesn't matter to the topic of bounce points itself though, that's true. The main reason I made that document is because, after approaching the issue myself, given I never did so before, I thought the mathematics and concepts behind it are quite interesting (and not that easy either). I thought it might be useful to have an approach for the calculation of bounce points. Your code is a good, comprehensible implementation of the P(wall(x)|y) formula. With that, one can calculate everything else quite easily - so that should help!
danpost danpost

2021/4/11

#
Hull wrote...
Your code is a good, comprehensible implementation of the P(wall(x)|y) formula.
Maybe, but I found that bouncing off opposite side of wall was not being caught (as I first thought) with line 9. Better is:
if (dpw*dew < 0 || dpf*def < 0 || Math.abs(dew) < Math.abs(def)) continue;
You need to login to post a reply.