- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经设法找到一个在 p5.js 中工作的好 3d 渲染引擎,现在正在研究本地移动。我试过使用 trig 将玩家移动到正确的方向和正确的数量。出于某种原因,这似乎不起作用,我真的不知道发生了什么。抱歉,评论不佳且代码有些模棱两可,其中大部分不是我的。有没有人有任何建议让这个工作?谢谢
var cam2;
var img = [];
var carousel;
var lookangle = 0;
var quadrant = 0;
var adjustedlookangle = 0;
function cameraFPS() {
this.controllable = true;
this.speed = 3;
this.sensitivity = 2;
this.friction = 0.75;
this.position = createVector(0, 0, 0);
this.velocity = createVector(0, 0, 0);
this.pan = 0; // titta vänster, höger
this.tilt = 0; // up and down
this.y = createVector(0, 1, 0);
this.x = createVector(1, 0, 0);
this.z = createVector(0, 0, 1);
this.center;
this.mouse;
this.prevMouse;
this.draw = function() {
perspective(60 / 180 * PI, width/height, 0.01, 1000);
if(!this.controllable) return;
this.mouse = createVector(mouseX, mouseY);
if (this.prevMouse == null) this.prevMouse = createVector(mouseX, mouseY);
var w = windowWidth;
var h = windowHeight;
if (this.mouse.x < 1 && (this.mouse.x - this.prevMouse.x) < 0){
this.mouse.x = w-2;
this.prevMouse.x = w-2;
}
if (this.mouse.x > w-2 && (this.mouse.x - this.prevMouse.x) > 0){
this.mouse.x = 2;
this.prevMouse.x = 2;
}
if (this.mouse.y < 1 && (this.mouse.y - this.prevMouse.y) < 0){
this.mouse.y = h-2;
this.prevMouse.y = h-2;
}
if (this.mouse.y > h-1 && (this.mouse.y - this.prevMouse.y) > 0){
this.mouse.y = 2;
this.prevMouse.y = 2;
}
this.pan = map(this.mouse.x - this.prevMouse.x, 0, width, 0, TWO_PI) * this.sensitivity;
this.tilt = map(this.mouse.y - this.prevMouse.y, 0, height, 0, PI) * this.sensitivity;
this.tilt = constrain(this.tilt, -PI/2.01, PI/2.01);
if (this.tilt == PI/2) tilt += 0.001;
this.z = createVector(cos(this.pan), tan(this.tilt), sin(this.pan));
this.z.normalize(this.z);
this.x = createVector(cos(this.pan - PI/2), 0, sin(this.pan - PI/2));
this.prevMouse = createVector(this.mouse.x, this.mouse.y);
if (keyIsDown(65)) this.velocity.add(p5.Vector.mult(this.x, this.speed));
if (keyIsDown(68)) this.velocity.sub(p5.Vector.mult(this.x, this.speed));
if (keyIsDown(87)) this.velocity.add(p5.Vector.mult(this.z, this.speed));
if (keyIsDown(83)) this.velocity.sub(p5.Vector.mult(this.z, this.speed));
if (keyIsDown(81)) this.velocity.add(p5.Vector.mult(this.y, this.speed));
if (keyIsDown(69)) this.velocity.sub(p5.Vector.mult(this.y, this.speed));
this.velocity.mult(this.friction);
this.position.add(this.velocity);
this.center = p5.Vector.add(this.position, this.z);
camera(this.position.x, this.position.y, this.position.z);
//rotateX(this.center.y);
//rotateY(this.center.x);
//rotateZ(this.center.z);
}
}
function navCamera() {
//Camera position and rotation
this.x = 0;
this.y = 0;
this.z = 0;
this.xRotation = 0;
this.yRotation = 0;
//Properties for rotating camera with mouse
this.lastMouseX = width / 2;
this.lastMouseY = height / 2;
this.beganMouseY = 0;
this.beganMouseX = 0;
this.update = function() {
this.checkInput();
//camera(this.x, this.y, this.z);
//translate(-this.x, -this.y, -this.z)
translate(0, 0, 1000);
rotateY(-PI/2 + (PI) * (this.xRotation / width));
//rotateX(-PI/2 + (PI) * (this.yRotation / height));
translate(-cam.x, -cam.y, -cam.z)
}
this.checkInput = function() {
//key input
if(keyIsDown(87)) { // W
if (quadrant == 1) {
this.x -= Math.cos(adjustedlookangle * (Math.PI / 180))
this.z -= Math.sin(adjustedlookangle * (Math.PI / 180))
} else if (quadrant == 2) {
this.z += Math.cos(adjustedlookangle * (Math.PI / 180))
this.x -= Math.sin(adjustedlookangle * (Math.PI / 180))
} else if (quadrant == 3) {
this.x -= Math.cos(adjustedlookangle * (Math.PI / 180))
this.z -= Math.sin(adjustedlookangle * (Math.PI / 180))
} else if (quadrant == 4) {
this.x -= Math.cos(adjustedlookangle * (Math.PI / 180))
this.z -= Math.sin(adjustedlookangle * (Math.PI / 180))
}
}
if(keyIsDown(83)) { //S
this.x += Math.cos(lookangle * (Math.PI / 180))
this.z += Math.sin(lookangle * (Math.PI / 180))
}
if(keyIsDown(65)) { //A
this.z += Math.cos(lookangle * (Math.PI / 180))
this.x -= Math.sin(lookangle * (Math.PI / 180))
}
if(keyIsDown(68)) { //D
this.z -= Math.cos(lookangle * (Math.PI / 180))
this.x += Math.sin(lookangle * (Math.PI / 180))
}
//Calculate mouse movement while pressed
this.xRotation = this.lastMouseX;
this.yRotation = this.lastMouseY;
if (mouseIsPressed) {
var deltaX = mouseX - this.beganMouseX;
this.xRotation += deltaX;
var deltaY = mouseY - this.beganMouseY;
this.yRotation += deltaY;
print (this.xRotation / 10)
//get lookangle in degrees
lookangle = (this.xRotation / 10 % 360)
//discern quadrant
if (lookangle < 90) {
quadrant = 1;
}else if (lookangle < 180) {
quadrant = 2;
}else if (lookangle < 270) {
quadrant = 3;
}else if (lookangle < 360) {
quadrant = 4;
}
print (quadrant)
adjustedlookangle = (lookangle - 90 * (quadrant - 1))
//to make radian (Math.PI / 180)
}
}
this.onMouseRelease = function() {
var deltaX = mouseX - this.beganMouseX;
var deltaY = mouseY - this.beganMouseY;
this.lastMouseX += deltaX;
this.lastMouseY += deltaY;
}
this.onMousePress = function() {
this.beganMouseX = mouseX;
this.beganMouseY = mouseY;
}
}
function preload() {
img.push(loadImage('assets/nature.jpg'));
img.push(loadImage('assets/nature2.jpg'));
//img.push(loadImage('assets/wall.jpg'));
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
cam = new navCamera();
cam2 = new cameraFPS();
carousel = new carousel();
}
function draw() {
background(200);
cam.update();
//cam2.draw();
carousel.update();
//texture(img[2]);
//fill(255, 255, 0);
//box(5000, 10, 5000);
}
function carousel() {
// setup 8 boxes in a caraousel
this.total = 4;
this.radius = 200;
this.update = function() {
this.centerX = 0;
this.centerY = 0;
for (var i = 0; i < this.total; i++) {
var x = (Math.cos(i / this.total * TWO_PI) * this.radius),
z = (Math.sin(i / this.total * TWO_PI) * this.radius),
y = 0;
push();
translate(x, y, z);
fill(100, 50, (255 / 8) * i);
texture(img[i % 2]);
rotateY(-QUARTER_PI * (i + 2));
box(100, 100, 100);
pop();
}
}
}
function mouseReleased() {
cam.onMouseRelease();
}
function mousePressed() {
cam.onMousePress();
}
最佳答案
不要把事情复杂化。根本不需要变量 lookangle
、quadrant
和 adjustedlookangle
。
您需要位置和方向的属性:
function navCamera() {
//Camera position and rotation
this.x = 0;
this.y = 0;
this.z = 0;
this.xRotation = 0; // rotation around x axis in radians
this.yRotation = 0; // rotation around y axis in radians
// [...]
以及鼠标移动开始的属性和拖动鼠标时的当前旋转 Angular :
function navCamera() {
// [...]
this.xCurrentRotation = 0; // current (dragged) rotation around x axis in radians
this.yCurrentRotation = 0; // current (dragged) rotation around y axis in radians
this.beganMouseX = 0; // x start of mouse drag
this.beganMouseY = 0; // y start of mouse drag
// [...]
}
使用参数通过camera
设置 View .例如:
function navCamera() {
// [...]
this.update = function() {
this.checkInput();
let sinay = Math.sin(this.yCurrentRotation);
let cosay = Math.cos(this.yCurrentRotation);
let sinax = Math.sin(this.xCurrentRotation);
let cosax = Math.cos(this.xCurrentRotation);
resetMatrix();
camera(this.x, this.y, this.z, cosax*sinay+this.x, sinax+this.y, cosax*cosay+this.z, 0.0, 1.0, 0.0);
}
// [...]
}
鼠标事件分别更新this.xRotation
this.yRotation
:
function navCamera() {
// [...]
this.onMousePress = function() {
this.beganMouseX = mouseX;
this.beganMouseY = mouseY;
}
this.onMouseRelease = function() {
this.yRotation -= PI/2 * (mouseX - this.beganMouseX) / width;
this.xRotation -= PI/2 * (mouseY - this.beganMouseY) / height;
}
// [...]
}
在 navCamera
中设置当前 Angular 和相机位置:
function navCamera() {
// [...]
this.checkInput = function() {
this.yCurrentRotation = this.yRotation;
this.xCurrentRotation = this.xRotation;
if (mouseIsPressed) {
this.yCurrentRotation -= PI/2 * (mouseX - this.beganMouseX) / width;
this.xCurrentRotation -= PI/2 * (mouseY - this.beganMouseY) / height;
}
let sina = Math.sin(this.yCurrentRotation);
let cosa = Math.cos(this.yCurrentRotation);
//key input
if(keyIsDown(87)) { // W
this.x += sina; this.z += cosa;
}
if(keyIsDown(83)) { //S
this.x -= sina; this.z -= cosa;
}
if(keyIsDown(65)) { //A
this.z -= sina; this.x += cosa;
}
if(keyIsDown(68)) { //D
this.z += sina; this.x -= cosa;
}
}
// [...]
}
看例子:
var img = [];
var carousel;
function navCamera() {
//Camera position and rotation
this.x = 0;
this.y = 0;
this.z = 0;
this.xRotation = 0; // rotation around x axis in radians
this.yRotation = 0; // rotation around y axis in radians
this.xCurrentRotation = 0; // current (dragged) rotation around x axis in radians
this.yCurrentRotation = 0; // current (dragged) rotation around y axis in radians
this.beganMouseX = 0; // x start of mouse drag
this.beganMouseY = 0; // y start of mouse drag
this.update = function() {
this.checkInput();
let sinay = Math.sin(this.yCurrentRotation);
let cosay = Math.cos(this.yCurrentRotation);
let sinax = Math.sin(this.xCurrentRotation);
let cosax = Math.cos(this.xCurrentRotation);
resetMatrix();
camera(this.x, this.y, this.z, cosax*sinay+this.x, sinax+this.y, cosax*cosay+this.z, 0.0, 1.0, 0.0);
}
this.checkInput = function() {
this.yCurrentRotation = this.yRotation;
this.xCurrentRotation = this.xRotation;
if (mouseIsPressed) {
this.yCurrentRotation -= PI/2 * (mouseX - this.beganMouseX) / width;
this.xCurrentRotation -= PI/2 * (mouseY - this.beganMouseY) / height;
}
let sina = Math.sin(this.yCurrentRotation);
let cosa = Math.cos(this.yCurrentRotation);
//key input
if(keyIsDown(87)) { // W
this.x += sina; this.z += cosa;
}
if(keyIsDown(83)) { //S
this.x -= sina; this.z -= cosa;
}
if(keyIsDown(65)) { //A
this.z -= sina; this.x += cosa;
}
if(keyIsDown(68)) { //D
this.z += sina; this.x -= cosa;
}
}
this.onMousePress = function() {
this.beganMouseX = mouseX;
this.beganMouseY = mouseY;
}
this.onMouseRelease = function() {
this.yRotation -= PI/2 * (mouseX - this.beganMouseX) / width;
this.xRotation -= PI/2 * (mouseY - this.beganMouseY) / height;
}
}
function preload() {
img.push(loadImage('https://raw.githubusercontent.com/Rabbid76/graphics-snippets/master/resource/texture/test1_texture.bmp'));
img.push(loadImage('https://raw.githubusercontent.com/Rabbid76/graphics-snippets/master/resource/texture/tree.jpg'));
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
cam = new navCamera();
carousel = new carousel();
}
function draw() {
background(200);
cam.update();
carousel.update();
}
function carousel() {
// setup 8 boxes in a caraousel
this.total = 4;
this.radius = 200;
this.update = function() {
this.centerX = 0;
this.centerY = 0;
for (var i = 0; i < this.total; i++) {
var x = (Math.cos(i / this.total * TWO_PI) * this.radius),
z = (Math.sin(i / this.total * TWO_PI) * this.radius),
y = 0;
push();
translate(x, y, z);
fill(100, 50, (255 / 8) * i);
texture(img[i % 2]);
rotateY(-QUARTER_PI * (i + 2));
box(100, 100, 100);
pop();
}
}
}
function mouseReleased() {
cam.onMouseRelease();
}
function mousePressed() {
cam.onMousePress();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
关于javascript - 建立地方运动系统的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58128118/
这是否可以检测到手机何时像图片上那样移动?这个在android中叫什么名字? 是否有处理此类事情的事件?一年前,我看到一个带指南针的应用程序,它可以实时运行。 谢谢! 最佳答案 我希望这段代码能有所帮
我正在为这个而撕扯我的头发。出于某种奇怪的原因,我找不到/想不出如何在 SFML 和/或 SDL 中移动 Sprite 。我看过的这两个库的教程对此一无所知;所以我认为它更像是 C++ 的东西而不是库
所以我最近一直在研究 DirectX11,但我对它还是很陌生。我现在正在尝试通过翻译来移动一些东西,这就是我所拥有的。我一直在阅读 Frank D Luna 关于 DirectX11 的书,他提供了一
我一直在尝试为绘图元素制作动画,但没有成功。我可以对导入的图像进行动画处理,但是当我尝试对 pygame 生成的绘图进行动画处理时,它们仍然是静态的。 编辑:“动画”是指“移动”。就像使圆在 x 和
好吧,我已经尝试 Java 几个星期了,遵循类和在线教程。我做了一个简单的游戏,其中方 block 落向屏幕底部,而玩家控制一个小球,仅在 x 轴上移动并尝试避开它们。 我遇到的问题是方 block
我的 python 代码遇到一些问题,我正在制作蛇的一个版本,我的问题涉及蛇本身的运动。我已经得到了工作正常的方向,我只需要做到这一点,以便蛇继续沿着通过按键告诉它的方向移动,我还需要使它成为一个 b
那是我的代码。 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationC
我正在构建这个用于慢跑的 Android 应用程序,它应该测量用户从某个时间点开始的距离。 我对使用 GPS 选项(经度、纬度)不感兴趣,所以如果没有 GPS 选项并且只使用 android 传感器,
什么是自律?网上有这样一个回答:“最健康的自律是早睡,最实用的自律是运动,最丰盈内心的自律是读书。”早睡养神,运动养身,读书养脑。坚持把这三件事做好,你就已经超越了很多人。 1 每天早点睡 你有没有过
考虑下面这行 Lisp 代码: (some-function 7 8 | 9) ;; some comment. note the extra indentation 该点位于“8”和
在 Vim 中,如何移动到 xml 文件中的父/表亲标签?我正在寻找类似的东西: vatat " create a selection for second parent tag with all c
用 Dart 做这样的 Canvas 运动的最佳方法是什么? http://jsfiddle.net/loktar/dMYvG/ 我正在尝试使 Canvas 运动平稳,并想看看Dart可以做什么。 还
我试图让一个物体在固定的时间段内沿着圆形路径移动。 这个应用程序是一个“平滑运动”时钟。 因此,我不想每次 .getSeconds() 更新时将位置移动到固定坐标,而是想使用 ( .getSecond
我正在尝试创建一个简单的动画,其中一系列气泡围绕中心点旋转。我有一个动画版本,其中气泡在开始旋转之前从中心点扩散,效果很好,但是一旦我单击其中一个图像(引发动画),屏幕就会卡住一会儿,然后气泡出现在他
不久前我开始学习java作为一种爱好,因为我想制作一个小游戏。我学习了 Java 基础知识,并决定尝试解决游戏开发问题。我的 JFrame 和一切都很好,从技术上讲我没有错误,但我的小矩形家伙不会在屏
我在制作台球游戏时遇到问题,当我模拟击球时,我需要球使用react,程序是这样工作的,您单击击球的方向和力量,然后单击开始, go按钮位于创建标签的GUI类中,该按钮调用我的主类中的一个方法来接收参数
我以前在 2d 项目中使用过类似的东西来移动并且它总是有效。我现在正在使用它,它为我提供了一些输出的正确角度和错误的角度。我认为我的触发器中有一些错误,我弄错了。不过,我已经检查了大约一百万次。 Pr
我的 OpenGL 应用程序有问题,您可以在 this gif 中清楚地看到.基本上我想朝光标指向的方向移动,但这并没有发生,而是“前进”方向保持不变。 例如,如果我转身 180° 并按“w”向前走,
因此,我再次开始使用 C++ 编程并尝试使用 OpenGL。目前我有一个基本的应用程序,我只想用键移动相机。我读了很多关于这个的文章,但我仍然对运动有问题,我猜是因为即使只有一点点,但它们与我的程序不
我在 Android OpenGL 中有一个 Sprite 。这个 Sprite (一只小甲虫)总是向前移动,我使用: sprite.setPosition(posX, posY); 现在我有一个旋转
我是一名优秀的程序员,十分优秀!