I didn't find a flexible enough line of sight / vision plugin for RMMV so I'm working on a system for a game that has Character version accessible via script for RMMZ since there doesn't really appear to be visions scripts that can be affected by lighting, obstacles, stealth behavior etc. I may also augment with sound detection as well since they can use similar concepts.
Some of the ideas I had were to hide / show character vision so you could for example equip a ring that lets you see your enemy's line of site, if it is dark and you walk outside of the radius of a light, if you are sneaking around etc.
Here is the first pass at it, I'll try to do daily updates:
Ok I got vision following the direction the characters are facing. Next is the actual collision detection of "seeing" the character
Here is an update,
I figured out how to bind a collision rectangle shaped sprite on the main player, just not sure it the right area to do so...
SceneManager.onSceneStart
Is the even I check if the player has the visionCollision object attached and if it does not, i create a new instance to attach. It is not an expensive task to re-attach if it gets lost but I wasn't sure if this was the right event to do this in. seems ok though.
This part was easy to set up and I completed it on Friday. You can see it in the sample with the red box around the player, which is resizeable, I just kept it 48x48 for now until I get the collision detection working.
Which leads me to the challenge I've been trying to figure out... I am actually rotating the sprites for the vision because there are cases where I was smooth turning and it can easily be done via sprite rotation. the problem is the collision detection for a rotated rectangle (and polygon) is super complex for my rusty Highschool/college geometry and trig brain so I am research different techniquest on how to determine a collision / point intersect and it deals with sin and cosin functions, which I have not had to do for like 20 years.
I was hoping to use an RMMZ collision detection behavior but it doesnt seem possible the way it is built. I am open to ideas and thoughts from others though
I think I have it figured out... I'm stuck on some of the Maths for rotations as my vector generation is off a bit... I need some help generating a debug graphics box and drawing it on the screen for the actual generated vectors for collision detection... I am so darn close on this
Ok new update... I got the actual verticies debugging and found out my vision is all jacked up... I need to figure out the shape and how to set a center point on it to rotate properly
Holy crap I spent waaaay to much time trying to figure out why it was acting weird
Closer... I'm trying to figure out how to shift the pivot point on the darn hit box... I have been able to figure it out on the display sprite but not the hit box yet 🙁
The collision detection lib I am using, I had to force a vertice update when I was debugging through it so here is the new code
createVisionDebugBitmap() {
if(this.IR_debug_sprite) {
SceneManager._scene._spriteset.removeChild(this.IR_debug_sprite);
}
let gr = new PIXI.Graphics();
gr.beginFill(0xFF0000);
let verts = this.shape.vertices;
//gr.drawRect(0, 0, this.shape.AABB[2] - this.shape.AABB[0], this.shape.AABB[3] - this.shape.AABB[1]);
gr.lineStyle(1, 0xFF0000, 1);
gr.moveTo(0, 0);
gr.lineTo(verts[2] - verts[0],verts[3] - verts[1]);
gr.lineTo(verts[4] - verts[0], verts[5] - verts[1]);
gr.lineTo(verts[6] - verts[0], verts[7] - verts[1]);
gr.endFill();
let visionBitmap = Graphics.app.renderer.generateTexture(gr);
gr.destroy();
visionBitmap.addLoadListener = function(listner) {
listner(this);
};
this.IR_debug_sprite = new PIXI.Sprite(visionBitmap);
this.IR_debug_sprite.opacity = 50;
this.IR_debug_sprite.x = this.shape.AABB[0];
this.IR_debug_sprite.y = this.shape.AABB[1];
SceneManager._scene._spriteset.addChild(this.IR_debug_sprite);
}
Ok so the math started to get a little too crazy and, my rule of thumb in coding is, "if it get complicated fast, its probably the wrong approach", so I shifted thinking... what if I double the size of the field of view, i'll get the same result AND I could ignore the rear side... this seems to be working on the way i want to and the math is greatly simplified. I'm working on ignoring the back half, then i'll convert this approach to a circle and change the Field of View to a configurable sliver of a circle (cone of view) then see
This looks quite neat. Would be useful for a stealh game or minigame, or even having on-screen battlers who chase you. Good luck with the dev and I'll definitely kep my eye on this!
That is a great development. You could make a two way shooter player to npc and npc to player.