Jump to content

Camera follows a character ever cut by a wall?


Dad72
 Share

Recommended Posts

How can we ensure that the camera is always behind a character but ever cut by a obstacle, for example. 

Imagining that one is on the inside of a house, if you enter a room and it orbits the character, it is found behind the wall. 
If that would be useful is that the camera moves inside the room to always face the character.
 

post-5292-0-91148900-1416582822.png

 

Do I proposed the idea of this ​​camera? (I do not know how it is technically called).

Link to comment
Share on other sites

Raycasting would probably be useful for this. See if there is a clear line-of-sight for the camera, then move closer until there is.

 

Can you elaborate a bit more your idea, I'm not sure I understand. Thank you  :) 

 

PS: @joshcamas: The link in your signature is dead  ;)

Link to comment
Share on other sites

I built a really simple playground right here: http://playground.babylonjs.com/#1VITHH

Basically the camera is the yellow sphere, the green box is the target, and the grey cube is a piece of terrain. The idea is that you push a mesh that you want to be considered "solid" into an array.

But why doesn't the intersection detection work right? It should turn red when it touches, but it doesn't do it completely...

Link to comment
Share on other sites

Thank you Josh, this seems a very good starting point.

and for : It should turn red when it touches, but it doesn't do it completely.

I think that for the detection is not complete, is that the camera see the cube green while the wire is still in contact with the cube grey.

Thank you for the start. it remains only that the camera moves past the gray cube when the wire is red.

 

 

 

 

 

Link to comment
Share on other sites

Ah, yes indeed, it's very strange. I think you found a bug. What do you think Deltakosh,this looks like a bug?
 
I try a different approach, but same thing there to bug I think :
http://playground.babylonjs.com/#1VITHH#8 (Having said that the fil is still in contact with the cube ???)
 
PS: Josh It's a small detail, but the right syntax:  new BABYLON.Color3.Yellow => BABYLON.Color3.Yellow();  ;)

Link to comment
Share on other sites

Hi Guys, did a little personal debugging :-)

the problem is in the Ray.prototype.intersectsBoxMinMax() function (Class Ray in Typescript).

Found out that when one of the direction's value (x,y or z) is 0, the code reacts weird.

in general, this code : 

 

if (Math.abs(this.direction.y) < 0.0000001) {                if (this.origin.y < minimum.y || this.origin.y > maximum.y) {                    return false;                }}

is the cause of the bug.

I managed to "fix"it by commenting it out and adding the following lines to the "else" part (three times, once for x, once for y and once for z, else should be removed, of course, since there is no if):

var inv = 1.0 / this.direction.x;var min = (minimum.x - this.origin.x) * inv;var max = (maximum.x - this.origin.x) * inv;/* My addition */if(max==-Infinity) {     max = Infinity; }

The -Infinity happens due to the inv value (division with 0). It should be Infinity, since at a later part of the function maxValue is being determined, and if it is -Infinity (well, the smallest value JS can produce :) ) maxValue is always too low and the fucntion returns false.

This is just a hack (which solves the problem). I couldn't quite understand the math part of the if() section. What is being tested there? Maybe the one who wrote it can elaborate

Link to comment
Share on other sites

hola,

 

I was working on uploading the fix and found out this goes deeper than i thought. After my fix is in (looks a bit different than what i wrote before, but in general works the same), the intersectsBox function in the Ray class can be used to determine whether the ray intersects with the BoundingBox of the Box (which is == the box). But, After further checking the intersects function in Mesh :

var intersection = cube.intersects(ray);

I found out this function has some other problems as well. The main problem is the intersectsTriangle in the Ray class, which delivers wrong results.

I am not yet sure which function is to blame, but the root of the problem is this function. I am working on a fix for that, this might take some time.

A few things i found - 

To determine the triangles, subMashes is being used. the problem here is that the triangles are in local coordinates, but should be in world coordinates to determine if a ray (in world coordinates) intersects with the triangle.

I believe this is the main problem of the function (it is simply tested, try moving the box a few units up, the result (red line) will still be in the same position).

Other than that, the direction vector should be normalized etc' etc'.

I am working on a fix, but don't have all day for that. So this might not be today.

@Deltakosh, you have used the Möller–Trumbore intersection algorithm, which is delivering weird results in that case. Do you have any idea where I should look (apart from the two things i wrote before)?

Link to comment
Share on other sites

Wow, that was a long day.

Ok, a few corrections, new findings etc' etc' :

The only thing missing in the code is the world/coordinates conversion of the submesh vectors. The rest was simply an error in understanding what origin and direction means. I haven't noticed that at the beginning, but it is now clearer - 

Look at this example - http://playground.babylonjs.com/#1VITHH#9 . It works! Check lines 41 and 42, this is the fix for the problem - a direction vector between the cam and the target objects. If the middle box is not at 0,0,0 the detection will be weird due to the missing local-to-world conversion of the submeshes. This is a way to fix it - http://playground.babylonjs.com/#1VITHH#10

@JoshCamas and others - the mistake here was that direction is NOT the end point. Origin is the starting point, direction should be a vector pointing at the target and not the target itself.

@Deltakosh , the fix that i wanted to push ( and was pushed by dad72) might fix a single problem if the target not in 0,0,0 , it is not a global fix. I am not sure it is necessary. I think the Ray class should be extended with a few needed help functions to make this easier for everyone. I will send you a pull request tomorrow. Would be nice to have a Ray.GenerateRayFromTo(origin : Vector3, target : Vector3)  to fix those kind of mistakes, plus a ray should have a distance to prevent unlimited ray tracing in case a target is set.

Tl;dr : 

Direction != Target , intersects missing submeshes to world coordinates conversion. 

 

//Edited after a few further tests

Link to comment
Share on other sites

On 11/25/2014 at 1:45 PM, raananw said:

Direction != Target , intersects missing submeshes to world coordinates conversion.

Ahhhh that explains it! Awesome! And I agree with having that new function. Would be useful. :D

 

On 11/25/2014 at 6:57 PM, TriBlade9 said:

It might be easier if you did the raycast from the object to the camera instead, as that would give you the proper side of the intersection to move the camera to.

That's probably true! Thank you for that idea!

 

EDIT: Well, I got something! 

http://playground.babylonjs.com/#1VITHH#14

However, it seems the distance value is strange, or something. Probably something wrong with my math. 

Link to comment
Share on other sites

On 11/25/2014 at 7:36 PM, joshcamas said:

Ahhhh that explains it! Awesome! And I agree with having that new function. Would be useful. :D

That's probably true! Thank you for that idea!

EDIT: Well, I got something! 

http://playground.babylonjs.com/#1VITHH#14

However, it seems the distance value is strange, or something. Probably something wrong with my math. 

I'm working on it, though I need to leave here pretty soon.

I'd say to use the picked point->World space conversion and then make it a tad closer to the camera.

 

Darn, I'm out of time. I just did similar math for this, so I'm a bit confused on why I don't get it yet.

Anyways, it should be something to do with picked point and picked mesh, convert that to world coords, and stuff.

 

I'm still not sure what space pickedPoint is in.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...