javascript - 在翻译的游戏世界中获取鼠标位置:processing.js
问题描述
我正在 Processing.js 中制作游戏。当我放大我的游戏世界时,鼠标位置是游戏世界明显偏移。如何获得它,以便鼠标位置相对于游戏世界中的位置,而不是使用 scale(x,y) 相对于左上角缩放后屏幕的左上角(0,0)?到目前为止,这是我的代码,包括移动和缩放。我所需要的只是找到游戏中鼠标位置的方法,这样我就可以单击以在地图中的任何位置放置圆圈。
pushMatrix();
translate(-pos.x*zoom+width/2, -pos.y*zoom+height/2);
scale(zoom);
for(var i = 0; i<circles.length; i++){
circles[i].update();
}
if(clicked){
circles.push(new Circle(mouseX-width/2,mouseY-width/2));//should i have this inside the translation? Outside? coordinates so far only work when zoom is 1 and pos.x and pos.y arent changed
}
popMatrix();//end of translation
解决方案
我想到了!这是根据鼠标位置在缩放和平移的世界中添加圆圈的代码:
circles.push(new Circle((pos.x*zoom+mouseX-width/2)/zoom,(pos.y*zoom+mouseY-height/2)/zoom))
本质上,我将 x 乘以缩放和 y 乘以缩放,并将鼠标 x 和 y 除以缩放添加到先前的结果除以缩放。最后,我减去 width/2,/zoom 以使所有内容居中。希望这可以帮助!
推荐阅读
- angular - 在代码停止后导航页面时,不应显示网页
- c# - EWS - 按全名获取文件夹
- java - RegEx - 在 HTML-Anchor 元素中的组之间未捕获的任何内容
- python - 我在代码中做错了什么,通过 Flask 应用程序向 MySQL 提交表单值?
- c# - 为什么我的 android xamarin 应用程序在权限被拒绝时会自动关闭?
- lua - 为什么雾的颜色不会根据 Roblox Studio 中的 ClockTime 变化?
- oauth-2.0 - WSO2:JWTConfiguration 不适用于多租户
- javascript - 在不设置默认值的情况下评估变量
- java - Android 10,huawei mate30,从app跳转到APN设置页面,没有数据!
- javascript - 车轮事件触发多次反应