smcameron / space-nerds-in-space

Multi-player spaceship bridge simulator game. Captain your starship through adventures with your friends. See https://smcameron.github.io/space-nerds-in-space
GNU General Public License v2.0
724 stars 73 forks source link

Moving objects on demon screen doesn't always work right #319

Closed smcameron closed 2 years ago

smcameron commented 2 years ago

Sometimes when you select say, the player's ship, and try to move it someplace else, it's as if there's some large offset added to the position.

smcameron commented 2 years ago

The reason the movement isn't working correctly is because selection isn't working properly. What's happening is I think, as follows:

  1. You attempt to select something. It seems like you miss, and nothing is selected, but you actually do select something ... something else, something wrong, in another location.
  2. You again attempt to select the thing. This time it works. Now you have unknowingly have 2 things selected.
  3. You hit the move button to bring the selected object(s) to the current camera location.
  4. When you have multiple objects selected, the average location of all selected objects is calculated and the offset from this average location to the camera is calculated and added to all selected objects.
  5. Since you unknowingly had some additional random objects selected, the calculated offset is "incorrect" from your point of view, and the movement is wrong.

What's really going wrong is that somehow, wrong objects are being selected. I don't yet know why this is the case, but suspect something to do with entity's sx, sy fields, or object id / user data fields.

smcameron commented 2 years ago

I think I found a big clue. in add_entity(), sx, sy, and onscreen fields are not initialized. In show_demon_3d(), where object selection happens, it's checking these fields. These fields are set by render_entities(). But render_entities is only called after checking the entity's sx, sy vs. mouse position. So it's using old sx, sy from the previous rendered frame, and it only happens to sometimes work because the allocation of entities mostly happens the same way from one frame to the next.

The fix should be, I think, to move the rendering before the mouse checks, and initialize sx, sy, and onscreen fields in add_entity.

smcameron commented 2 years ago

Fixed by: