It looks like you're new here. If you want to get involved, click one of these buttons!
Not too good at math, and i've been struggling to come up with a function to have a camera turn to a particular point. I figured I'd have to get the direction the camera is currently facing, and then some how calculate the direction it supposed to be at so I can use quat.fromToRotation()? I don't know if i'm on the right track, but if anybody has done this before or wants to attempt it, help me out!
Something like lookAt(objectToRotate, position)?
Comments
This is what I have so far, but it's definitely not correct lol
@John @dave1707 - thought occured to me with this thread - how does the Craft camera relate to the Codea camera ? Are both configrable through a single routine ?
@arismoko This is what I’ve used in the past but not sure if it’s what you’re looking for.
The
setDirection
function may be useful.@arismoko This is probably something I should have built-in to the entity class. For now I've made this:
This will point an entity towards a given target (I have to negate the direction to make the camera 'see' the position. It also lets you set an optional up value and t is used for interpolation if you want to turning to happen over time
@Bri_G Craft's camera system is separate to Codea's normal camera functionality. In Codea 4 I've unified these more so that you can use the camera class by itself or in a scene
@John Thanks a lot! That’s awesome! (:
I took one of my examples and stripped it down to just a camera and sphere. I added code for the look at calculations. I don’t understand any quat stuff, but I got it to work anyways. When the program starts, the camera and red sphere are randomly placed somewhere in a 400x400x400 area. When you tap the screen, the camera will rotate to look at the sphere. You can use the sliders to change the x,y,z positions of the camera and sphere. Tap the screen again to point the camera at the sphere. The x,y,z positions are displayed along with the camera quat values and camera to sphere distance.
As I mentioned above, I don’t understand the quat stuff, but I’ve been getting it to work. Here’s an example that uses the slerp function. It took awhile before I knew what it was supposed to do, but it works. This code creates 600 random green spheres just for reference, 1 random red sphere, and a randomly placed camera. When you tap the screen, the slerp function will rotate the camera at varying speeds to center the red sphere on the screen. To restart, just tap the restart icon again.
@dave1707 code above crashed on me after running several times then scrolling Codea off screen to the iOS apps screen and seconds later got Codea crash alert.
Edit: so far unable to reproduce it.
@Bri_G It doesn’t surprise me if it crashes after a few times. I’m creating a lot of entities each time and Codea might not be cleaning up with restarts. Maybe a collectgarbage() in draw would help it it continues. This was mostly just for show and needs to be coded differently if used for real.