How do I stop the object from going outside the screen? - java

public class MovingBagView extends View {
private Bitmap bag[] = new Bitmap[2];
private int bagX;
private int bagY = 1000;
private int bagSpeed;
private Boolean touch = false;
private int canvasWidth, canvasHeight;
private Bitmap backgroundImage;
private Paint scorePaint = new Paint();
private Bitmap life[] = new Bitmap[2];
public MovingBagView(Context context) {
super(context);
bag[0] = BitmapFactory.decodeResource(getResources(), R.drawable.bag1);
bag[1] = BitmapFactory.decodeResource(getResources(), R.drawable.bag2);
backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.background);
scorePaint.setColor(Color.BLACK);
scorePaint.setTextSize(40);
scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
scorePaint.setAntiAlias(true);
life[0] = BitmapFactory.decodeResource(getResources(), R.drawable.heart);
life[1] = BitmapFactory.decodeResource(getResources(), R.drawable.heart_grey);
bagX = 10;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvasWidth = canvas.getWidth();
canvasHeight = canvas.getHeight();
canvas.drawBitmap(backgroundImage, 0, 0, null);
int minBagX = bag[0].getWidth();
int maxBagX = canvasWidth - bag[0].getWidth() * 3;
bagX = bagX + bagSpeed;
if (bagX < minBagX) {
bagX = minBagX;
}
if (bagX < maxBagX) {
bagX = maxBagX;
}
bagSpeed = bagSpeed + 2;
if (touch) {
canvas.drawBitmap(bag[1], bagX, bagY, null);
}
else {
canvas.drawBitmap(bag[0], bagX, bagY, null);
}
canvas.drawText("Score : ", 20, 60, scorePaint);
canvas.drawBitmap(life[0], 500, 10, null);
canvas.drawBitmap(life[0], 570, 10, null);
canvas.drawBitmap(life[0], 640, 10, null);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
touch = true;
bagSpeed = -12;
}
return true;
}
}
I can tap on the screen to move the object to the left, and the more I tap on it, the further it should move to the left. However, the problem is that the object moves too much to the right, and goes off the screen. I want it to stop at the edge, so its still visible on the screen. Furthermore, the object is positioned in the center bottom of the screen, how do I position it to the bottom left corner? You can see how it works by looking at the GIF below.

This condition is never applied on the code you've provided. It continues to exceed the max value.
if (bagX < maxBagX) {
bagX = maxBagX;
}
It should look like this:
if (bagX >= maxBagX) {
bagX = maxBagX;
}
And the maxBagX value should be canvasWidth - bag[0].getWidth() in order to achieve the right edge of the bag itself. (Unless you're using the multiply 3 times for a different reason, this should be the solution.)

Create object of the layout which ever you are using linear,relative or constraint then fetch width of the screen using that object using
object.getWidth();

I did it like this //the maintainoffscreen function will maintain the path to donot move off screen, and return true ,, else if path does not collide to any side this will return false, and you can move the path by setting offset.
private boolean maintainOffScreen(Path path, float px, float py){
boolean done = true;
RectF rectF = new RectF();
path.computeBounds(rectF,true);
float l = getPathWidth(path) - rectF.right;
float r = getPathWidth(path) + rectF.left;
float t = getPathHeight(path) - rectF.bottom;
float b = getPathHeight(path)+ rectF.top;
if(l < (-1) && r < getWidth() && b < getHeight() && t< (-1)){
//if path does not collide to any side//you can move your path here as well
//by setting Path.offset(px,py)
done = false;
offScreen = OffScreen.NOOFF; //im using these as enum according to my need,
//as this is saving which side of screen collides
}
else if(l >= (-1)){
path.offset(px+(l),py);
offScreen = OffScreen.LEFT;
offscrenn_xl = 1;
done = true;
}
else if(r >= getWidth()){
float s = getWidth() - r;
path.offset(px+(s),py);
offScreen = OffScreen.RIGHT;
offscrenn_xr = s;
done = true;
}
else if(b >= getHeight()){
float s = getHeight() - b;
path.offset(px,py+s);
offScreen = OffScreen.BOTTOM;
offscrenn_yb = s;
done = true;
}
else if(t >= (-1)){
path.offset(px,py+(t));
offScreen = OffScreen.TOP;
offscrenn_yt = t;
done = true;
}
return done;
}
confuse about px,py ? this is the movement of you finger with respect to path
`
final float px = event.getX() - this.startX;
final float py = event.getY() - this.startY;
if(!maintainOffScreen(path,px,py)){path.offset(px,py); }` //you should call
//this function here ,this will move your path to finger if there is no collision.
//and donot forgot to set the startx,and starty yo new positon like this
this.startX = event.getX();
this.startY = event.getY();

Related

Text is stretched on different screen in my custom view after settings text size in dp android

I am trying to create a text sticker in android. I am creating a text sticker on one of my phones and then saving the text size in the DP.
Then on another phone, I am loading the same text with the same DP and converting the DP value to pixel. Then calling the method
TextPaint.setTextSize(pixelValue)
But the output is different on both phones. I also tried replacing DP with SP, but it seems that's not the solution.
Also one more thing, I am determining the width and height of my view based on a drawable(res) width height.
Here is code of that drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<size android:width="200dp" android:height="50dp" />
</shape>
But this doesn't seem to be problem as the values are in DP in the drawable file.
Here is the constructor of text sticker.
public DynamicTextSticker(#NonNull Context context, #Nullable Drawable drawable, String text) {
this.context = context;
this.drawable = drawable;
if (drawable == null) {
this.drawable = ContextCompat.getDrawable(context, R.drawable.sticker_transparent_background);
}
textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
minTextSizePixels = DensityUtils.px2dp(context, 6);
maxTextSizePixels = DensityUtils.px2dp(context, 32);
alignment = Layout.Alignment.ALIGN_CENTER;
textPaint.setTextSize(maxTextSizePixels);
realBounds = new Rect(0, 0, getWidth(), getHeight());
textRect = new Rect(0, 0, getWidth(), getHeight());
}
draw method
#Override public void draw(#NonNull Canvas canvas) {
Matrix matrix = getMatrix();
canvas.save();
canvas.concat(matrix);
if (drawable != null) {
drawable.setBounds(realBounds);
drawable.draw(canvas);
}
canvas.restore();
canvas.save();
canvas.concat(matrix);
if (textRect.width() == getWidth()) {
int dy = getHeight() / 2 - staticLayout.getHeight() / 2;
// center vertical
canvas.translate(0, dy);
} else {
int dx = textRect.left;
int dy = textRect.top + textRect.height() / 2 - staticLayout.getHeight() / 2;
canvas.translate(dx, dy);
}
staticLayout.draw(canvas);
canvas.restore();
}
here is the resize method where the static layout is initialized
final int availableHeightPixels = textRect.height();
final int availableWidthPixels = textRect.width();
final CharSequence text = getText();
// Safety check
// (Do not resize if the view does not have dimensions or if there is no text)
if (text == null
|| text.length() <= 0
|| availableHeightPixels <= 0
|| availableWidthPixels <= 0
|| maxTextSizePixels <= 0) {
return this;
}
float targetTextSizePixels = maxTextSizePixels;
int targetTextHeightPixels =
getTextHeightPixels(text, availableWidthPixels, targetTextSizePixels);
// Until we either fit within our TextView
// or we have reached our minimum text size,
// incrementally try smaller sizes
while (targetTextHeightPixels > availableHeightPixels
&& targetTextSizePixels > minTextSizePixels) {
targetTextSizePixels = Math.max(targetTextSizePixels - 2, minTextSizePixels);
targetTextHeightPixels =
getTextHeightPixels(text, availableWidthPixels, targetTextSizePixels);
}
// If we have reached our minimum text size and the text still doesn't fit,
// append an ellipsis
// (NOTE: Auto-ellipsize doesn't work hence why we have to do it here)
if (targetTextSizePixels == minTextSizePixels
&& targetTextHeightPixels > availableHeightPixels) {
// Make a copy of the original TextPaint object for measuring
TextPaint textPaintCopy = new TextPaint(textPaint);
textPaintCopy.setTextSize(targetTextSizePixels);
// Measure using a StaticLayout instance
StaticLayout staticLayout =
new StaticLayout(text, textPaintCopy, availableWidthPixels, Layout.Alignment.ALIGN_NORMAL,
lineSpacingMultiplier, lineSpacingExtra, false);
// Check that we have a least one line of rendered text
if (staticLayout.getLineCount() > 0) {
// Since the line at the specific vertical position would be cut off,
// we must trim up to the previous line and add an ellipsis
int lastLine = staticLayout.getLineForVertical(availableHeightPixels) - 1;
if (lastLine >= 0) {
int startOffset = staticLayout.getLineStart(lastLine);
int endOffset = staticLayout.getLineEnd(lastLine);
float lineWidthPixels = staticLayout.getLineWidth(lastLine);
float ellipseWidth = textPaintCopy.measureText(mEllipsis);
// Trim characters off until we have enough room to draw the ellipsis
while (availableWidthPixels < lineWidthPixels + ellipseWidth) {
endOffset--;
lineWidthPixels =
textPaintCopy.measureText(text.subSequence(startOffset, endOffset + 1).toString());
}
setText(text.subSequence(0, endOffset) + mEllipsis);
}
}
}
textPaint.setTextSize(targetTextSizePixels);
this.targetTextSizePixel = targetTextSizePixels;
staticLayout =
new StaticLayout(this.text, textPaint, textRect.width(), alignment, lineSpacingMultiplier,
lineSpacingExtra, true);
return this;
here is the getWidth and getHeight method
#Override public int getWidth() {
return drawable.getIntrinsicWidth();
}
#Override public int getHeight() {
return drawable.getIntrinsicHeight();
}
I tried several ways but none of them worked for me.
I think the problem is not with text size. but the problem might be with drawable. But I don't know what possibly could go wrong with that, as I have used DP unit everywhere.
**If i forget to add any code please tell me I will attach it as well**
I am new to android so I am not sure the reason behind this problem.
Thanks for your help.

How to rotate one circle anti clockwise canvas android

I can't solve problem and I need help. I create planets around sun to rotate and I need one planet t rotate anti clockwise and my problem begins. I try everything I'm familiar. I'm beginner and this is simple code but I cant solve it. Please help. Thanks in advance.
I tried with getting anti clockwise just putting minus and this not work. I try with radius and angle and didn't work. I somewhere getting failed in code but I don't know where.
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenDimensions = getDisplayDimensions();
Bitmap bitmap = Bitmap.createBitmap(1200, 2000, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.parseColor("#FFFFFF"));
canvas = new Canvas();
canvas.setBitmap(bitmap);
paint = new Paint();
paint1 = new Paint();
paint2 = new Paint();
paint3 = new Paint();
paint4 = new Paint();
paint.setColor(Color.YELLOW);
paint1.setColor(Color.RED);
paint2.setColor(Color.BLUE);
paint3.setColor(Color.BLACK);
paint4.setColor(Color.MAGENTA);
imageView = findViewById(R.id.imageView3);
imageView.setImageBitmap(bitmap);
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(
new Runnable() {
#Override
public void run() {
animationFrame();
}
}
);
}
}, 2000, 80);
}
private int[] getDisplayDimensions() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
return new int[]{width, height};
}
private void animationFrame() {
canvas.drawColor(Color.parseColor("#EBEBEB"));
paint.setColor(Color.YELLOW);
canvas.drawCircle(500f, 750f, 130f, paint);
if (paint1 != null | paint3 != null) {
paint1.setColor(Color.RED);
canvas.drawCircle(400f, 560f, 51f, paint1);
canvas.rotate(10f, 500f, 750f);
paint3.setColor(Color.BLACK);
canvas.rotate(30f, 500f, 750f);
canvas.drawCircle(350f, 1050f, 45, paint3);
}
if (paint2 != null | paint4 != null) {
paint2.setColor(Color.MAGENTA);
canvas.rotate(-10f, 500f, 750f);
canvas.drawCircle(720f, 950f, 48, paint2);
paint4.setColor(Color.GRAY);
canvas.rotate(-25f, 500f, 750f);
canvas.drawCircle(290f, 330f, 42, paint4);
}
imageView.invalidate();
}
}
...
I expected to one of planets get rotation anticlockwise.
EDIT
This code works perfectly for me:
In your case, the rotationPoint is the center point of sun and the bodyCenter is the center point of a planet.
Final points cx and cy are the center point of a planet after rotation around the sun.
class Vector {
float x;
float y;
Vector(float x, float y){
this.x = x;
this.y = y;
}
}
public Vector getRotatedPoint(boolean isClockwise, float rotation, Vector bodyCenter, Vector rotationPoint){
float sin = MathUtils.sin(rotation * MathUtils.degreesToRadians);
float cos = MathUtils.cos(rotation * MathUtils.degreesToRadians);
float xSin = (bodyCenter.x - rotationPoint.x) * sin;
float xCos = (bodyCenter.x - rotationPoint.x) * cos;
float ySin = (bodyCenter.y - rotationPoint.y) * sin;
float yCos = (bodyCenter.y - rotationPoint.y) * cos;
float cx, cy;
if (isClockwise) {
//For clockwise rotation
cx = rotationPoint.x + xCos + ySin;
cy = rotationPoint.y - xSin + yCos;
} else {
//For counter clockwise rotation
cx = rotationPoint.x + xCos - ySin;
cy = rotationPoint.y + xSin + yCos;
}
return new Vector(cx, cy);
}
to use it you have to call first the method and then draw a circle
Vector newCenter = getRotatedPoint(true, 10f, planetCenter, sunCenter)
canvas.drawCircle(newCenter.x, newCenter.y, 48, paint2);

How to get coordinates of click on resized ImageView in JavaFX Gridpane

Currently, there is a disconnect between where I click and the coordinates that are printed to terminal. It seems it is basing the coordinates off of the center GridPane but not the image itself of the imageView.
Previously, I would not resize the images in this application but rather in an automated photoshop process and the click/coordinates would be in sync.
Here is where I make an arrayList of resized Images which will be displayed in the GridPane
//Makes imageView arraylist from all images in a given directory
private ArrayList<ImageView> makeImageViewArr(File imagesDir) {
//transer file names from directory folder to string array
File[] strImageList = imagesDir.listFiles();
myMouseHandler mouseHandler = new myMouseHandler();
//instantiate imageview arraylist
arrImageList = new ArrayList<ImageView>();
//get files from folder & start at 1 to ignore ds.Store
for(int count = 1; count < strImageList.length; count++) {
ImageView imgView =
new ImageView(strImageList[count].toURI().toString());
imgView.setFitHeight(360);
imgView.setFitWidth(640);
imgView.setPreserveRatio(true);
imgView.setSmooth(true);
imgView.setOnMouseClicked(mouseHandler);
arrImageList.add(imgView);
}
return arrImageList;
}//END METHOD
Here is the class where I handle the actual click on the imageView
//inner class for mouse input
public class myMouseHandler implements EventHandler<MouseEvent>
{
/* Method which handles & executes mouse clicks
*
* #param e KeyEvent the code from the mouse click
*
* returns none
*/
#Override
public void handle(MouseEvent e)
{
//reset image to erase previous box
pane.setCenter(arrImageList.get(index));
//get image that was clicked on and pixel reader from image
Image clickedImg = arrImageList.get(index).getImage();
PixelReader pxlRdr = clickedImg.getPixelReader();
//get image height/width for copy
int clickedImgW = (int) clickedImg.getWidth();
int clickedImgH = (int) clickedImg.getHeight();
//get x and y coordinates from click
int xCord = (int) e.getX();
int yCord = (int) e.getY();
nudgeX = xCord;
nudgeY = yCord;
//create writeable image and pixelwriter to draw click region
WritableImage outlineImg = new WritableImage(pxlRdr,
clickedImgW, clickedImgH);
PixelWriter pxlWrtr;
pxlWrtr = outlineImg.getPixelWriter();
//draws region
drawRegion(pxlWrtr, xCord, yCord);
//display image with click boundary and link mouseHandler
//to refresh on next click
ImageView tempImg = new ImageView(outlineImg);
tempImg.setFitHeight(360);
tempImg.setFitWidth(640);
tempImg.setPreserveRatio(true);
tempImg.setSmooth(true);
myMouseHandler mouseHandler = new myMouseHandler();
tempImg.setOnMouseClicked( mouseHandler );
pane.setCenter( tempImg );
//print relevant info about click region
System.out.println("xCord: " + xCord);
System.out.println("yCord: " + yCord + "\n");
}//END METHOD
}//END INNER CLASS
This simply draws a region to show where the click has occurred.
//Draws region boundary after user clicks on image
private void drawRegion(PixelWriter pxlWrtr, int xCord, int yCord) {
Image clickedImg = arrImageList.get(index).getImage();
PixelReader pxlRdr = clickedImg.getPixelReader();
//get image height/width for copy
int clickedImgW = (int) clickedImg.getWidth();
int clickedImgH = (int) clickedImg.getHeight();
//draw right vert boundary
for( int yTrack = (yCord + 1); yTrack > (yCord - regionSize ); yTrack--) {
if((yTrack > 0 && yCord < clickedImgH - 1)
&& ( xCord < clickedImgW - 1 )) {
pxlWrtr.setColor(xCord + 1, yTrack, Color.RED);
}
}
//draw left vert boundary
for( int yTrack = (yCord + 1); yTrack > (yCord - regionSize); yTrack--) {
if((yTrack > 0 && yCord < clickedImgH - 1)
&& (xCord - regionSize) >= 0) {
pxlWrtr.setColor(xCord - regionSize, yTrack, Color.RED);
}
}
//draw bottom boundary
for(int xTrack = (xCord + 1); xTrack >= (xCord - regionSize); xTrack--) {
if(xTrack > 0 && yCord < clickedImgH - 1) {
pxlWrtr.setColor(xTrack, yCord + 1, Color.RED);
}
}
//draw top boundary
for(int xTrack = (xCord + 1); xTrack >= (xCord - regionSize); xTrack--) {
if(xTrack >= 0 && (yCord - regionSize) >= 0 ) {
pxlWrtr.setColor(xTrack, yCord - regionSize, Color.RED);
}
}
}//END METHOD
According to the documentation for preserveRatio:
…the dimensions of this node as reported by the node's bounds will be equal to the size of the scaled image…
So you can use that to scale the MouseEvent coordinates:
double x = e.getX();
double y = e.getY();
ImageView view = (ImageView) e.getSource();
Bounds bounds = view.getLayoutBounds();
double xScale = bounds.getWidth() / view.getImage().getWidth();
double yScale = bounds.getHeight() / view.getImage().getHeight();
x /= xScale;
y /= yScale;
int xCord = (int) x;
int yCord = (int) y;

Combining two codes with a mousePressed-event processing

My problem is following:
I wrote a code and managed to display an image when i click on a rectangle (with the loadImage-fuction). The rectangle serves as a button that I want to replace with an image later.
But i actually don't just want an image to be displayed when the button is clicked. I want to call a code, to copy an image onto another:
public static int SQUARE_WIDTH = 30;
public static int SQUARE_HEIGHT = 30;
PImage img1,img2, img3;
void setup() {
size(670, 943);
img1 = loadImage("white.png");
img2 = loadImage("hase.jpg");
img3= loadImage("ohrring.jpg");
image(img1,0,0);
}
void draw() {
if(mousePressed)
copy(img2,
constrain(mouseX-SQUARE_WIDTH/2,0,width),
constrain(mouseY-SQUARE_HEIGHT/2,0,height),
SQUARE_WIDTH,SQUARE_HEIGHT,
constrain(mouseX-SQUARE_WIDTH/2,0,width),
constrain(mouseY-SQUARE_HEIGHT/2,0,height),
SQUARE_WIDTH,SQUARE_HEIGHT);
}
The copy-code doesn't simply copy an image, it uses the mouse as a brush! When you "draw" on an area, the image shows with the "strokes" of the brush pixel after pixel!
processing.org/reference/copy_.html
I happen to have huge problems when I want to combine this one with my main code:
int rectX, rectY;
int rectSize = 90;
boolean rectOver = false;
color rectHighlight;
color currentColor, baseColor;
color rectColor;
public static int SQUARE_WIDTH = 30;
public static int SQUARE_HEIGHT = 30;
PImage img1,img2, img3;
void setup() {
size(670, 943);
rectColor = color(0);
rectX = width/2-rectSize-10;
rectY = height/2-rectSize/2;
baseColor = color(102);
currentColor = baseColor;
img1 = loadImage("frida.jpg");
img2 = loadImage("hase.jpg");
img3 = loadImage("white.png");
background(img3);
}
void draw() {
update(mouseX, mouseY);
if (rectOver) {
fill(rectHighlight);
} else {
fill(rectColor);
}
stroke(255);
rect(rectX, rectY, rectSize, rectSize);
}
void update(int x, int y) {
if ( overRect(rectX, rectY, rectSize, rectSize) ) {
rectOver = true;
}else {
rectOver = false;
}
}
void mousePressed() {
if (rectOver) {
background(img2);
}
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
Theoretically I got the tip to set a boolean in mousePressed() to do the copy-operation in draw(), and then to check this boolean in draw(): if set (true) it shall do the copy. But I'm unfortunately not the brightest star in the programming-sky , so could anybody show me what this part is supposed to look like? Of course, I'm open to other suggestions how to solve this problem!
Thank you!
I hope this is what you are looking for. If you want to copy an image you don't need to call a function to copy an image, you can simply invoke the = sign and the image will be copied.
In my example code buttonImage is the image on the button. Whenever you don't want an image on the button assign it the following way:
buttonImage = null;
If you want to have an image instead of the rectangle do the following:
buttonImage = yourImage;
buttonImage.resize(w, h); //fit it on the button.
I think this is what you want to achieve?
PImage buttonImage;
void setup()
{
}
void draw()
{
if(buttonImage == null || buttonImage.width < 2) rect(x, y, w, h);
else image(buttonImage, x, y);
}
void mouseReleased()
{
if(mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h)
{
//copy any image onto the buttonImage
buttonImage = loadImage("newPath.png"); //update / overwrite image
buttonImage.resize(w, h); //fit it on the button
}
}
x and y are the position of the button, w and h are the width and the height of the button in my example.
EDIT:
Ok, so basically you want to have a white background and you want to scrap it using your tool so an image appears? I'm still not 100% sure of what you're asking, but if that is the case try this:
I used img.get() instead of img.copy(), because it has less parameters to deal with. I really hoped i understood this correctly, if not maybe link a video to something similar? I have a hard time understanding what you want.
The toolSelected integer is a counter to which tool you are using. Depending on its value, it is executing a different code.
My code:
PImage img1;
int toolSelected = 0; //Normal tool;
int widthOfBrush = 20; //Now you are drawing 20x20
int buttonX = 200;
int buttonY = 200;
int buttonW = 40;
int buttonH = 20;
void setup()
{
size(640, 480);
background(255);
img1 = loadImage("yourImg.png");
img1.resize(width, height); //Fit it on processing screen
}
void draw()
{
if(toolSelected == 0) {}//other tool
//Instead of using copy we will be using buttonImage.get(x, y, w, h) --> makes more sense
if(toolSelected == 1 && mousePressed)
{
float yourX = mouseX;
float yourY = mouseY;
yourX -= floor(widthOfBrush / 2);
yourY -= floor(widthOfBrush / 2);
//scale & copy:
PImage brushImage = img1.get((int)yourX, (int)yourY, widthOfBrush * (width / img1.width), widthOfBrush * (width / img1.width)); //Draw the image at your destination
image(brushImage, mouseX, mouseY);
}
stroke(0);
fill(255);
rect(buttonX, buttonY, buttonW, buttonH);
}
void mouseReleased()
{
if (mouseX > buttonX && mouseX < buttonX + buttonW && mouseY > buttonY && mouseY < buttonY + buttonH)
{
//copy any image onto the buttonImage
//buttonImage = loadImage("newPath.png"); //update / overwrite image
toolSelected = 1; //Our tool is the image brush now.
}
}

OpenGl ES on android fine tuning of ray picking code

Ive implemented ray picking using min3d and some code I found online but when I implemented it I notice that its off a bit some times meaning that when I click on one box sometimes the wrong one is picked. Sometimes the one picked isnt even on the screen. This took quite a bit of code to get implemented in the first place but the main two methods I call are the following:
private void rayPicking(float x, float y)
{
//intersection with near plane
float[] near = new float[4];
GLU.gluUnProject(x, scene.getViewport()[3] - y, 0f, scene.getGrabber().mModelView, 0, scene.getGrabber().mProjection, 0, scene.getViewport(), 0, near, 0);
if (near[3] != 0)
{
near[0] = near[0] / near[3];
near[1] = near[1] / near[3];
near[2] = near[2] / near[3];
}
Number3d near3 = new Number3d(near[0], near[1], near[2]);
//and far plane
float[] far = new float[4];
GLU.gluUnProject(x, scene.getViewport()[3] - y, 1f, scene.getGrabber().mModelView, 0, scene.getGrabber().mProjection, 0, scene.getViewport(), 0, far, 0);
if (far[3] != 0)
{
far[0] = far[0] / far[3];
far[1] = far[1] / far[3];
far[2] = far[2] / far[3];
}
Number3d far3 = new Number3d(far[0], far[1], far[2]);
Box firstPicked = null;
Box currentModel = null;
Number3d currentCoords = null;
Number3d firstPickedCoords = new Number3d();
if (!sceneBoxes.isEmpty())
{
//here we check each model if it was tapped and if several models were tapped, we take only that which is closer to the near clipping plane
Iterator<Box> itr = sceneBoxes.iterator();
while (itr.hasNext())
{
currentModel = itr.next();
currentCoords = new Number3d(currentModel.position().x, currentModel.position().y, currentModel.position().z);
if (picked (far3, near3, currentCoords, 1.2f))
if (firstPicked==null)
{
firstPicked = currentModel;
firstPickedCoords.setAllFrom(currentCoords);
}
else if (Number3d.add(currentCoords, near3).length() < Number3d.add(firstPickedCoords, near3).length())
{
firstPicked = currentModel;
firstPickedCoords.setAllFrom(currentCoords);
}
}
}
if (firstPicked != null) // if model picked
{
String temp = firstPicked.getTitle();
String temp2 = firstPicked.getLink();
Log.w("3d touch working "+temp, "3d touch working "+temp);
Intent intent = new Intent(CurAct.this, WebViewer.class);
intent.putExtra("clkURL", temp2);
intent.putExtra("clkUID", curr_uid);
intent.putExtra("rid", curr_rid);
startActivity(intent);
firstPicked = null;
temp = null;
temp2 = null;
}
}
private boolean picked(Number3d a, Number3d b, Number3d q, float r)
{
float ab = (float) Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
float aq = (float) Math.sqrt((a.x-q.x)*(a.x-q.x)+(a.y-q.y)*(a.y-q.y)+(a.z-q.z)*(a.z-q.z));
float bq = (float) Math.sqrt((b.x-q.x)*(b.x-q.x)+(b.y-q.y)*(b.y-q.y)+(b.z-q.z)*(b.z-q.z));
float p = (ab + aq + bq) / 2;
float hh = (float) Math.sqrt(p * (p - ab) * (p - aq) * (p - bq));
float h;
if (ab!=0) h = 2 * hh / ab; else h = 2*hh;
if (aq<h) h = aq;
if (bq<h) h = bq;
if (h<r)return true; else return false;
}
Now I have also tried implementing frustum culling but having done that it slows things way down because I didnt do much to optimize it and its not running natively. But if some one can look and maybe give me some pointers on how to make it more accurate that would be awesome. Heres where I call the picking methods.
_glSurfaceView.setOnTouchListener(
new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent e) {
switch (e.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
startX = e.getX();
startY = e.getY();
trace = 0.0f;
time = System.currentTimeMillis();
touchMode = DRAG;
break;
case MotionEvent.ACTION_UP:
// we use ray picking only when the tap wasn't longer than 0.5 sec and if we almost didn't move our finger
//Log.w("this is touch y"+e.getY()+" viewport is"+scene.getViewport()[3], "this is touch x"+e.getX());
if ((System.currentTimeMillis()-time < 500) && (trace<scene.getViewport()[3]*0.075)) rayPicking(e.getX(), e.getY());
time = 0;
case MotionEvent.ACTION_POINTER_UP:
touchMode = NONE;
break;
}
return true;
}
}
);
The bug you have sounds like you are making a mistake in you collision detection, I had a bug in the function I had for dot products and caused some really odd results. Can you post more of your collision algorithm?
I implemented ray picking here: http://android-raypick.blogspot.ca/ I did not paste the code here because there is allot of it, feel free to compare your code to this code, this code is tested and working.

Categories

Resources