make objects seem on the map at a sure distance based mostly on scale?


Mode7 – Sprites on Display screen / Pseudo 3D

I am attempting to recreate this technique with JAVASCRIPT:
https://github.com/vinibiavatti1/Mode7/blob/grasp/src/mode7/FlatModeSeven.java

The end result was this:

var pal = ["880015","5E000E","B7001C"];
var pTexture = [
0,0,0,0,1,0,0,0,
0,0,0,0,1,0,0,0,
2,2,2,2,1,2,2,2,
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,
1,2,2,2,2,2,2,2,
1,1,1,1,1,1,1,1
];
var textureW = 8;
var textureH = 8;
var ctx, canvasW, canvasH, centerH;

var qPI = 0.7853981633974483; // 45 levels
var p_angle = qPI*7; // FaceUP in Sport

var scaleX, scaleY, scaleLmt, camX, camY;

var management = {};

perform Predominant() {
    // Preliminary Scale
    scaleX = 1;
    scaleY = 1;
    scaleLmt = scaleX*300; // Limiter for testing
    
    camX = 15; //(textureW*2)+8;
    camY = 15; //-((textureH*2)+8);
    
    var canvas = doc.getElementById("display");
    canvasW = canvas.width;
    canvasH = canvas.peak;
    centerH = canvasH/2;
    ctx = canvas.getContext("2nd");
    ctx.fillStyle = "yellow"; //Background shade
    ctx.fillRect(0, 0, canvasW, canvasH); // Draw background    

    doc.physique.addEventListener("keydown", perform (e) {
        management[e.keyCode] = 1;
    });

    doc.physique.addEventListener("keyup", perform (e) {
        management[e.keyCode] = 0;
    });
    
    doc.physique.addEventListener('wheel', perform(e){
        if (e.deltaY < 0) {
            scaleX += 0.2;
            scaleY += 0.2;
        }
        if (e.deltaY > 0) {
            scaleX -= 0.2;
            scaleY -= 0.2;
        }
    });
    
    setInterval(Render,60);
}

perform Render() {
    var p, y2, x2;
    var cos = Math.cos(p_angle);
    var sin = Math.sin(p_angle);
    var z = 1;
    
    if (scaleX < 1) {
        scaleX = 1;
    }
    if (scaleY < 1) {
        scaleY = 1;
    }
    if (scaleX > scaleLmt) {
        scaleX = scaleLmt;
    }
    if (scaleY > scaleLmt) {
        scaleY = scaleLmt;
    }
    for (y = centerH; y < canvasH; y++) {
        for (x = 0;x < canvasW;x++) {
            y2 = ((canvasW - x) * cos - x * sin) / z;
            x2 = ((canvasW - x) * sin + x * cos) / z;
            if (z < 0) {
                x2 -= (camX/scaleX);
                y2 -= (camY/scaleY);
            } else {
                x2 += (camX/scaleX);
                y2 += (camY/scaleY);
            }
            if (y2 < 0) {
                y2 *= -1;
            }
            if (x2 < 0) {
                x2 *= -1;
            }
            y2 *= scaleY;
            x2 *= scaleX;

            x2 %= textureW;
            y2 %= textureH;
            
            p = pTexture[(Math.floor(x2) + Math.floor(y2) * textureW)];
            ctx.fillStyle = "#"+pal[p];
            ctx.fillRect(x,y, 1,1);
        }
        z++;
    }
    
    // CONTROL
    if (management["87"]) {
        /*W*/
        camX += Math.sin(p_angle + qPI) //* (scaleX*2);
        camY += Math.cos(p_angle + qPI) //* (scaleY*2);
    }
    if (management["83"]) {
        /*S*/
        camX -= Math.sin(p_angle + qPI) //* (scaleX*2);
        camY -= Math.cos(p_angle + qPI) //* (scaleY*2);
    }
    if (management["65"]) {
        /*A*/
        p_angle -= 0.1;
    }
    if (management["68"]) {
        /*D*/
        p_angle += 0.1;
    }
    if (management["38"]) {
        /*ArrowUp*/
        scaleY += 2;
    }
    if (management["40"]) {
        /*ArrowDown*/
        scaleY -= 2;
    }
    if (management["37"]) {
        /*ArrowLeft*/
        scaleX += 2;
    }
    if (management["39"]) {
        /*ArrowRight*/
        scaleX -= 2;
    }
}
window.addEventListener("load", perform(){
    Predominant()
});
<canvas id="display" width="320", peak="180"></canvas>
<pre>Ahead: W | Again: S | Rotate Left: A | Rotate Proper: D
WheelUp: Additional (Scale) | WheelDown : Nearer (Scale)
ArrowUP/ArrowDow : Additional (ScaleY)
ArrowLeft/ArrowRight : Additional (ScaleX)</pre>

Nonetheless, I can not consider a method to make objects seem on the map at a sure distance based mostly on scale.
https://www.coranac.com/tonc/textual content/mode7ex.htm

It could be one thing like this, however I am nonetheless having bother getting this to work with rotation:

/* Draw Sprites */
for (let s in sprites) {
    let deltaX = sprites[s].x - camX;
    let deltaY = sprites[s].y - camY;

    let dxx = cos / scaleX;
    let dyx = sin / scaleY;
    let dxy = -sin / scaleX;
    let dyy = cos / scaleY;
    let dx = sprites[s].x/textureW;
    let dy = sprites[s].y/textureH;

    let posX = dxx * deltaX + dyx * deltaY + dx;
    let posY = dxy * deltaX + dyy * deltaY + dy;

    ctx.fillStyle = "Blue";
    ctx.fillRect(posX,posY,16,16);
}

I’ve learn a number of tutorials about RayCasting, however nothing appears to work nicely with this methodology that I am already utilizing.
One of many ones I attempted was this:
https://github.com/catmanjan/mode-7-gdx/blob/grasp/core/src/au/com/twosquared/mode7/Mode7.java#L74

The closest resolution I discovered was this, however I do not know if I may convert one thing made in Sport Maker to pure Javascript:
https://mrlevrocks.itch.io/mode7/devlog/812974/how-to-add-billboards-to-mode-7-shader

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles