Note that there are some explanatory texts on larger screens.

plurals
  1. POOrbiting a 3d camera around an object in javascript
    primarykey
    data
    text
    <p>After googling I haven't found the anwser to this question so I am going to try it here.</p> <p>Using (2d) canvas I made a 3d world in javascript and even a 3d camera that works fine with it. However, the camera is a 'free camera' so when you rotate, the camera rotates around itself.</p> <p>Now I would like to have the camera rotate around an in-world object but I have a lot of trouble understanding how to do that.</p> <p>I tried the following formula but it freaks out before I even attempt to rotate... <s></p> <pre><code>this.x += (Math.sin(this.yd * (Math.PI / 180)) * distance); this.z -= (Math.cos(this.yd * (Math.PI / 180)) * distance); </code></pre> <p></s> <strong>UPDATED SO FAR</strong> Thanks to the anwser provided by WojtekT below, I got to the following code, it turns the camera arounds the object, but also seem to zoom in... this might just be my eyes fooling me since the distance variable barely changes (I think there is a tiny rounding issue, but other then that it gives the expected result)</p> <pre><code>this.y = (Math.sin(this.yd * (Math.PI / 180)) * distance + this.targetY); this.z = (Math.cos(this.yd * (Math.PI / 180)) * distance + this.targetZ); </code></pre> <p>Where this.yd is the speed it should run in degrees.</p> <p>What formula do I need?</p> <p><strong>EDIT</strong>: I am unsure what pieces of code can help solve this problem, so here is the complete source: </p> <p><a href="http://clanpvp.com/sol" rel="nofollow">http://clanpvp.com/sol</a> http://clanpvp.com/js/sol/camera.js &lt; Camera object, this probably.</p> <p>For debugging reasons, a new frame is loaded when a keyup event is triggered. Also for debugging, the target object is a fixed point. at the center of the screen.</p> <p>The arrow keys move the camera, the wasd keys turn the camera, so far only W and S do something and they do it wrong thanks to above code...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload