javascript - Collision detection on chip8 emulator -
i made chip8 emulator, ran problems detecting collision when drawing. chip8 draws onto screen xor'ing individual pixels onto screen, , setting flag if pixel turned off. code follows:
_dxyn: function(x, y, n) { console.log("_dxyn"); (var = 0; < n; i++) { (var j = 0; j < 8; j++) { var bitbefore = graphics[(i + cpu.registers.v[y]) * 64 + j + cpu.registers.v[x]]; var bitafter = bitbefore ^ cpu.getbits(memory[cpu.registers.i + i])[j]; graphics[(i + cpu.registers.v[y]) * 64 + j + cpu.registers.v[x]] = bitafter; if ((bitbefore != bitafter) && bitbefore == 1) cpu.registers.v[0xf] = 0x1; else cpu.registers.v[0xf] = 0x0; } } }
graphics
1-dimensional array of ints, each corresponding pixel on screen. pixel on if corresponding integer in array 1, , off it 0. cpu
object contains methods, including one, registers. x
opcode parameter register contains x coordinate of sprite draw, , y
opcode parameter register y coordinate. i
register location in memory read from. cpu.getbits
function follows:
getbits: function(opcode) { var bits = []; (var = 0; < 8; i++) { bits.push((opcode >> i) & 1) } return bits.reverse(); }
the complete code can found on github here: https://github.com/ichub/chip8 in chip8cpu.js implementation.
i found website : http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter explains in detail how write chip-8 interpreter. , wikipedia aticle on chip-8 think routine should following:
_dxyn: function(x, y, n) { console.log("_dxyn"); cpu.registers.v[0xf] = 0x0; (var = 0; < n; i++) { // rows of sprite (var j = 0; j < 8; j++) { var bitbefore = graphics[(i + cpu.registers.v[y]) * 64 + j + cpu.registers.v[x]]; var bitafter = bitbefore ^ cpu.getbits(memory[cpu.registers.i + i])[j]; graphics[(i + cpu.registers.v[y]) * 64 + j + cpu.registers.v[x]] = bitafter; if ((bitbefore != bitafter) && bitbefore == 1) cpu.registers.v[0xf] = 0x1; } } }
in routine vf cleared @ each pixel if there no collision value depends solely on last pixel drawn. should clear vf before starting draw, , set when there collision.
Comments
Post a Comment