Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (775) Games in Android Showcase (230) games submitted by our members Games in WIP (856) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Libgdx, Box2D, Box2dLight  (Read 1198 times) 0 Members and 1 Guest are viewing this topic.
_Tumppu_

Senior Newbie

 « Posted 2018-02-05 13:18:33 »

Hello there

I have a little problem with Box2dLight. I want it to rotate to the direction where the hand is pointing (follow the mouse pointer).

So I made a little gif to show ya how it's behaving.

As u can see, it's pointing opposite direction :l.

So this is how I create my light:

 1  2  3  4  5 `         light = new ConeLight(ray,100, Color.WHITE, 200,arm.getPosition().x,arm.getPosition().y, 360, 20);         light.attachToBody(arm);         light.setColor(Color.BLUE); `

This is how I rotate the arm:

 1  2  3  4  5  6  7 `public void rotate() {      Vector3 worldCoords = camera.unproject(new Vector3(Gdx.input.getX(),Gdx.input.getY(), 0));      Vector2 vec2 = new Vector2(arm.getWorldCenter().x - worldCoords.x,arm.getWorldCenter().y- worldCoords.y);      vec2.set(vec2.x, vec2.y);float angle = (float) Math.atan2(arm.getWorldCenter().y - worldCoords.y, arm.getWorldCenter().x - worldCoords.x);                arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), angle);}`

And this is where rotating is happening:

 1  2  3  4  5  6  7  8  9  10  11  12  13 `   public boolean mouseMoved(int screenX, int screenY) {            mx = Gdx.input.getX();      my = Gdx.input.getY();         light.setPosition(arm.getPosition().x, arm.getPosition().y);         System.out.println(Math.toDegrees(arm.getAngle()));      rotate();            return false;   }`

So what I want, is that the licghtcone would point always to a mouse pointer. I read that, when attaching light to a body, it will automatically rotate to bodys direction. I have tried like changig the light direction and so, but it always seems to rotate just 180 and to an opposite direction. If u have any ideas, I would be glad .

Cheers

_Tumppu_
bmanmcfly
 « Reply #1 - Posted 2018-02-06 03:41:43 »

Rotate 180... 1pi

Sorry, I see you tried that.

Have you tried just putting the body to the opposite point?  Like at width - X and height - y?  Seems that would be a way of butchering things into what you want?

(Sorry, I don't have a lot of experience with box2d, it didn't produce an effect I had found desirable and ripped it out, just that's something I would try)
_Tumppu_

Senior Newbie

 « Reply #2 - Posted 2018-02-06 06:02:51 »

Rotate 180... 1pi

Sorry, I see you tried that.

Have you tried just putting the body to the opposite point?  Like at width - X and height - y?  Seems that would be a way of butchering things into what you want?

(Sorry, I don't have a lot of experience with box2d, it didn't produce an effect I had found desirable and ripped it out, just that's something I would try)

Hey, and thanks for your input.

Sorry, really didn't understand what u mean. Do u mean the light at the opposite point? My char is built up from 4 bodies (body, head, weapon & arm). Or do u mean the arm should be attached like the way u discribed? Sorry I'm little confused . Wierd thing thou, the arm is working nicely and rotating all the 360 degrees. Can't just understand why the light won't behave the same way.

Cheers

_Tumppu_
_Tumppu_

Senior Newbie

 « Reply #3 - Posted 2018-02-06 07:13:02 »

Also tried somethin:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32 `   public void rotate() {      Vector3 worldCoords = camera.unproject(new Vector3(Gdx.input.getX(),Gdx.input.getY(), 0));      Vector2 vec2 = new Vector2(arm.getWorldCenter().x - worldCoords.x,arm.getWorldCenter().y- worldCoords.y);      vec2.set(vec2.x, vec2.y);            float angle = (float) Math.atan2(arm.getWorldCenter().y - worldCoords.y, arm.getWorldCenter().x - worldCoords.x);            arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), angle);            float newAngle = (float) Math.PI / 180 * angle;      light.setDirection(newAngle);      System.out.println(newAngle);                  //System.out.println(angle);      /** double angle2 = arm.getAngle();      angle2 = normalRelativeAngle(angle2);      System.out.println(angle2);      light.attachToBody(arm); **/             /**  final float angle3 = arm.getAngle();         final float sin = MathUtils.sinDeg(angle3);         final float cos = MathUtils.cosDeg(angle3);         final float x = arm.getPosition().x + 1 * cos - 1 * sin;         final float y = arm.getPosition().y + 1 * sin + 1 * cos;         light.setPosition(x, y);         light.setDirection(angle3); **/                        }`

Output is intredasting

 1  2  3  4  5  6 `-0.04592159-0.04593443-0.045944814-0.0459527-0.045958187-0.045961812`

Just can't get it work :/

Cheers

_Tumppu_
orange451

JGO Kernel

Medals: 444
Projects: 7
Exp: 7 years

 « Reply #4 - Posted 2018-02-06 14:30:30 »

newAngle = angle + (float)Math.PI

First Recon. A java made online first person shooter!
_Tumppu_

Senior Newbie

 « Reply #5 - Posted 2018-02-06 14:39:00 »

newAngle = angle + (float)Math.PI

Hi there, okay thanks . I will test it out after a few hours when I'm back home.
I will report here how's it going .

Cheers

_Tumppu_
_Tumppu_

Senior Newbie

 « Reply #6 - Posted 2018-02-06 16:16:37 »

newAngle = angle + (float)Math.PI

Hi there, okay thanks . I will test it out after a few hours when I'm back home.
I will report here how's it going .

Cheers

_Tumppu_

Damn, still not working :l

code:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34 `   public void rotate() {      Vector3 worldCoords = camera.unproject(new Vector3(Gdx.input.getX(),Gdx.input.getY(), 0));      Vector2 vec2 = new Vector2(arm.getWorldCenter().x - worldCoords.x,arm.getWorldCenter().y- worldCoords.y);      vec2.set(vec2.x, vec2.y);            float angle = (float) Math.atan2(arm.getWorldCenter().y - worldCoords.y, arm.getWorldCenter().x - worldCoords.x);            float newAngle = angle + (float)Math.PI;      arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), newAngle);            light.attachToBody(arm);      //light.setDirection(arm.getAngle() + 135);      light.setDirection(newAngle);                  //System.out.println(angle);      /** double angle2 = arm.getAngle();      angle2 = normalRelativeAngle(angle2);      System.out.println(angle2);      light.attachToBody(arm); **/             /**  final float angle3 = arm.getAngle();         final float sin = MathUtils.sinDeg(angle3);         final float cos = MathUtils.cosDeg(angle3);         final float x = arm.getPosition().x + 1 * cos - 1 * sin;         final float y = arm.getPosition().y + 1 * sin + 1 * cos;         light.setPosition(x, y);         light.setDirection(angle3); **/                        }`

Cheers

_Tumppu_
buddyBro
 « Reply #7 - Posted 2018-02-06 17:10:54 »

My hypothesis of the issue, you're doing
arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), newAngle);
and you're ALSO doing
light.setDirection(newAngle);
My interpretation of Orange451's suggestion, and assumptions of the box2d api, would suggest you shouldn't be flipping (adding PI) to both the arm and the light, as that would just put the light back where it started. Try flipping only the light, not the arm. That is, revert line 9 to use angle
arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), newAngle);

Other notes,

does vec2.set(vec2.x, vec2.y); do anything? I'm not familiar with box2d, but assuming I understand correctly, that code seems redundant.

float angle = (float) Math.atan2(arm.getWorldCenter().y - worldCoords.y, arm.getWorldCenter().x - worldCoords.x);
can be replaced by
float angle = (float) Math.atan2(vec2.y, vec2.x);
in order to make it both more efficient, and more importantly, easier to read.

You have some formatting issues. It's a good idea to format your spacing and such before posting online. After all, you're asking other people to offer their time and experience, the least you could do is make it easier for them to understand your problem. Most IDE's have a keyboard shortcut to auto-format.
_Tumppu_

Senior Newbie

 « Reply #8 - Posted 2018-02-06 17:15:33 »

Thanks for the input, yeah the code is a little messy right now (sorry bout that). I will try those suggestions.

Cheers

_Tumppu_
Pages: [1]
 ignore  |  Print

 hadezbladez (42 views) 2018-11-16 13:46:03 hadezbladez (44 views) 2018-11-16 13:41:33 hadezbladez (25 views) 2018-11-16 13:35:35 hadezbladez (19 views) 2018-11-16 13:32:03 EgonOlsen (1894 views) 2018-06-10 19:43:48 EgonOlsen (1930 views) 2018-06-10 19:43:44 EgonOlsen (1285 views) 2018-06-10 19:43:20 DesertCoockie (1716 views) 2018-05-13 18:23:11 nelsongames (1404 views) 2018-04-24 18:15:36 nelsongames (2036 views) 2018-04-24 18:14:32
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 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