Can't get multitouch to work in Unity3D - android

after searching for a solution I still can't figure out why my multitouch script in unity isn't working. This is my code. And before you ask: All variables do exist.
void Update()
{
if (Input.touchCount > 0)
{
for (i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase != TouchPhase.Ended)
{
hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
if (hit.collider != null && hit.transform.gameObject.tag == "Links")
{
cannon.GetComponent<Rigidbody2D>().MovePosition(cannon.GetComponent<Rigidbody2D>().position + new Vector2(-0.1f, 0) * Time.deltaTime * moveSpeed);
}
else if (hit.collider != null && hit.transform.gameObject.tag == "Rechts")
{
cannon.GetComponent<Rigidbody2D>().MovePosition(cannon.GetComponent<Rigidbody2D>().position + new Vector2(0.1f, 0) * Time.deltaTime * moveSpeed);
}
}
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
if (hit.collider != null && hit.transform.gameObject.tag == "Fire")
{
clone = Instantiate(projectile, cannon.transform.position + new Vector3(0, 1.3f, 0), transform.rotation) as Rigidbody2D;
clone.velocity = new Vector2(0, speed);
}
}
}
}
}
It only registers one input at a time. Yes my phone does support multitouch. I'll appreciate any kind of help.

your problem is very simple !
You have a "0" were you should have an "i". That's all it is.
You are looping through with i ...
for (i = 0; i < Input.touchCount; i++)
sometimes you correctly refer to
GetTouch(i)
but at other times you incorrectly refer to
GetTouch(0)
fortunately that's all it is!
Don't forget you can easily solve such problems in the future by logging as you go (use Debug.Log, or, have a Text on the screen and write your development info there, dev.text = "blah" )

Related

How can I use libusb to send/receive data from ubuntu to Android?

The goal I'm looking to achieve is to send/receive data to/from an Android device from a Linux server. I feel like I've tried all the examples I could find on the Internet as well as reading the libusb APIs, which don't talk at my level of a total noob. Here's what I've got so far:
I've written a program that runs on a Ubuntu server (ARMx64), from examples, I have the following code:
//Error handling removed for brevity
#define BULK_EP_IN 0x08
main(int argc, char* argv) {
libusb_context* libusb_ctxt;
int status;
bool libusb_err = false;
libusb_device** libusb_dvcs = NULL;
bool error = false;
unsigned vid = 0, pid = 0;
unsigned busnum = 0, devaddr = 0, _busnum, _devaddr;
struct libusb_device_descriptor desc;
status = libusb_init(&libusb_ctxt);
if (status < 0) {
libusb_err = true;
}
if (libusb_get_device_list(NULL, &libusb_dvcs) < 0) {
error = true;
} else {
libusb_device *dev = NULL;
for (uint16_t idx = 0; (dev = libusb_dvcs[idx]) != NULL; idx++) {
_busnum = libusb_get_bus_number(dev);
_devaddr = libusb_get_device_address(dev);
status = libusb_get_device_descriptor(dev, &desc);
if (status >= 0) {
if (desc.idVendor == vid_arg && desc.idProduct == pid_arg) {
vid = desc.idVendor;
pid = desc.idProduct;
busnum = _busnum;
devaddr = _devaddr;
fnd_dev = dev;
break;
}
}
}
if (fnd_dev == NULL) {
error = true;
} else {
libusb_device_handle* dev_hnd = NULL;
//Connect to device and write data to it.
dev_hnd = libusb_open_device_with_vid_pid(NULL, vid, pid);
if (dev_hnd) {
char str1[64], str2[64];
int e = 0,config2;
char my_string[64];
int length = 0;
int transferred = 0;
e = libusb_get_string_descriptor_ascii(dev_hnd, desc.iManufacturer, (unsigned char*)str1, sizeof(str1));
if (e < 0) {
break;
}
e = libusb_get_string_descriptor_ascii(dev_hnd, desc.iProduct, (unsigned char*)str2, sizeof(str2));
if(e < 0) {
break;
}
e = libusb_get_configuration(dev_hnd, &config2);
if (e == 0) {
if (config2 != 1)
{
libusb_set_configuration(dev_hnd, 1);
if (e!=0) {
libusb_err = true;
}
}
if (!libusb_err) {
if (libusb_kernel_driver_active(dev_hnd, 0) == 1) {
if (libusb_detach_kernel_driver(dev_hnd, 0) == 1) {
libusb_err = true;
}
}
if (!libusb_err) {
e = libusb_claim_interface(dev_hnd, 0);
if (e < 0) {
libusb_err = true;
}
if (!libusb_err) {
active_config(dev, dev_hnd);
memset(my_string, '\0', 64);
strcpy(my_string, "hello sally");
length = strlen(my_string);
e = libusb_bulk_transfer(dev_hnd, BULK_EP_IN | LIBUSB_ENDPOINT_IN, (unsigned char*)my_string, length, &transferred, 0);
if (e == 0 && transferred == length) {
child_log("Write successful!");
child_log("Sent %d bytes with string: %s", transferred, my_string);
}
else {
child_log("Error in write! e = %d and transferred = %d, error desc: %s", e, transferred, libusb_error_name(e));
}
}
}
}
}
e = libusb_release_interface(dev_hnd, 0);
libusb_close(dev_hnd);
}
}
if (libusb_dvcs != NULL) {
libusb_free_device_list(libusb_dvcs, 1);
}
}
if (!libusb_err) {
libusb_exit(libusb_ctxt);
}
}
Everything works, at least doesn't report an error, until I get to the libusb_bulk_transfer call, which it returns a -1, which translates to a LIBUSB_ERROR_IO.
The USB device I'm connecting to is a Samsung Z-Fold3 mobile phone. I'm using the phone as a test OS as I don't have access to the Android tablet that will be used in the final application.
Here's my questions:
How do I know which interface to connect to?
 a) I'll have an application running on the Android device that will be using the same library, hopefully, that will receive the data sent from the Ubuntu server.
How do I know which configuration to use?
Am I doing everything correctly?
Does anyone have any code that can run on the Android to read the data that I'm sending from this code? My goal was to write a program in C that pretty much runs like the one that runs on the Ubuntu server, but reads the data. Once I have that, my next step will be to write data from the Android device to the Ubuntu server using libusb again.
Any help, advice, alternative APIs is very much appreciated.
Thank you!

Ionic Android TV

I'm doing at APK on Ionic for Android TV and I have a problem with the controls of arrows.
The APK run of the device but his behavior not is correct. When use arrows for a shift of app, the focus not work that must be. It jumps some divs.
Here some of the code that this app uses:
TS:
matrixNodes: Array<any> = [];
matrixPosX: number = 0;
matrixPosY: number = 0;
.
.
.
keyboardAction(e) {
let activeEle: any = this.matrixNodes[this.matrixPosY][this.matrixPosX];
let parent: any = activeEle.parentNode;
let moveRight = -224;
let moveLeft = +224;
if (this.matrixPosY != 0) {
moveRight = -340;
moveLeft = +340;
}
if (e.key == "ArrowRight") {
if (this.matrixPosX < (this.matrixNodes[this.matrixPosY].length - 1)) {
this.matrixPosX++;
this.matrixNodes[this.matrixPosY][this.matrixPosX].focus();
//SCROLL
if (parent.style.left == 0 + 'px' || parent.style.left == 0) {
if(this.matrixPosY != 0){
console.log(this.matrixPosY)
parent.style.left = '-350px';
}else
parent.style.left = '-240px';
} else {
let old = parent.style.left;
old = old.replace("px", "");
parent.style.left = (moveRight + parseInt(old)) + "px";
}
////////////////
}
} else if (e.key == "ArrowLeft") {
if (this.matrixPosX > 0) {
this.matrixPosX--;
this.matrixNodes[this.matrixPosY][this.matrixPosX].focus();
//SCROLL
if (parent.style.left == 0 + 'px' || parent.style.left == 0) {
if(this.matrixPosY != 0)
parent.style.left = '350px';
else
parent.style.left = '238px';
} else {
let old = parent.style.left;
old = old.replace("px", "");
parent.style.left = (moveLeft + parseInt(old)) + "px";
}
/////////////////////////////
} else {
//Moverse al TAB
let tab = document.getElementById("tab-button-home");
tab.focus();
}
This function(keyboardAction) its called with event keydown.
I want know why jumps some divs.. if is because there are use other event or other elements at HTML
When 1 line of code is the solution...
e.preventDefault()

ExoPlayer useArtwork never being called, Thumbnail not shown

I am trying to load a thumbnail but the video becomes enabled and setDrawableArtwork(defaultArtwork) is never called.
I am setting defaultArtwork with
public void setDefaultArtwork(#Nullable Drawable defaultArtwork) {
if (this.defaultArtwork != defaultArtwork) {
this.defaultArtwork = defaultArtwork;
updateForCurrentTrackSelections(/* isNewPlayer= */ false);
}
}
But when this selections for loop runs the render type is TRACK_TYPE_VIDEO. And the artwork is never shown.
useArtwork is true.
TrackSelectionArray selections = player.getCurrentTrackSelections();
for (int i = 0; i < selections.length; i++) {
if (player.getRendererType(i) == C.TRACK_TYPE_VIDEO && selections.get(i) != null) {
// Video enabled so artwork must be hidden. If the shutter is closed, it will be opened in
// onRenderedFirstFrame().
hideArtwork();
return;
}
}
// Video disabled so the shutter must be closed.
closeShutter();
// Display artwork if enabled and available, else hide it.
if (useArtwork) {
for (int i = 0; i < selections.length; i++) {
TrackSelection selection = selections.get(i);
if (selection != null) {
for (int j = 0; j < selection.length(); j++) {
Metadata metadata = selection.getFormat(j).metadata;
if (metadata != null && setArtworkFromMetadata(metadata)) {
return;
}
}
}
}
if (setDrawableArtwork(defaultArtwork)) {
return;
}
}
ExoPlayer : implementation 'com.google.android.exoplayer:exoplayer:2.9.0'
Are there any ExoPlayer experts out there who can help me?
Edit :
In the meantime im probably just going to build a ImageView over PlayerView to handle thumbnails.
https://github.com/google/ExoPlayer/issues/4639

Android: How I can take Bracketing photos using SonyCamera remote API

This issue is troubling me until 1 week. I want to take picture like bracketing but in Sony Camera API its not support. so I coded it like continuously take 5 pictures with different value (0,+2,-2,-4,+4) using loop. (using button for it, and when press the button it should take picture 5 times) U can see the code in below:
This code for set Shutter Speed on camera
This code for taking picture:
private void takePicture() {
if (mLiveviewSurface == null || !mLiveviewSurface.isStarted()) {
DisplayHelper.toast(getApplicationContext(), R.string.msg_error_take_picture);
return;
}
new Thread() {
#Override
public void run() {
try {
JSONObject replyJson = mRemoteApi.actTakePicture();
JSONArray resultsObj = replyJson.getJSONArray("result");
JSONArray imageUrlsObj = resultsObj.getJSONArray(0);
String postImageUrl = null;
if (1 <= imageUrlsObj.length()) {
postImageUrl = imageUrlsObj.getString(0); continousShottingWithDifferentShutterValue();
}
if (postImageUrl == null) {
Log.w(TAG, "takePicture: post image URL is null.");
DisplayHelper.toast(getApplicationContext(), //
R.string.msg_error_take_picture);
return;
}
// Show progress indicator
DisplayHelper.setProgressIndicator(SonyCameraActivity.this, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
and this is for loop:
int val, posVal = 0;
int currentShutterSpeed = 0;
private void continousShottingWithDifferentShutterValue() {
if (val == 0) {
currentShutterSpeed = -5;
setShutterSpeed(currentShutterSpeed);
val++;
} else if (val == 1) {
currentShutterSpeed = 5;
setShutterSpeed(currentShutterSpeed);
val++;
} else if (val == 2) {
currentShutterSpeed = -10;
setShutterSpeed(currentShutterSpeed);
val++;
} else if (val == 3) {
currentShutterSpeed = 10;
setShutterSpeed(currentShutterSpeed);
val++;
} else if (val == 4) {
setShutterSpeedVal0(0);
posVal++;
}
if (posVal == 3) {
posVal = 0;
val = 0;
}
}
But when I take picture sometimes shutterSpeed or takePicture is getting error and loop is stop.
Error types are: setShutterSpeed IOExeption error: 500; or setShutterSpeed is not set. Someone works with Sonycamera remote API and has any idea how to fix it or take picture 5 times with different value. will thankful for any idea. Thanks
I solved the problem. it was a shutter speed value error. As u know in Sony Camera Api there is not have the value of shutter speed and I wrote it as in camera settings. and JSON request and response is not match so its shows error 500. If someone wants to use shutter speed value: here it is:
String shutterValue[] = {"30\"", "25\"", "20\"", "15\"", "13\"", "10\"", "8\"", "6\"",
"5\"", "4\"", "3.2\"", "2.5\"", "2\"", "1.6\"", "1.3\"", "1\"", "0.8\"", "0.6\"", "0.5\"",
"0.4\"", "1/3", "1/4", "1/5", "1/6", "1/8", "1/10", "1/13", "1/15", "1/20", "1/25", "1/30",
"1/40", "1/50", "1/60", "1/80", "1/100", "1/125", "1/160", "1/200", "1/250", "1/320", "1/400",
"1/500", "1/640", "1/800", "1/1000", "1/1250", "1/1600", "1/2000", "1/2500", "1/3200", "1/4000"};

Does c++/Arduino handle '?' differently compared with other chars

I have an Android app that is communicating with and arduino board via bluetooth
All the commands are going backward and forwards fine, until I wanted to send a comand of the type
"aT?bb"
from the android app, however when I print it in ardunio I am getting
"aT%3F"
I am logging the command in android and it is formed correctly My quesion is does c++/Arduino handle '?' differently then normal chars?
here is my arduino code->
while(bluetooth.available())
{
char toSend = (char)bluetooth.read();
if(toSend != '\0'){
if (toSend == 'a'){ i=0 ;}
inMsg[i] = toSend;
i++;
}
}
if(i == 5 )
{
// mock sending queries
if(inMsg[2] == '?'){
if(inMsg[1] == 'T'){
bluetooth.write("ty1");Serial.println("");
}else if(inMsg[1] == 'x'){ //normal cycle
bluetooth.write("xx1");
}else if(inMsg[1] == 'X'){ Serial.println(""); //booter
bluetooth.write("XX0");
}else if(inMsg[1] == 'N'){Serial.println(""); //On time
bluetooth.write("on1");
}else if(inMsg[1] == 'F'){ Serial.println(""); //Off time
bluetooth.write("of30");
}else if(inMsg[1] == 'S'){ Serial.println(""); //Speed percent
bluetooth.write("sp30");
}
}
// write to console
for(int j = 0; j < 5; j++){
Serial.write(inMsg[j]);
}
// new line
if(i == 5){Serial.println("");}
i = 0; // reset buffer
}
aT%3F <- this is mal formed
aS133 <- all the other are as I sent them from android
aN169
aF192
aS200
aXXXX
aYYYY
ayYYY
axXXX
my Android Code
...
command = "aT?bb";
writeCommand(command);
...
private void writeCommand(String command)
{
for (BluetoothGattCharacteristic characteristic : characteristics)
{
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) >0)
{
try {
characteristic.setValue(URLEncoder.encode(command, "utf-8"));
gatt.writeCharacteristic(characteristic);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
As pointed out in the comments above it was the URLEncoder that was changing the String. I have now changed this method to
private void writeCommand(String command)
{
for (BluetoothGattCharacteristic characteristic : characteristics)
{
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) >0)
{
characteristic.setValue(command);
gatt.writeCharacteristic(characteristic);
}else{
Log.i(TAG,"non write able");
}
}
}

Categories

Resources