I have a sprite that needs to follow the command of an arrow sprite that rotates, right now I can only make my sprite move up,down and left,right. How about diagonally? I have no Idea how to do that, the sprite also needs to move always towards outside the screen.
EDIT:
So this is what I did:
Vector2 position=new Vector2();
public void update(){
//getAngle() is the return value from another class
position.set(MathUtils.cosDeg(cannonObj.getAngle()),MathUtils.sinDeg(cannonObj.getAngle()));
sprite.setPosition(position.x,position.y);
}
What happens is that when I rotate the arrow the main sprite is just moving like the arrow(moving in a circular motion).
//I also tried this
position.scl(2,2);
or
position.translate(2,2);
sprite.x += sin(angle) * amount;
sprite.y += cos(angle) * amount;
I forget whether you have to enter your angle in radians or degrees. In Java you can use the Math class to do the calculations though.
Related
I am making a game like pong except that there is only one paddle and the ball moves in projectile motion. The goal is to keep the ball bouncing on your paddle for as long as possible. I when I have the ball hit the paddle the direction of the y component of the velocity has it's sign flipped. The issue with this is that when the ball is moving up gravity acts upon in in that direction, speeding it up. The code is below
This is the code for my ball class, this is the tick method which is called 60 times a second
public Ball(double x, double y, Game game) {
super(x,y);
this.game=game;
}
public void tick() {
time+=1.0/60.0;
if(x<=0)
xreflection=1.0;
else if(x>=Game.Width-15)
xreflection=-1.0;
if(y<=0)
yreflection=1.0;
else if(y>=Game.Height-15)
gameover=1;//different variable here as I use this to end the game if the ball hits the bottom of the screen
x+=traj.xvel()*xreflection;
y-=traj.yvel(time)*yreflection;
if(Physics.Collision(this, game.getP())) {
time=2;
System.out.println("Collision");
yreflection=-1;
}
}
This is my ball Trajectory class which handles all the math for this
public double xvel() {
double xvelo=initvel*Math.cos(Math.toRadians(theta));
return xvelo;
}
public double yvel(double time) {
double yvelo;
yvelo=initvel*Math.sin(Math.toRadians(theta))-(9.8*time);
return yvelo;
}
And I have tried to use an if statement with y reflection to make 9.8 negative when yreflection is 1 and positive when it is -1.
you are not really doing reflection ... to reflect by major axis you should negate the appropriate coordinate of the speed vector (and correct the position) I do not see such behavior in your code. Instead your y velocity has no sign regardless of the direction up/down hence you just add gravity acc to it ... to remedy either rewrite the code or add the yreflection to your gravity related code too... also you got theta? I would expect angle only in the first shooting
see
Bouncing Ball Making it slow down at peak of height
My Algorithm to Calculate Position of Smartphone - GPS and Sensors
pendulum simulation
You just use/add forces you want to use. But in your collision once you detect you are inside wall you need to correct the position along with the reflection of speed too otherwise you risk double collisions ...
Here small C++ example with air friction:
//---------------------------------------------------------------------------
double pos[2],vel[2],acc[2],r; // ball
double x0,y0,x1,y1; // walls
//---------------------------------------------------------------------------
void ball_update(double dt)
{
int i;
double v,k=0.0001,g=9.81;
dt*=10.0; // time multiplier for simulation speed ...
// compute driving force/acceleration
v=sqrt((vel[0]*vel[0])+(vel[1]*vel[1])); // |vel|
acc[0]= -(k*vel[0]*v); // gravity + air friction (k*vel^2)
acc[1]=+g-(k*vel[1]*v);
// Newton/D'Alembert simulation
for (i=0;i<2;i++) vel[i]+=acc[i]*dt;
for (i=0;i<2;i++) pos[i]+=vel[i]*dt;
// colision/reflect
if (pos[0]<x0+r){ pos[0]=x0+r; vel[0]=-vel[0]; }
if (pos[0]>x1-r){ pos[0]=x1-r; vel[0]=-vel[0]; }
if (pos[1]<y0+r){ pos[1]=y0+r; vel[1]=-vel[1]; }
if (pos[1]>y1-r){ pos[1]=y1-r; vel[1]=-vel[1]; }
}
//---------------------------------------------------------------------------
void ball_init()
{
Randomize();
pos[0]=0.5*(x0+x1);
pos[1]=0.5*(y0+y1);
double a=2.0*M_PI*Random(),v=50.0;
vel[0]=v*cos(a);
vel[1]=v*sin(a);
r=20.0;
}
//---------------------------------------------------------------------------
My coordinate system is (0,0) is top left and x point right and y points down ...
To use this just init the walls x0,y0,x1,y1 call the ball_init() and then in some timer call ball_update(dt) and render ball at pos and radius r ...
This is how it look like:
PS. You need to tweak the parameters like delta time dt, accelerations or add pixel scale to match your needs... You need to have all units compatible ... I recommend using SI (m,m/s,m/s^2,s,N,.. ) so you also need to decide how big is pixel (in meters)
I am working on a project in LibGDX, and I am using Scene2D actors for some of my sprites. In this regard, I have a sprite, which is spawning somewhere on the screen and needs to move to another position on the screen. To do this I am using the moveTo(xPos, yPos, duration, interpolation) method in the Actions, to make the move animation.
However, when I use this approach, the actor moves like I told it to, but it only moves in a straight line, from point A to B. I have tried several Interpolation options, like Circle interpolation and such, but it seems only to impact the speed of the animation line.
So now my question: How do I make my animation make a smooth curved line (See picture), from A to B?
I am currently using this code to make the Actions animation:
adultCustomerPointActor.addAction(Actions.sequence(
Actions.moveTo(300, 200, 2f, Interpolation.circle)
));
Thanks in advance for your help :)
It's a geometry problem. Using vectors, find the point halfway between the two points:
vec1.set(bx, by).sub(ax, ay).scl(0.5f).add(ax, ay);
Get another vector that is 90 or 270 to from the vector between the points:
vec2.set(bx, by).sub(ax, ay).rotate90().add(vec1);
This vec2 can be scaled to adjust how extreme curvature of the arc is. If you leave it alone, you'll have a quarter circle. You can also scale it negative to reverse the curvature.
Then add the second vector to the first to find the center point of your arc, which we can call point C.
vec1.set(bx, by).sub(vec2); // CB
vec3.set(ax, ay).sub(vec2); // CA
float angle = vec1.angle(vec3);
Now you need a vector that points from point C to point A. You will rotate this vector until it reaches point B. So you need the angle between CA and CB.
So here's a very simplistic class that implements this. It doesn't account yet for deciding if you want the arc to go up or down and if you want to scale how extreme it looks. You could add those as additional parameters with getters/setters. I haven't tested it, so it may need some debugging.
public class ArcToAction extends MoveToAction {
private float angle;
private final Vector2 vec1 = new Vector2(), vec2 = new Vector2(), vec3 = new Vector2();
#Override
protected void begin () {
super.begin();
float ax = target.getX(getAlignment()); // have to recalculate these because private in parent
float ay = target.getY(getAlignment());
vec1.set(getX(), getY()).sub(ax, ay);
vec2.set(vec1).rotate90();
vec1.scl(0.5f).add(ax, ay);
vec2.add(vec1);
vec1.set(bx, by).sub(vec2); // CB
vec3.set(ax, ay).sub(vec2); // CA
angle = vec1.angle(vec3);
}
protected void update (float percent) {
if (percent >= 1){
target.setPosition(getX(), getY(), getAlignment());
return;
}
vec1.set(vec3).rotate(percent * angle);
target.setPosition(vec1.x, vec1.y, getAlignment());
}
}
If you want to support automatic pooling, you can add a method like this:
static public ArcToAction arcTo (float x, float y, float duration, Interpolation interpolation) {
ArcToAction action = Actions.action(ArcToAction .class);
action.setPosition(x, y);
action.setDuration(duration);
action.setInterpolation(interpolation);
return action;
}
I am currently creating a game based on a platform that tilts side to side and there is a ball on that platform. For this ball I applied dynamic body physics and got it to work great, but when I try updating the ball's sprite rotation based on the ball's body angle the sprite will not rotate.
My code is below.
public static void update(float delta) {
delta *= 0.7;
world.step(delta, 10, 4);
//Angular Impulse is here to test if ball sprite is rotating
ballBody.applyAngularImpulse(40, true);
AssetManager.ballSprite.setRotation(ballBody.getAngle());
AssetManager.ballSprite.setPosition(ballBody.getPosition().x,
ballBody.getPosition().y);
}
}
Whenever you want to rotate a sprite in libgdx when rendering it you must do:
sprite.draw(spritebatch);
but I made the mistake of trying to render it how I would render anything else:
spritebatch.draw(sprite);
In my 3d application I wish to have an object (a tree, for example), and my camera to look at this object. Then, I want the camera to rotate about the object, in a circle, while looking at the tree the whole time. Imagine walking around a tree, while constantly changing your angle so that you are still looking at it. I know this requires both rotation of my camera, and translation of my camera, but the math is far beyond the level I have been taught in schooling thusfar. Can anyone point me in the right direction?
Here is one way with very simple math. First, you need a constant for the distance the camera is from the center of the tree (the radius of the circle path it travels on). Also, you need some variable to track it's angle around the circle.
static final float CAM_PATH_RADIUS = 5f;
static final float CAM_HEIGHT = 2f;
float camPathAngle = 0;
Now you can change the camPathAngle to anything you want from 0 to 360 degrees. 0 degrees corresponds with the location on the circle that is in the same direction as the world's X-axis from the tree's center.
On each frame, after you've update camPathAngle, you can do this to update the camera position.
void updateTreeCamera(){
Vector3 camPosition = camera.getPosition();
camPosition.set(CAM_PATH_RADIUS, CAM_HEIGHT, 0); //Move camera to default location on circle centered at origin
camPosition.rotate(Vector3.Y, camPathAngle); //Rotate the position to the angle you want. Rotating this vector about the Y axis is like walking along the circle in a counter-clockwise direction.
camPosition.add(treeCenterPosition); //translate the circle from origin to tree center
camera.up.set(Vector3.Y); //Make sure camera is still upright, in case a previous calculation caused it to roll or pitch
camera.lookAt(treeCenterPosition);
camera.update(); //Register the changes to the camera position and direction
}
I did it like that for the sake of commenting it. It's actually shorter than the above if you chain commands:
void updateTreeCamera(){
camera.getPosition().set(CAM_PATH_RADIUS, CAM_HEIGHT, 0)
.rotate(Vector3.Y, camPathAngle).add(treeCenterPosition);
camera.up.set(Vector3.Y);
camera.lookAt(treeCenterPosition);
camera.update();
}
In a Java 2D game, I have a rectangular sprite of a tank. The sprite can rotate in any angle, and travel in the direction of that angle.
This sprite needs to have a bounding box, so I can detect collision to it.
This bounding box needs to:
Follow the sprite around the screen.
Rotate when the sprite rotates.
Obviously it should be invisible, but right now I'm drawing the box on the screen to see if it works. It doesn't.
My problem is this:
When the sprite travels parallel to the x axis or y axis, the box follows correctly and keeps 'wrapping' the sprite precisely.
But when the sprites travles diagonaly, the box doesn't follow the sprite correctly.
Sometimes it moves too much along the x axis and too little along the y axis. Sometimes the opposite. And maybe sometimes too much both or too little on both. Not sure.
Could you look at my code and tell me if you see anything wrong?
(Please note: The bounding box most of the time is actually just two arrays of coordinates, each one containing 4 values. The coordinates are used to form a Polygon when collision is checked, or when the box is drawn to the screen).
Relevant code from the Entity class, the superclass of Tank:
int[] xcoo = new int[4]; // coordinates of 4 vertices of the bounding box.
int[] ycoo = new int[4];
double x,y; // current position of the sprite.
double dx,dy; // how much to move the sprite, and the vertices of the bounding box.
double angle; // current angle of movement and rotation of sprite and bounding-box.
// Returns a Polygon object, that's the bounding box.
public Polygon getPolyBounds(){ return new Polygon(xcoo,ycoo,xcoo.length) ; }
public void move(){
// Move sprite
x += dx;
y += dy;
// Move vertices of bounding box.
for(int i=0;i<4;i++){
xcoo[i] += dx;
ycoo[i] += dy;
}
// Code to rotate the bounding box according to the angle, will be added later.
// ....
}
Relevant code from the Board class, the class that runs most of the game.
This is from the game-loop.
// keysPressed1 is an array of flags to tell which key is currently pressed.
// if left arrow is pressed
if(keysPressed1[0]==true)
tank1.setAngle(tank1.getAngle()-3);
// if right arrow is pressed
if(keysPressed1[1]==true)
tank1.setAngle(tank1.getAngle()+3);
// if up arrow is pressed (sets the direction to move, based on angle).
if(keysPressed1[2]==true){
tank1.setDX(2 * Math.cos(Math.toRadians(tank1.getAngle())));
tank1.setDY(2 * Math.sin(Math.toRadians(tank1.getAngle())));
tank1.move(); // should move both the sprite, and it's bounding box.
}
Thanks a lot for your help. If you need me to explain something about the code so you can help me, please say so.
Your sprite is using doubles and your bounding box is using ints, see these declarations:
int[] xcoo = new int[4];
double x, y
And the following updates:
(double dx, dy, showing it is a double)
x += dx
xcoo[i] += dx
In the latter (the bounding box) you are adding an int to a double which causes it to drop it's decimal places as it is being cast to an integer.
Hence why they do not follow the sprite exactly, as an int can never follow a double.
To solve this you need xcoo, ycoo and corresponding methods to work with double instead of int.
Update: So Polygon only takes Integers appereantly, to solve that take a look at the following question: Polygons with Double Coordinates
You should be using Path2D.Double