Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Mathematically Locking Object to Camera With Offsets

Author
Message
Milkman
18
Years of Service
User Offline
Joined: 30th Nov 2005
Location: United States
Posted: 4th Feb 2007 06:45 Edited at: 21st Mar 2007 05:32
This does exactly what the title says. I worked on this for another thread, and thought I'd post it here. Could be very useful for a lot of things

Base code:


An explaination of the variables:

All variables are floats. sx, sy, sz, cx, cy, cz just store the sine/cosine values of the camera angles. camAngleX, camAngleY, camAngleZ represent the camera's current angles of rotation. camPosX, camPosY, camPosZ represent the camera's current position. xOffset, yOffset, zOffset are the offset values relative to the camera. xPos, yPos, and zPos are the coordinates at which the object should be positioned.



EDIT:

Here are the three functions that were made from this code:
lockObjectToCamera(object, xOffset, yOffset, zOffset)
lockObjectToObject(object, baseObj, xOffset, yOffset, zOffset)
lockCameraToObject(camera, baseObj, xOffset, yOffset, zOffset)




Here's a no media example for dbp (just copy, paste, and run):



And here's a function that works with dbc (thanks Duke Blue Devils/Latch):


Who needs a signature?
Juso
21
Years of Service
User Offline
Joined: 23rd Sep 2002
Location: Finland
Posted: 25th Feb 2007 11:04
Fine job,
but what should be changed to lock camera to (moving) object with offsets?
Milkman
18
Years of Service
User Offline
Joined: 30th Nov 2005
Location: United States
Posted: 26th Feb 2007 21:58
you would need a function such as this:



here is a demonstration of it with dbp code, just copy, paste, and run:



so now you have a lockObjectToCamera() function and a lockObjectToObject() function

Who needs a signature?
Juso
21
Years of Service
User Offline
Joined: 23rd Sep 2002
Location: Finland
Posted: 2nd Mar 2007 16:47 Edited at: 2nd Mar 2007 16:47
Thanks Milkman, so I made easily LockCameraToObject()



and with

camera spins with cube, very nice
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 7th Mar 2007 10:30 Edited at: 7th Mar 2007 10:31
@milkman

There was a question on the DBC forum that I was answering that looks like it was based on this algorithm. The problem they were having was with the object not rotating properly with the camera.

I discovered that their function needed to reverse the order of rotation for their object. I had to use the command SET OBJECT ROTATION ZYX. Here's some example code I put together:



Enjoy your day.
Milkman
18
Years of Service
User Offline
Joined: 30th Nov 2005
Location: United States
Posted: 8th Mar 2007 23:13
@Juso: cool

@Latch: Thanks, I didn't test this with dbc so I wasn't aware of that problem. I've put your dbc code in the first post for anyone who might need it.

Who needs a signature?
Ianhfar
19
Years of Service
User Offline
Joined: 8th Jul 2004
Location: UK
Posted: 12th Mar 2007 10:36
Very usefull fuctions, thans milkman !

IanF
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Mar 2007 11:02
@Milkman

Duke Blue Devils actually designed the function based on your code. I just was able to get it to work for DBC. So he should get the credit for the function.

Enjoy your day.

Login to post a reply

Server time is: 2024-05-04 15:42:45
Your offset time is: 2024-05-04 15:42:45