Reputation: 529
What I am trying to do is take in an inputted user touch down, convert the camera coordinates of the touch down to world coordinates, then create a vector to use in order to apply force to my body. This is what I have for that:
Vector3 worldPos = new Vector3(newX, newY, 0);
GameScreen.gameCam.unproject(worldPos);
newX = worldPos.x;
newY = worldPos.y;
Vector2 direction = new Vector2();
direction.set((newX - xPos), (newY - yPos));
if (newX <= xPos){
direction.set((xPos - newX), (yPos - newY));
}
b2Body.applyForceToCenter(direction, true);
I know that this really isn't right at all as far as creating the vector goes, but the coordinates are being converted properly.
So my question is, how can I get an accurate vector, even in the negative direction, when I have both the world coordinates of the body and touch down?
I have tried doing some research to get a better understanding of all of this but I am having a bit of trouble. Any help would be greatly appreciated, thank you for your time.
Upvotes: 0
Views: 128
Reputation: 21
If you have on newX, newY screen touch coordinates and next change it to world coords you can get direction by subtraction touchWorldPos to currentObjectPos:
Vector3 worldPos = new Vector3(newX, newY, 0);
GameScreen.gameCam.unproject(worldPos);
newX = worldPos.x;
newY = worldPos.y;
Vector2 direction = new Vector2();
direction.set((newX - xPos), (newY - yPos));
b2Body.applyForceToCenter(direction, true);
without this if statement:
if (newX <= xPos){
direction.set((xPos - newX), (yPos - newY));
}
in box2d this direction might have small values and you must muliply this one by some value like 100(mayby more)
Upvotes: 1