Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (808) Games in Android Showcase (239) games submitted by our members Games in WIP (872) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Make an Entity in 3D face a specific angle?  (Read 10363 times) 0 Members and 1 Guest are viewing this topic.
BurningCore
 « Posted 2014-11-26 06:38:55 »

Okay, so right now, my Entities have both a position and a rotation vector. I have a follow(Player) function, but as you may have imagined, the object is facing the same direction while it's chasing the player. Ultimately, I would like the entity to face the player as he chases him. Here's what I have in mind:

 1  2  3  4  5  6  7  8  9 `      double theta = Math.tan(dz / dx), angle = this.getRotY() + theta;                //dz and dx are the sides of the triangle that I use for distance                  if (angle == 0)            increaseRotation(0, 0, 0);         else if (angle > 180)             increaseRotation(0, (float) (-angle), 0);         else            increaseRotation(0, (float) (angle), 0);`

Any ideas? Sorry if this is super simple, but I'm having a tough time thinking this through (very tired, at the moment).
Stranger
 « Reply #1 - Posted 2014-11-26 07:39:57 »

At least, your code should look like this:
 1  2  3  4  5  6  7  8  9 `double theta = Math.atan2(dz, dx), angle = this.getRotY() + Math.toDegrees(theta); //dz and dx are the sides of the triangle that I use for distance         if (angle == 0)   increaseRotation(0, 0, 0);else if (angle > 180)         increaseRotation(0, (float) (-angle), 0);else        increaseRotation(0, (float) (angle), 0);`

Anton
BurningCore
 « Reply #2 - Posted 2014-11-26 13:56:07 »

Thanks for the reply, but the entity still spazzes out.
Stranger
 « Reply #3 - Posted 2014-11-26 14:32:36 »

Anton
BurningCore
 « Reply #4 - Posted 2014-11-26 14:42:09 »

Well, that's all the code there is to it. I added a time constant to the angle, and it fixed the spazzing, but it looks at the player it's following from the right or left side of the model instead of directly at it (if that makes sense). So, I modified my code to the following:

 1  2  3  4 `if (angle == 0)    increaseRotation(0, 0, 0);else    increaseRotation(0, (float) ((-angle + 90) * DisplayManager.getFrameTimeSeconds() * 2), 0);`

The only issue I have now is that it is looks at the player directly but when it is facing from 0 <= x <= 180, it stares at the player from the back part of the model.
BurningCore
 « Reply #5 - Posted 2014-11-26 21:43:24 »

I know I'm dealing with the discontinuity of atan function, but is there any way to bypass it? For example, once it hits 180 degrees, it just keeps going to make a full circle?
Stranger
 « Reply #6 - Posted 2014-11-27 13:01:58 »

I have found the similar case. Hope it will help.

http://www.java-gaming.org/index.php?topic=21947.0

Anton
BurningCore
 « Reply #7 - Posted 2014-11-27 16:07:55 »

I'll let you know.
BurningCore
 « Reply #8 - Posted 2014-11-27 17:36:56 »

Yeah, it didn't completely fix it, but I did alter my code some. The following entity currently only turns once (when passing the angle 360). Here is the altered code:

 1  2  3  4  5  6 `double theta = Math.atan2(dz, dx), angle = this.getRotY() + Math.toDegrees(theta);if (angle == 0)   increaseRotation(0, 0, 0);else    increaseRotation(0, (float) ((-angle + 90) * DisplayManager.getFrameTimeSeconds() * 5), 0);`
BurningCore
 « Reply #9 - Posted 2014-11-30 05:23:15 »

Still haven't fixed the problem. I got the enemy that's following to make a complete circle perfectly fine, but after that complete circle, the enemy flips due to the tangent function. Any ideas? Here is the full method:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `      float xPos = player.getPosition().x, zPos = player.getPosition().z;      float minXPos = position.x, minZPos = position.z;      float dx = xPos - minXPos, dz = zPos - minZPos;      double distance = Math.sqrt(dx * dx + dz * dz);      double theta = Math.toDegrees(Math.atan2(dz, dx)), angle = this.getRotY() + theta;            if (angle < 0)         angle += 360;      if (distance < getRange()) {         increasePosition((float) (DisplayManager.getFrameTimeSeconds() * walkSpeed * (dx / distance)), 0,                (float) (DisplayManager.getFrameTimeSeconds() * walkSpeed * (dz / distance)));      increaseRotation(0, (float) ((-angle + 90) * DisplayManager.getFrameTimeSeconds() * 5), 0);`
Pages: [1]
 ignore  |  Print

 mercenarius (14 views) 2020-06-04 19:26:01 mercenarius (24 views) 2020-06-04 19:13:43 Riven (866 views) 2019-09-04 15:33:17 hadezbladez (5848 views) 2018-11-16 13:46:03 hadezbladez (2657 views) 2018-11-16 13:41:33 hadezbladez (6273 views) 2018-11-16 13:35:35 hadezbladez (1518 views) 2018-11-16 13:32:03 EgonOlsen (4749 views) 2018-06-10 19:43:48 EgonOlsen (5804 views) 2018-06-10 19:43:44 EgonOlsen (3298 views) 2018-06-10 19:43:20
 A NON-ideal modular configuration for Eclipse with JavaFXby philfrei2019-12-19 19:35:12Java Gaming Resourcesby philfrei2019-05-14 16:15:13Deployment and Packagingby philfrei2019-05-08 15:15:36Deployment and Packagingby philfrei2019-05-08 15:13:34Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04: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