How can I implement multiple touch in Box2D? - java

I want to move two bodies on a android phone using two separate touch's, Is this possible?

You can get coordinates of first and second touch with following methods
int firstX = Gdx.input.getX(0);
int firstY = Gdx.input.getY(0);
int secondX = Gdx.input.getX(1);
int secondY = Gdx.input.getY(1);
Use them to move your objects.
You can read more in libgdx documentation

Related

How to move two sprite object in one screen in android libgdx?

I want to add motion to two sprite objects in single game screen. I mean two sprite object should move(or follow some predefined path) independently in single page or game screen of app using Libgdx. How can I achieve that.
Please help me. If possible please provide some reference code also. Thank you.
you can use scene2d for this purpose.where you can easily move object through actions.
you can follow these links to learn scene2d.
http://www.gamefromscratch.com/post/2013/11/27/LibGDX-Tutorial-9-Scene2D-Part-1.aspx
http://www.gamefromscratch.com/post/2013/12/09/LibGDX-Tutorial-9-Scene2D-Part-2-Actions.aspx
i hope these links will help you.
You can do this:
take two 2DVector objects:
private Vector2 positiononesprite,positiontwosprite;
Sprite sprite_one,sprite_two;
then in your create method do this
positiononesprite = new Vector2(0,0);
positiontwosprite = new Vector2(0,0);
//set your sprite position
sprite_one.setPosition(x,y);//your x and y coordinates
sprite_two.setPosition(x1,y1);//your second sprite postions
positiononesprite.x = sprite_one.getX();
positiononesprite.y = sprite_one.getY();
positiontwosprite.x = sprite_two.getX();
positiontwosprite.y = sprite_two.getY();
/*
then to make them move in a custom direction you can use either
setPosition method or translate method*/
//apply your algorithm on vectors and set or translate your sprites
// in render method define there speed, direction and move them
//for example i did this to move it in a particular direction
pointerposition.x += directionpointer.x * speed;
pointerposition.y += directionpointer.y * speed;
// pointer.setPosition(pointerposition.x, pointerposition.y);
ball.setPosition(pointerposition.x, pointerposition.y);
this is moving my ball in a particular direction
here directionpointer is a direction vector and speed is a float variable and pointerposition is a vector2 object as i declared positiononesprite

Java - Create world collision with Heightmap (JBullet)

I'm trying to create an infinite playable world with Jogl, Jbullet, and OpenSimplexNoise (OSN).
I'm generating the world with OSN, rendering it successfully, but I don't know how to add it to the world/collision system.
I found the btHeightfieldTerrainShape class, but it isn't implemented in Java.
I tried to use BvhTriangleMeshShape too, but i don't understand how it work.
I have 3 values of generation:
int smooth: the number of division in one meter
int viewDistance: the number of chunk to draw in one axis
int chunkSize: the number on
meter in one chunk
I'm using this code for generate heightmap:
/**
* Generate all height in the chunk p[cx][cy].
*/
private float[][] genPerlinMap(int[] p) {
float[][] pts = new float[chunkSize*smooth+1][chunkSize*smooth+1];
for(int i1=0;i1<chunkSize*smooth+1;i1++){
for(int i2=0;i2<chunkSize*smooth+1;i2++){
pts[i1][i2] = (float) (osp.eval(
(p[0]*chunkSize+i1/(float)(smooth))/(float) (mapSize),
(p[1]*chunkSize+i2/(float)(smooth))/(float) (mapSize)
)+1)*0.5f*mapSize;
}
}
return pts;
}
Does someone know how to add this ?
I found a solution: use https://github.com/bubblecloud/jbullet (jbullet from github) with the code here: https://github.com/bubblecloud/jbullet/commit/a5da8cdc679e998ef3a8605ee9b3cd5f94d71fee .
Thanks gouessej for the jbullet github link :)

Libgdx, how can I create a rectangle from coordinates?

I am currently trying to make a selector box for an RTS game. For this I need to be able to drag the mouse in order to create the selection box, however this can lead to a negative length/width.
In Libgdx is there a way to make rectangle from just using 2 sets of coordinates?
Thanks.
this is a simple idea, if I understand what you want to do:
to create a rectangle you can use this, Rectangle(float x, float y, float width, float height) for more inforamacion you can read it here http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Rectangle.html
this a psuedo code more or less:
create a listener that captures keystrokes, mouse or them as appropriate,
in touchdown catches x, y, and assign a:
yourVariableTouchDown.x = x;
yourVariableTouchDown.y = y;
then when the touchup captures the x is executed, and the point where it makes up touch and assign a:
yourVariableTouchUp.x = x;
yourVariableTouchUp.y = y;
after create the rectagle:
private Rectangle yourRectangle = new Rectangle();
yourRectangle(yourVariableTouchDown.x, yourVariableTouchDown.y,
(yourVariableTouchDown.x - yourVariableTouchUp.x),
(yourVariableTouchDown.y - yourVariableTouchUp.y));
if you want to see it you can use ShapeRenderer:
look this http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html
add for test in variable class
private ShapeRenderer sRDebugRectangel = new ShapeRenderer();
add for test in update or draw
sRDebugRectangel.begin(ShapeType.Filled);
sRDebugRectangel.identity();
sRDebugRectangel.rect(yourRectangle.getX(),
yourRectangle.getY(),
yourRectangle.getWidth(),
yourRectangle.getHeight());
sRDebugRectangel.end();
you can look on that listener use:
https://www.google.es/#q=listener+libgdx
P.S: what you say negative, will be a matter of check when touchup is less than touchdown change where the rectangle is created that was just what I happened to you have test it and adjust the variables to create the rectangle now because you can not be created desirably when negative, now I have time to get with it, in fact eh not tested this why I said it was pseudo code, hope you serve, idea
P.S: You can also look at this https://stackoverflow.com/tour

How to move an entire GPen object?

In Java using the acm.graphics GPen is there any way to move the entire drawn sequence of lines? I've read the manual thoroughly and I'm beginning to think it's not possible which brings me to my second question. Are there any other graphics objects in Java that work very similar to a pen that can also be moved. The reason I'm asking is because I've been working on a graphing program that allows mouse gestures to be used to pan around and zoom in and out. After building functionality for implicit functions I realized simply clearing the drawing board and redrawing everything is not going to cut it anymore so I really need to work on more efficient ways to handle intermediate changes of the graph without having to recalculate everything. For example with this or similar code:
GPen p = new GPen();
p.setLocation(100,100); //places the pen on the canvas at 100, 100
p.drawLine(-50,0); //draw a line left 50 pixels
p.drawLine(50,-50); //draw a line right and up 50 pixels each
p.drawLine(0,50); //draw a line down 50 pixels
This would result in a simple right triangle who's bottom right most point is at 100, 100 on a particular canvas. What I need to do is be able to move this same drawn sequence of lines relative to one another to another origin. What I hoping for is a class that has separate methods for setLocation() and move() where setLocation() controls pen position and move() would move the entire object around.
Ok so having received almost no attention on here I've came to the conclusion that such a method just needs to be written from scratch and went ahead and did that. I'm not entirely sure how helpful posting my proprietary code would be but in the event that anybody could use it I'll post the basic idea of it. Since Pen utilities are essentially a bunch of lines and lines are a bunch of from and to's I created an object that I called FPen (for FunctionPen) that accepts the instructions for from and to. While defining FPen you pass it where to start and how far to go however many times you need and that's it. Once you've passed these instructions I created another method called returnGPen(Color c) which will on call use the instructions it has on hand and generate the desired GPen object. When you want to move the entire GPen you can then create a method called adjustOrigin(double oX, double oY); which will calculate a change from a previously recorded origin and this new one and go through the list of instructions and adjust them appropriately.
My needs for this Class are strictly for my Graphing program and are not entirely finished either but it does work for most purposes.
import acm.graphics.GPen;
import java.awt.Color;
import java.util.ArrayList;
public class FPen{
private double relativeCenterX;
private double relativeCenterY;
private ArrayList<Double> fromX = new ArrayList<Double>();
private ArrayList<Double> fromY = new ArrayList<Double>();
private ArrayList<Double> distX = new ArrayList<Double>();
private ArrayList<Double> distY = new ArrayList<Double>();
public FPen(double rX, double rY, double z){
relativeCenterX = rX;
relativeCenterY = rY;
}
public void adjustOrigin(double cX, double cY){
double changeX = relativeCenterX-cX;
double changeY = relativeCenterY-cY;
for(int i = 0; i < fromX.size(); i++){
fromX.set(i,fromX.get(i)+changeX*zoom);
fromY.set(i,fromY.get(i)-changeY*zoom);
}
relativeCenterX = cX;
relativeCenterY = cY;
}
public void clear(){
fromX.clear();
fromY.clear();
distX.clear();
distY.clear();
}
public void drawLine(double fX, double fY, double tX, double tY){
fromX.add(fX);
fromY.add(fY);
distX.add(tX);
distY.add(tY);
}
public GPen returnGPen(Color c){
GPen pen = new GPen();
pen.setColor(c);
for(int i = 0; i < fromX.size(); i++){
pen.setLocation(fromX.get(i),fromY.get(i));
pen.drawLine(distX.get(i),distY.get(i));
}
return pen;
}
}
Of course a unexpected nice thing that came out of this was the idea that I can now quickly benchmark different drawing routines by creating different methods for each and calling what I'm interested in.

Animating a line between two Point objects

What's the simplest way to draw a line between two Point objects in a way
that will look like I am drawing that line in real time by hand?
For example:
Point a = new Point(5,20);
Point b = new Point(15,20);
How do I connect these points with a "moving" line?
In other words I want to make the user feel "motion" of some sort. Is there a simple way to do that?
Given two points, you can determine the equation of the line connecting them. The equation of a line is of the form y = mx + c, where m is the slope, and c is the y intercept.
So, given your two points (5,20) and (15,20), we first determine m.
m = (y2-y1)/(x2-x1)
= (20-20)/(15-5)
= (0)/10
= 0
Substituting into the equation for a straight line, we get y = 0x + c or y = c. Now that we know this, we simply need to know the points where y = c and 5<=x<=15. Simply draw each of these points in the normal way (look at this for the exact method) with a Thread.sleep() call in between drawing each point. In this case, you have only 11 points to draw, so it would make sense to draw 1 point every 100 ms. For details on Thread.sleep() see here.
EDIT: Since Thread.sleep() won't work on the EDT, look at javax.swing.Timer instead, as Uhlen suggested.
Following the answer by Chinmay Kanchi, you need to create a feeling of animation. As mentioned above in comments by Uhlen you should use Swing's Timer when working on EDT. To give you example of how to use Timer. Lets assume we have a panel and we want it to slide open on e.g. a button click, thus we need to animate it sliding open by increasing its size. Below is an example showing pretty much how you would use Timer to do the operations.
this.extendingTimer = new Timer(0, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//assume sliding is vertical
int value = maximumHeight;
//make sure the size will not be bigger then allowed maximum
if(currentExtensionSize + extensionRate >= value)
{
currentExtensionSize = value;
stopExtending();
}
else
currentExtensionSize += extensionRate;
setSize(new Dimension(maximumWidth, currentExtensionSize));
}
});
extendingTimer.setInitialDelay(0);
extendingTimer.setDelay(100);
extendingTimer.setRepeats(true);
int lineCount = 0; //global
// timer calls the below
xLocation = (*a)[a->size()-1] * timeSoFar / duration ;
if(xLocation > (*a)[lineCount+1]){
lineCount++;
}
double m = ((*b)[lineCount+1] - (*b)[lineCount])/((*a)[lineCount+1]-(*a)[lineCount]);
double yIntercept = (*b)[lineCount]-m*(*a)[lineCount];
yLocation = m * xLocation + yIntercept;
xLocation = (yLocation - yIntercept) / m;
this is in c++ and using vectors but its the theory we want. This allows for multiple lines not just one.

Categories