When you first set the robot position to, say, (5,5), you will set vector2D[5][5]
to R
. Then if you set the position to something like (5,6), you will set vector2D[5][6]
to R
. Now both elements [5][5]
and [5][6]
are set to R
, so the robot is in both positions.
There are a few solutions that depend on how you want to design it:
- You can store the current robot position in
matrix
and at the beginning ofrobotPosition
set that position to whatever the non-robot character is. This will clear the previous position before setting the new one. - You can clear the entire matrix at the beginning of each frame. You are calling
update
at the beginning of each frame, which attempts to resize the vectors to exactly the same size they already are - this doesn't clear it. Instead, you should do that work in the constructor, and you can turninit
into aclear
function. - If you want to use a different
matrix
for each time step, then you need to move the declaration ofmatrix m;
into thewhile
loop. Then you have one for each frame which should be cleared during construction (if you move theinit
stuff to the constructor).