humaidk2 Posted January 6, 2017 Share Posted January 6, 2017 I'm wondering if there's a way to manually check for collisions. I'm making VR first person Pacman game for a project and I need the pacman to eat all the pellets in the direction the user is facing. I got the game working with gravity and collisions but the game was laggy and slow due to collisions with the ground and the pellet at the same time. This was my original function: camera.onCollide = function(collidedMesh) { if (arr[collidedMesh.uniqueId] !== undefined) { //console.log('gone'); arr[collidedMesh.uniqueId].dispose(); pellet.play(); score++; if (canvas2 === undefined) { canvas2 = create(scene, score); } else { canvas2.levelVisible = false; canvas2 = create(scene, score); } } }; Now I'm trying to move the camera as it renders but the collisions completely turn off, I'm wondering if there's a way to manually check/enforce for collisions? var moveCamera = function() { var camSpeed = 0.1; var forwards = new BABYLON.Vector3(parseFloat(Math.sin(camera.rotation.y)) * camSpeed, 0, parseFloat(Math.cos(camera.rotation.y)) * camSpeed); camera.position = camera.position.add(forwards); }; scene.registerBeforeRender(function() { moveCamera(); }); mazeRunner.jsx Quote Link to comment Share on other sites More sharing options...
jschwuch Posted January 6, 2017 Share Posted January 6, 2017 I think what's really slowing you down here is the creation of a new ScreenSpaceCanvas2d in your create function every time you have a hit. You should only create one ScreenSpaceCanvas2D at startup and modify it when score changes. humaidk2 1 Quote Link to comment Share on other sites More sharing options...
Raggar Posted January 6, 2017 Share Posted January 6, 2017 Why don't you use velocities insteaf of moving the camera using units? Are you using Oimo or Cannon? I know Cannon has collisiiongroups and masks, as well as collisionresponse, so you can turn off actual physical collisiions, but still register the event. When you say laggy, does this mean that the framerate drops, or that the camera physics body simply stops, due to colliding with the pellets? humaidk2 1 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 6, 2017 Share Posted January 6, 2017 Look up AABB and vector projection humaidk2 1 Quote Link to comment Share on other sites More sharing options...
humaidk2 Posted January 6, 2017 Author Share Posted January 6, 2017 Thanks a lot for all the help. I was running out of stuff to search for and I'm new to Babylon so I really appreciate it. 10 hours ago, jschwuch said: I think what's really slowing you down here is the creation of a new ScreenSpaceCanvas2d in your create function every time you have a hit. You should only create one ScreenSpaceCanvas2D at startup and modify it when score changes. I don't think that was my main problem but it definitely needed a fix, thanks. 8 hours ago, Raggar said: Why don't you use velocities insteaf of moving the camera using units? Are you using Oimo or Cannon? I know Cannon has collisiiongroups and masks, as well as collisionresponse, so you can turn off actual physical collisiions, but still register the event. When you say laggy, does this mean that the framerate drops, or that the camera physics body simply stops, due to colliding with the pellets? I'm not using oimo or cannon, i'll start looking into them right away. When I was using the keyboard to move around, the camera body stopped when it collided with the pellets but the pellets disappeared as the camera slowed down. It's because I'm always colliding with the ground(due to gravity) and looking for collisions for the pellets.(It wasn't laggy) Now I'm trying to fix the issue by removing gravity and moving the camera in the direction it's facing using the registerBeforeRendermethod but it stops detecting collisions. Hopefully all your solutions will make it work 2 hours ago, Pryme8 said: Look up AABB and vector projection I'll look into it, thanks. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 6, 2017 Share Posted January 6, 2017 Worry about two dimensions right now while you learn how that system works. And one you have a grasp on it add a third dimension... and then to test your wits add a fourth so you can do subtime steps and narrow down to a specific point of time in the future or past where the collision state will be the most accurate... sounds scary I know but it's really not it's pretty basic algerbra and trig. ohh and kinda unrelated but related... is I got into all this math crap while my dad was at Purdue getting his masters in engineering, and I remember it specifically the moment when he took a pen and threw it across the room (we were just arguing about my homework I hated math at the point and I was like 9 or something) and looked at me and said there is a math equation that will have told you where every point on that pen was at any point in time, and that if I wanted to design video games that I would need to be able to explain that system to him at some point and how was I going to be able to do that if I won't even learn long division or what ever it was at the time. And my prepubescent mind was blown... humaidk2 1 Quote Link to comment Share on other sites More sharing options...
Raggar Posted January 6, 2017 Share Posted January 6, 2017 54 minutes ago, humaidk2 said: When I was using the keyboard to move around, the camera body stopped when it collided with the pellets but the pellets disappeared as the camera slowed down. With Cannon.js, you can set the collisionResponse to 0, thereby preventing the actual collisiion with the physics body, but still fire the 'collide' event, so you can remove the pellet, increase score etc. humaidk2 1 Quote Link to comment Share on other sites More sharing options...
humaidk2 Posted January 6, 2017 Author Share Posted January 6, 2017 Perfect, thanks for the fix. I decided that since we don't have any input for the VR that we should always move in the direction that the camera is facing and get rid off all inputs. It also keeps with the legacy of the original pacman. From what I've learned about cannon, I should be able to get a fix in a few hours but I'm gonna go over AABB first. Quote Link to comment Share on other sites More sharing options...
humaidk2 Posted January 6, 2017 Author Share Posted January 6, 2017 1 hour ago, Pryme8 said: Worry about two dimensions right now while you learn how that system works. And one you have a grasp on it add a third dimension... and then to test your wits add a fourth so you can do subtime steps and narrow down to a specific point of time in the future or past where the collision state will be the most accurate... sounds scary I know but it's really not it's pretty basic algerbra and trig. ohh and kinda unrelated but related... is I got into all this math crap while my dad was at Purdue getting his masters in engineering, and I remember it specifically the moment when he took a pen and threw it across the room (we were just arguing about my homework I hated math at the point and I was like 9 or something) and looked at me and said there is a math equation that will have told you where every point on that pen was at any point in time, and that if I wanted to design video games that I would need to be able to explain that system to him at some point and how was I going to be able to do that if I won't even learn long division or what ever it was at the time. And my prepubescent mind was blown... Oh I really didn't understand what AABB was till now but I had an idea that was similar.(I was planning on finding the intersection between the positions of the 2 objects) The problem though is that I have to look over all the collisions in that case(collision of camera with pellet, camera with block, camera, with block). I think I'm gonna go with @Raggar solution and use a physics engine (cannon). but I'll come back to AABB and vector projection if I can't get it to work. Thanks for all the help and the inspiring story. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 6, 2017 Share Posted January 6, 2017 Yeah it's really cool you can use it in additional your physics engine to help out with missed collisions and stuff. In mine and @dbawel's last project we where having cells penitrate each other's bounding space sometimes and were having the colliders in some case trap the other object so as a buffer I programmed a secondary AABB system into the renderloop before and after the physics calculations to see if any objects were in an intersect state, and if they were I used the projection vector to establish a response normal vector that gave me the direction for the shortest route out of the object and how far it was so from that I was able to instantly snap the object out of the collider mesh and then applied velocity to the response mesh in the amount of the length of the vector or its scale. When physic systems dont do what you want you make them do it. thats the beauty of a vector it will give you direction and magnitude. Quote Link to comment Share on other sites More sharing options...
humaidk2 Posted January 6, 2017 Author Share Posted January 6, 2017 Sound Amazing, I just have one more question, How do you add a physics imposter to a camera? I know it's possible because that's what it says in the docs: " An AbstractMesh will be the first choice, of course. But a Solid Particle also applies, and so does a light or certain cameras. I will show how to use an impostor on different object types in the advanced tutorial. " but I can't find how? Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 6, 2017 Share Posted January 6, 2017 Add it to a non visiable mesh that is a approximation of the cameras "shape" child the camera to that object and offset it to where you want or where you head would be, and only worry about applying physics to the non visible object. or add capsule to the camera I forget how though and move its physics body with collisions or what ever method the guys use. I always do my own movement controllers with the above method though so I can't speak on this one @jerome or someone could though I bet. humaidk2 1 Quote Link to comment Share on other sites More sharing options...
Raggar Posted January 6, 2017 Share Posted January 6, 2017 This ended up really shitty, but it shows collisionResponse in action: http://www.babylonjs-playground.com/#29TTKJ#5 humaidk2 1 Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted January 7, 2017 Share Posted January 7, 2017 Why do you need collisions with the pellets? i bet checking for position / distance against an array containing the pellets would be faster. So if distance <= pacmanRadius = eat pellet. humaidk2 1 Quote Link to comment Share on other sites More sharing options...
humaidk2 Posted January 7, 2017 Author Share Posted January 7, 2017 So I spent the day trying to use cannon and collisions but couldn't get it to work.@Raggar Thanks a lot for the help and the amazing demo, but I really need to learn more about cannon and my MVP presentation is on Monday. So I think I'm gonna use positions, AABB, and projection vectors tomorrow. Edit: I'm still using the cannon plugin to move my camera/user around so that was real helpful Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 7, 2017 Share Posted January 7, 2017 http://www.metanetsoftware.com/technique/tutorialA.html. good refresher for speration of axis calculations. I learned a lot from this years ago. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.