Camera collision in BabylonJS - babylonjs

I'm trying to make a camera not overlap with meshes. Here I reproduced what is happening https://www.babylonjs-playground.com/#4NHCRD#4 You can see that the small ball with an attached camera can see through the big ball on collide.
I've tried all tricks from here https://doc.babylonjs.com/divingDeeper/cameras/camera_collisions but no success :pensive:

You have to adjust minZ-property of the camera:
crCamera.minZ = 0.01

Related

Why are the transparent pixels not blending correctly in WebGL

Result of my code:
Basically, what the issue is, the transparent part of my image are not blending correctly with what is drawn before it. I know I can do a
if(alpha<=0){discard;}
in the fragment shader, the only issue is I plan on having a ton of fragments and don't want the if statement for each fragment on mobile devices.
Here is my code related to alpha, and depth testing:
var gl = canvas.getContext("webgl2",
{
antialias : false,
alpha : false,
premultipliedAlpha: false,
}
);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.GREATER);
Also, these are textured gl.POINTS I am drawing. If I change the order the two images are drawn in the buffer, the problem doesn't exist. They will be dynamically rotating during the program's runtime so this is not an option.
It's not clear what your issue is without more code but it looks like a depth test issue.
Assuming I understand correctly you're drawing 2 rectangles? If you draw the red one before the blue one then depending on how you have the depth test setup the blue one will fail the depth test when the X area is drawn.
You generally solve this by sorting what you draw, making sure to draw things further away first.
For a grid of "tiles" you can generally sort by walking the grid itself in the correct direction instead of "sorting"
On the other hand, if all of your transparency is 100% draw or not draw then discard has its advantages and you can draw front to back. The reason is because in that case drawing front to back, the pixel drawn (not discarded) by the red quad will be rejected when drawing the blue quad by the depth test. The depth test is usually optimized to happen before running the fragment shader for a certain pixel. If the depth test says the pixel will not be drawn then no reason to even run the fragment shader for that pixel, time saved. Unfortunately as soon as you have any transparency that is not 100% opaque or 100% transparent then you need to sort and draw back to front. Some of these issues are covered in this article
A few notes:
you mentioned mobile devices and you mentioned WebGL2 in your code sample. There is no WebGL2 on iOS
you said you're drawing with POINTS. The spec says only POINTS of 1 pixel in size are required. It looks like you're safe up to points of size 60 but to be safe it's generally best to draw with triangles as there are other isses with points
you might also be interested in sprites with depth

Centering Perspective Camera on two objects by panning

In Unity, I have a perspective camera, and I've got two transforms in my scene that I want the camera to perfectly center on screen. The camera will pan left/right/up/down to the appropriate location.
So far my approach has been to convert the transform positions to screen positions using Camera.WorldToScreenPoint, and taking their average to find the screen midpoint. From there, I know I want to pan the camera a certain number of units toward that midpoint. What I'm having trouble with is figuring out the formula for deciding how much to pan (or, maybe this isn't even the preferred way to determine this).
I think your approach is great. Let me expand the idea.
So this is your screen :D. Blue circle is where you want your objects to be. There are two scenarios. We will use green dots as an example of zooming scenario. Then red dots for panning scenario.
The trick is, you want to keep the dots as close as possible to circumference of blue circle.
Let's say you get red dots as your objects' screen position. You have to shift them towards the center. Let's calculate CenterOfDots. Then calculate it's difference to CenterOfBlueCircle. That's how much pan you need in screen coordinates.
So you have calculated the pan. Now you want to know how much you need to zoom. Let's say you get green dots this time. Calculate DistanceBetweenDots and compare it to DiameterOfBlueCircle. You want them to be the same. So their difference is how much zoom you need in screen coordinates.
There comes the tricky part. Now you know how much to pan and zoom in screen space. But you need to move the camera in world space. Trying to solve it using geometry magic is fine. But I hate headache :D
So instead, I would iteratively shift my camera using the data I calculated above. Just shift the camera in it's local x-y axes towards HowMuchPan, multiplied by a manually given coefficient PanSpeed. This will give a smooth transition to the camera. Same is for the zoom. This time you shift the camera in it's local z axis using HowMuchZoom multiplied by your manually given coefficient ZoomSpeed.
Hope it helps. Have fun :)
i figured out the mathy approach!
for panning, you want to figure out the average screen position of your objects (i.e. the middle). then you want to generate a couple world points against an arbitrary plane some distance away from the camera. the difference between these points is how much to pan the camera
center=Camera.ScreenToWorldPoint(Screen.width*0.5f, Screen.height*0.5f, 10f)
mid=Camera.ScreenToWorldPoint(averageScreenPoint.x, averageScreenPoint.y, 10f)
Camera.transform.Translate(mid-center)
zooming is a bit more complicated, but very similar to the panning approach. you want to use Camera.ScreenToWorldPoint against an arbitrary plane, but you want to do this for 4 points, which will help you figure out a scale to apply to your camera's z position. psuedocode -
screenMin = Camera.ScreenToWorldPoint(0f,0f,10f);
screenMax = Camera.ScreenToWorldPoint(Screen.width,Screen.height,10f);
objMin = Camera.ScreenToWorldPoint(screenPosMin.x, screenPosMin.y, 10f);
objMax = Camera.ScreenToWorldPoint(screenPosMax.x, screenPosMax.y, 10f);
screenDiff = screenMax-screenMin;
objDiff = objMax-objMin;
Vector3 scale = new Vector3(objDiff.x/screenDiff.x, objDiff.y/screenDiff.y, 0f);
ratio = scale.x < scale.y ? scale.y : scale.x;// pick the one that best puts fits on screen.
Camera.localPosition.z = Mathf.Min(ZoomMin, Camera.localPosition.z*ratio);

determine rectangle rotation point

I would like to know how to compute rotation components of a rectangle in space according to four given points in a projection plane.
Hard to depict in a single sentence, thus I explain my needs.
I have a 3D world viewed from a static camera (located in <0,0,0>).
I have a known rectangular shape (an picture, actually) That I want to place in that space.
I only can define points (up to four) in a spherical/rectangular referencial (camera looking at <0°,0°> (sph) or <0,0,1000> (rect)).
I considere the given polygon to be my rectangle shape rotated (rX,rY,rZ). 3 points are supposed to be enough, 4 points should be too constraintfull. I'm not sure for now.
I want to determine rX, rY and rZ, the rectangle rotation about its center.
--- My first attempt at solving this constrint problem was to fix the first point: given spherical coordinates, I "project" this point onto a camera-facing plane at z=1000. Quite easy, this give me a point.
Then, the second point is considered to be on the <0,0,0>- segment, which is about an infinity of solution ; but I fix this by knowing the width(w) and height(h) of my rectangle: I then get two solutions for my second point ; one is "in front" of the first point, and the other is "far away"... I now have a edge of my rectangle. Two, in fact.
And from there, I don't know what to do. If in the end I have my four points, I don't have a clue about how to calculate the rotation equivalency...
It's hard to be lost in Mathematics...
To get an idea of the goal of all this: I make photospheres and I want to "insert" in them images. For instance, I got on my photo a TV screen, and I want to place a picture in the screen. I know my screen size (or I can guess it), I know the size of the image I want to place in (actually, it has the same aspect ratio), and I know the four screen corner positions in my space (spherical or euclidian). My software allow my to place an image in the scene and to rotate it as I want. I can zoom it (to give the feeling of depth)... I then can do all this manually, but it is a long try-fail process and never exact. I would like then to be able to type in the screen corner positions, and get the final image place and rotation attributes in a click...
The question in pictures:
Images presenting steps of the problem
Note that on the page, I present actual images of my app. I mean I had to manually rotate and scale the picture to get it fits the screen but it is not a photoshop. The parameters found are:
Scale: 0.86362
rX = 18.9375
rY = -12.5875
rZ = -0.105881
center position: <-9.55, 18.76, 1000>
Note: Rotation is not enought to set the picture up: we need scale and translation. I assume the scale can be found once a first edge is fixed (first two points help determining two solutions as initial constraints, and because I then know edge length and picture width and height, I can deduce scale. But the software is kind and allow me to modify picture width and height: thus the constraint is just to be sure the four points are descripbing a rectangle in space, with is simple to check with vectors. Here, the problem seems to place the fourth point as a valid rectangle corner, and then deduce rotation from that rectangle. About translation, it is the center (diagonal cross) of the points once fixed.

LibGDX - Rectangle collision detection in 2d?

OK, I want to try this:
Make two cars (with sprites: e.g. red rectangle for car 1 texture, green rectangle for car 2 texture). With width: 32px and height: 20px.
(Movement of the cars are not the problem)
Then check collision detection like in the picture. The first is front crash and the second is side crash.
collision http://img802.imageshack.us/img802/2934/rectangles2.png
Then delete the sprites and only hold the vectors in the code. (position and rotation)
I want it so, because I want to add 3d Cars at these positions with his rotations.
I mean, Collision detection without sprites in 2d.
In the end game, there will be no sprites. Only 3d Objects.
Anybody has some codes for that?
I want to make it without Box2D. But when there is a good box 2d example. Then I can make it with box2d.
Thank You for any help.
Well if you want to do collision detection I would just use the included box2d. Have a look at Box2d Car Physics, this will give you a good starting point on how to build up the car.The code is for C however because LibGDX is a wrapper all methods that are demonstrated in the tutorial are available. If you need help setting up the box2d physics in libgdx the wiki is very good. To get started building your engine you should just use the box2d debugger provided with libgdx, This just draws all shapes (box/circle/polygon) then once your happy with the behaivour of your engine, you can just change the rendering code and use the X,Y positions and rotation of your car and use your 3d models.

XNA Track rotated pixel positions

Im making a game in xna where a tank has to move over a landscape.
I need to be able find the bottom of the tank when it is rotated so I can make it move up and down as the player goes over the landscape.
for example if i have a sprite at with its top left corner at 400,300 and i rotate it around its center by 45 degrees around its center, how do i find the new locations of the bottom track.
Thanks
Thanks for the reply Langaurd.
I have looked at the article link before but didnt understand how it works.
Im making a 2d side scrolling game. As the player moves left and right, the tank has to also tilt to follow the contour of the terrain.
I have two vectors that store the back bottom of the track and one that stores the front bottom of the track.
I have tried
Vector2 backBottom = new Vector2(5, 25);
Vector2 frontBottom = new Vector2(5, 32);
backBottom = Vector2.Transform(backBottom+position, Matrix.CreateRotationZ(angle));
frontBottom = Vector2.Transform(frontBottom+position, Matrix.CreateRotationZ(angle));
but that gave me some strange values
Not 100% clear on exactly what it is you are trying to do. You mention a sprite, which is 2D, but your description is in 3D terms. If you are doing a 2D side view, then you can't tell the tank is rotated 45 degrees. If you are doing a 2D top-down view, then you shouldn't really care where the bottom of the tred is.
In any case, two suggestions. If you are die-hard on tracking rotated pixels, then read this article: 2D collision with Transformed Pixels from the creators.xna.com site. However I would recommend tracking vectors. Use two vectors to represent the track locations, and then use Vector2.Transform to rotate them with the tank. You could then use the vectors to check to see if the tracks have hit something, what angle they are at, ect.
You need to define a clearer orientation for you sprite. I would use a Front and Up Vector for the tank. Now you rotate both of them together with the angle your tank drives depending on the terrain. Lets say these vectors are at the center of your sprite. and your sprite is rotated, exactly like your up and front vectors. Now just multiply your Halfheight with -Up vector and you should have your local bottom center, add your tank position and you have your world bottom track position.
Important: Don't mix up a point, which can be expressed by a vector, or a real vector which has no position and only shows the direction. For directions its important to normalize the vector.
Sorry for the vague answer but you question is a little bit vague too.

Resources