Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (564)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  3d camera locking onto a target/z-targeting  (Read 1055 times)
0 Members and 1 Guest are viewing this topic.
Offline SkyAphid
« Posted 2013-01-01 02:42:40 »

So, I'm trying to recreate z-targeting from the zelda games in my own, and to do this I plan on using the lookAt function. Unfortunately, it's not working out for me, even if it is basic trigonometry.

I read the following articles on it:

http://www.euclideanspace.com/maths/algebra/vectors/lookat/index.htm
http://www.ehow.com/how_5717908_calculate-lookat-function.html

My problem is, even if I have the eye coordinates, and the target coordinates, I don't know what to do about the current target coordinates. All I have available to use are the Yaw and pitch of my camera, so I don't know how to take these and make the coordinates I need to get the proper dot product.

And even when I do get the dot product, I don't know what to do about the arccos. In the second tutorial, the author says he got his angle from the arccos of 10. Of course in Java, the input of arccos can't be over 1, so how do I get around this?

I'm sorry if these are obvious questions, my geometry teacher was terrible so most of my trig is self taught.

Thank you.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline Agro
« Reply #1 - Posted 2013-01-02 01:50:17 »

Well, I'm hoping your 3D camera is first-person based, then it will be easier. Cheesy

a 3D camera has pitch and yaw, but it can also be represented with a 3 dimensional vector. So, calculate the vector from your target to you, which is basically target.pos - camera.pos, and then you'd need to normalize it.

That's basically your directional vector, so now you need to figure out how to calculate the pitch and yaw values of the 3D vector. I've shown how to convert pitch and yaw to a 3d vector here: http://www.java-gaming.org/topics/pitch-yaw-to-directional-vector/27819/view.html

But now we need the inverse, which I don't really have an idea how to do. Sad Read up and euler angles, and maybe you'll find something...

I'll post if I find anything.

Offline LunaticEdit

Senior Member


Medals: 8
Projects: 1



« Reply #2 - Posted 2013-01-02 01:57:17 »

I'm not a 3D guy so I don't know what functions your lib does, but in XNA I had a 3rd person camera that 'looked at' a center point. What I would do is start at the X/Y/Z center of the character and then apply rotation matrix. THEN I'd apply a move translation matrix to 'pull' the camera back out (and still be looking at the character in a natural way) which seemed to feel natural while playing around with it. Assuming your library has the function, you could then simply perform a 'look at' on the camera to then face the enemy, which I would imagine would give it that zelda-like field.

Here's what I did using the method above (minus look-at of course):
<a href="http://www.youtube.com/v/-4n4Nz1xFKQ?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/-4n4Nz1xFKQ?version=3&amp;hl=en_US&amp;start=</a>

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2013-01-02 03:32:02 »

In case anyone's wondering, we solved this over Skype...

1  
2  
3  
4  
5  
6  
7  
8  
   public void lookAt(float targetX, float targetY, float targetZ){
      float dx = targetX - cameraX;
      float dy = targetY - cameraY;
      float dz = targetZ - cameraZ;
     
      yaw = (float)Math.atan2(dx, -dz);
      pitch = (float)Math.atan2(-dy, Math.sqrt(dx*dx + dz*dz));
   }

Myomyomyo.
Offline Roquen
« Reply #4 - Posted 2013-01-03 16:09:57 »

Camera's aren't worth worrying about any performance issues so this is just informational.  Skipping euler angles, you have:

The lookat direction from the camera to target (the dx,dy,dz above).  Normalize that.  Cross it with your up vector (say y, so little computation) and that give the direction to the 'side'.  Normalize that. Cross side and lookat gives camera's up (no need to normalize...side and lookat are orthogonal).  Shove these three vectors into the matrix (row or columns depending on your convention) and you're done.

forward = (fx,fy,fz) = (dx,dy,dz)/sqrt(dx*dx+dy*dy+dz*dz)
side = (sx,sy,sz) = (-fz, 0, fx)/sqrt(fz*fz+fx*fx)
up = (ux,uy,uz) = (-fy*sz, fx*sz-fz*sx, fy*sx)

Assuming I didn't screw anything up. Smiley
Offline theagentd
« Reply #5 - Posted 2013-01-03 16:39:25 »

Muh, matrix math is hard! xD The reason why I computed the angles was because I/we have a camera class that works like a first person camera with a yaw and a pitch. By computing those I can just feed those directly into the camera class and be done with it. Hmhmmm...

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Grunnt (20 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (107 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!