I am currently developing an android game using LibGdx. I am trying to get the multi touch functionality working as the game requires the player to have one thumb to control the character and the other to click buttons.
Code:
for (int i = 0; i < 2; i++) {
if (Gdx.input.isTouched(i)) {
final int iX = Gdx.input.getX(i);
if (iX > screenwidth - screenwidth / 14) {
buttontouch = true;
} else {
buttontouch = false;
}
if (iX <= screenwidth - screenwidth / 14) {
playertouch = true;
}else{
playertouch = false;
}
}
}
if (playertouch){
etc...
}
if(buttontouch){
etc...
}
The player can be moved and the buttons pressed, but not at the same time :( ... which is what i need.
Any help would be greatly appreciated! Thanks in advance.
Logic which moves your character should be called inside for circle. You should move your character every circle iteration (if button was clicked) Like this:
for (int i = 0; i < 2; i++) {
if (Gdx.input.isTouched(i)) {
final int iX = Gdx.input.getX(i);
if (iX > screenwidth - screenwidth / 14) {
buttontouch = true;
} else {
buttontouch = false;
}
if (iX <= screenwidth - screenwidth / 14) {
playertouch = true;
}else{
playertouch = false;
}
}
//Logic here!
if (playertouch){
etc...
}
if(buttontouch){
etc...
}
}
Ok I seemed to have worked out an answer.
added
if (Gdx.input.isTouched(i)) {
below in each boolean if
for (int i = 0; i < 2; i++) {
if (Gdx.input.isTouched(i)){
final int iX = Gdx.input.getX(i);
if (iX > screenwidth - screenwidth / 14) {
buttontouch = true;
} else {
buttontouch = false;
}
if (iX <= screenwidth - screenwidth / 14) {
playertouch = true;
}else{
playertouch = false;
}
}
}
//Logic here!
if (playertouch){
if (Gdx.input.isTouched(i)) {
etc...
}
}
if(buttontouch){
if (Gdx.input.isTouched(i)) {
etc...
}
}
}
Related
I am new to coding and don't really know what I am doing. I know that there my code is pretty inefficient and I could probably fix it later (i.e. collision and movement), but I have what I think is an efficient map setup. It only renders what is either around the camera or is around the player based on the camera type (freeRoam vs placerCentered). This works and runs fine until I start to resize pictures when I zoom in and out my map. I think that every time I draw a resized image my computer resizes it and then draws it for every instance of it rather than resizing it once and drawing it. Would I have to make images for every instance of zooming in or out or is there an optimized resizing tool that I am missing?
Google Drive link with images if anyone is interested: https://drive.google.com/drive/folders/1s5_9YPX7_6QWaxZPexN7pWk8zgmaT0w5?usp=sharing
(All images are 20 x 20 I think besides warrior1 and stoneTile1 which are both 32 x 32)
import processing.core.*;
import processing.event.MouseEvent;
import java.util.*;
public class Dungeon1 extends PApplet implements MyLibrary {
static gameState currentState;
static cameraState currentCameraState = cameraState.playerCentered;
enum gameState {
Over, Running
}
enum cameraState {
freeRoam, playerCentered
}
int cameraScaling = 20;
PImage testBoots;
PImage clown;
PImage testSquare;
PImage stoneTile1;
PImage resizedTile;
PImage warrior1;
float enemyX = 10;
float enemyY = 10;
float redSquare;
int currentXPos = 10;
int currentYPos = 10;
int tileType;
int mapWidth = 500;
int mapHeight = 500;
int[][] squareType = new int[mapHeight][mapWidth];
boolean movingUp, movingDown, movingLeft, movingRight;
int squareX;
int squareY;
boolean canMoveUp, canMoveDown, canMoveLeft, canMoveRight = true;
int cameraX = currentXPos;
int cameraY = currentYPos;
int lastCameraX;
int lastCameraY;
boolean centerCamera = false;
int pastMouseXPosition;
int pastMouseYPosition;
int squareWidth;
int squareHeight;
boolean mouseDragging = true, freeRoam, playerCentered;
public static void main(String[] args) {
PApplet.main("Dungeon1");
}
public void settings() {
fullScreen();
}
public void setup() {
playerStartPos();
createMap();
currentState = gameState.Running;
testBoots = loadImage("Images/TestBoots.png");
clown = loadImage("Images/clown.png");
testSquare = loadImage("Images/testSquare.png");
stoneTile1 = loadImage("Images/stoneTile1.png");
resizedTile = loadImage("Images/resizedTile.png");
warrior1 = loadImage("Images/warrior1.png");
}
public void draw() {
background(0, 0, 0);
drawMap();
isMoving();
}
public void keyPressed() {
if (key == 'w') {
movingUp = true;
}
if (key == 's') {
movingDown = true;
}
if (key == 'a') {
movingLeft = true;
}
if (key == 'd') {
movingRight = true;
}
if (key == 'k') {
Test = true;
}
if (key == 'c') {
currentCameraState = cameraState.playerCentered;
centerCamera = true;
}
if (key == 'v') {
currentCameraState = cameraState.freeRoam;
}
if (key == 'b') {
currentCameraState = cameraState.playerCentered;
}
}
public void keyReleased() {
if (key == 'w') {
movingUp = false;
}
if (key == 's') {
movingDown = false;
}
if (key == 'a') {
movingLeft = false;
}
if (key == 'd') {
movingRight = false;
}
if (key == 'k') {
Test = false;
}
}
public void mouseDragged(MouseEvent e) {
if (pastMouseXPosition < mouseX) {
lastCameraX--;
}
if (pastMouseXPosition > mouseX) {
lastCameraX++;
}
if (pastMouseYPosition < mouseY) {
lastCameraY--;
}
if (pastMouseYPosition > mouseY) {
lastCameraY++;
}
pastMouseXPosition = mouseX;
pastMouseYPosition = mouseY;
}
public void mousePressed() {
currentCameraState = cameraState.freeRoam;
mouseDragging = true;
pastMouseXPosition = mouseX;
pastMouseYPosition = mouseY;
}
public void mouseReleased() {
mouseDragging = false;
}
public void mouseWheel(MouseEvent e) {
if (e.getAmount() < 0) {
cameraScaling--;
} else {
cameraScaling++;
}
}
public void isMoving() {
if (movingUp) {
if (canMoveUp) {
currentYPos--;
cameraY--;
}
if (squareType[currentXPos][currentYPos] == 2) {
currentYPos++;
canMoveUp = false;
cameraY++;
}
}
if (movingDown) {
if (canMoveDown) {
currentYPos++;
cameraY++;
}
if (squareType[currentXPos][currentYPos] == 2) {
currentYPos--;
canMoveDown = false;
cameraY--;
}
}
if (movingRight) {
if (canMoveRight) {
currentXPos++;
cameraX++;
}
if (squareType[currentXPos][currentYPos] == 2) {
currentXPos--;
canMoveRight = false;
cameraX--;
}
}
if (movingLeft) {
if (canMoveLeft) {
currentXPos--;
cameraX--;
}
if (squareType[currentXPos][currentYPos] == 2) {
currentXPos++;
canMoveLeft = false;
cameraX++;
}
}
if (squareType[currentXPos][currentYPos] != 2) {
drawPlayer();
canMoveUp = true;
canMoveLeft = true;
canMoveDown = true;
canMoveRight = true;
}
}
public void playerStartPos() {
currentXPos = 96/2;
currentYPos = 54/2;
}
public void drawPlayer() {
if (currentCameraState == cameraState.playerCentered) {
image(testSquare, (currentXPos * cameraScaling - cameraX * cameraScaling), (currentYPos * cameraScaling - cameraY * cameraScaling), cameraScaling, cameraScaling);
} else if (currentCameraState == cameraState.freeRoam) {
image(testSquare, (currentXPos * cameraScaling - lastCameraX * cameraScaling), (currentYPos * cameraScaling - lastCameraY * cameraScaling), cameraScaling, cameraScaling);
}
}
public void createMap() {
//First creates the plane to navigate
for (int r = 0; r < mapHeight; r++) {
for (int c = 0; c < mapWidth; c++) {
redSquare = generator.nextInt(100);
if (redSquare > 90) {
tileType = 2;
} else {
tileType = 0;
}
squareType[r][c] = tileType;
}
}
}
public void drawMap() {
//TODO: Either figure out how to make actual scaled images not run poorly or make a bunch of images that only are displayed a scaled resolution
if (cameraScaling < 15) {
cameraScaling = 15;
}
if (cameraScaling > 50) {
cameraScaling = 50;
}
if (centerCamera) {
cameraX = currentXPos - 47*20/ cameraScaling;
cameraY = currentYPos - 23*20/ cameraScaling;
}
squareWidth = 20+ cameraScaling;
squareHeight = 20+ cameraScaling;
centerCamera = false;
if (currentCameraState == cameraState.freeRoam) {
cameraX = lastCameraX;
cameraY = lastCameraY;
for (int r = lastCameraX; r < lastCameraX + 96*20/ cameraScaling + 2 ; r++) {
for (int c = lastCameraY; c < lastCameraY + 54*20/ cameraScaling + 2; c++) {
squareX = r * cameraScaling - lastCameraX * cameraScaling;
squareY = c * cameraScaling - lastCameraY * cameraScaling;
if (r > 0 && c > 0 && r < mapWidth && c < mapHeight) {
if (squareType[r][c] == 0) {
image(resizedTile, squareX, squareY, cameraScaling, cameraScaling);
} else {
colorTiles(r, c);
rect(squareX, squareY, cameraScaling, cameraScaling);
}
}
}
}
} else if (currentCameraState == cameraState.playerCentered) {
lastCameraX = cameraX;
lastCameraY = cameraY;
for (int r = cameraX; r < cameraX + 96*20/ cameraScaling + 2; r++) {
for (int c = cameraY; c < cameraY + 54*20/ cameraScaling + 2; c++) {
squareX = r * cameraScaling - cameraX * cameraScaling;
squareY = c * cameraScaling - cameraY * cameraScaling;
if (r > 0 && c > 0 && r < mapWidth && c < mapHeight) {
if (squareType[r][c] == 0) {
image(resizedTile, squareX, squareY, cameraScaling, cameraScaling);
} else {
colorTiles(r, c);
rect(squareX, squareY, cameraScaling, cameraScaling);
}
}
}
}
}
}
public void colorTiles(int r, int c) {
if (squareType[r][c] == 2) {
fill(255, 0, 0);
}
}
}```
I have no clue how to change this into p5.js. Just trying to work on conversions. Please help me get this code to work in p5.js.
I know that this is old code and p5.js have different rules. Maybe the array isn't working. I have no clue.
Myself myself;
ArrayList<Enemy> enemies;
`enter code here`ArrayList<Enemy> enemies;
ArrayList<Bullet> myBullets;
ArrayList<Bullet> eneBullets;
void setup(){
size(640, 640);
rectMode(CENTER);
myself = new Myself();
enemies = new ArrayList<Enemy>();
myBullets = new ArrayList<Bullet>();
eneBullets = new ArrayList<Bullet>();
}
void draw(){
background(0);
myself.display();
for(Enemy enemy: enemies){
enemy.display();
}
for(Bullet bullet: myBullets){
bullet.display();
}
for(Bullet bullet: eneBullets){
bullet.display();
}
myself.update();
ArrayList<Enemy> nextEnemies = new ArrayList<Enemy>();
for(Enemy enemy: enemies){
enemy.update();
if(!enemy.isDead){
nextEnemies.add(enemy);
}
}
enemies = nextEnemies;
ArrayList<Bullet> nextMyBullets = new ArrayList<Bullet>();
for(Bullet bullet: myBullets){
bullet.update();
if(!bullet.isDead){
nextMyBullets.add(bullet);
}
}
myBullets = nextMyBullets;
ArrayList<Bullet> nextEneBullets = new ArrayList<Bullet>();
for(Bullet bullet: eneBullets){
bullet.update();
if(!bullet.isDead){
nextEneBullets.add(bullet);
}
}
eneBullets = nextEneBullets;
if(random(1) < 0.02){
enemies.add(new Enemy());
}
}
class Myself{
PVector loc;
float size;
int coolingTime;
boolean isDead;
Myself(){
size = 25;
loc = new PVector(width / 2, height - size / 2 - 10);
coolingTime = 0;
isDead = false;
}
void display(){
if(isDead){
fill(255, 255, 0);
stroke(0, 255, 0);
} else {
noFill();
stroke(0, 255, 0);
}
rect(loc.x, loc.y, size, size);
}
void update(){
isDead = false;
float dmx = mouseX - loc.x;
dmx = constrain(dmx, -5, 5);
loc.x += dmx;
coolingTime++;
if(mousePressed && coolingTime >= 10){
myBullets.add(new Bullet());
coolingTime = 0;
}
for(Bullet b: eneBullets){
if((loc.x - size / 2 <= b.loc.x && b.loc.x <= loc.x + size / 2)
&& (loc.y - size / 2 <= b.loc.y && b.loc.y <= loc.y + size / 2)){
isDead = true;
b.isDead = true;
break;
}
}
for(Enemy e: enemies){
if(abs(loc.x - e.loc.x) < size / 2 + e.size / 2 && abs(loc.y - e.loc.y) < size / 2 + e.size / 2){
isDead = true;
e.isDead = true;
break;
}
}
}
}
class Bullet{
PVector loc;
float vel;
boolean isMine;
boolean isDead;
Bullet(){
loc = new PVector(myself.loc.x, myself.loc.y);
vel = -10;
isMine = true;
}
Bullet(Enemy enemy){
loc = new PVector(enemy.loc.x, enemy.loc.y);
vel = 5;
isMine = false;
}
void display(){
if(isMine){
stroke(0, 255, 0);
} else {
stroke(255, 0, 0);
}
line(loc.x, loc.y, loc.x, loc.y + vel);
}
void update(){
loc.y += vel;
if((vel > 0 && loc.y > height) || (vel < 0 && loc.y < 0)){
isDead = true;
}
}
}
class Enemy{
PVector loc;
float vel;
float size;
int coolingTime;
boolean isDead;
Enemy(){
size = 25;
loc = new PVector(random(size / 2, width - size / 2), -size / 2);
vel = 3;
coolingTime = int(random(60));
isDead = false;
}
void display(){
noFill();
stroke(255, 0, 0);
rect(loc.x, loc.y, size, size);
}
void update(){
loc.y += vel;
if(loc.y > height){
isDead = true;
}
coolingTime++;
if(coolingTime >= 60){
eneBullets.add(new Bullet(this));
coolingTime = 0;
}
for(Bullet b: myBullets){
if((loc.x - size / 2 <= b.loc.x && b.loc.x <= loc.x + size / 2)
&& (loc.y - size / 2 <= b.loc.y && b.loc.y <= loc.y + size / 2)){
isDead = true;
b.isDead = true;
break;
}
}
}
}
If you could just tell me what parts i need to change or fix it it would help me a lot thankyou.
You shouldn't try to translate code line-by-line. Instead, you need to take a step back and understand what the program does, and then you implement that in the target language.
They syntax for Processing (which is based on Java) is very different from the syntax for p5.js (which is based on JavaScript).
Instead of trying to "convert" the code, you need to think in terms of what the code actually does. And then start with a new program in p5.js and do that using p5.js. It's not always going to be a line-by-line conversion.
here's your code:
class myself{
constructor()
{
this.x = 0;
this.y = height-35;
this.size = 25;
this.health = 25;
this.cooldown = 0;
this.plrBullets = [];
}update(){this.cooldown++;
if (mouseIsPressed && this.cooldown>10)
{
this.cooldown = 0;
this.plrBullets.push(new Bullet(true,this.x,this.y));
}
let nextPlrBullets = [];
for (let i = 0; i<this.plrBullets.length; i++)
{
this.plrBullets[i].update();
if (this.plrBullets[i].alive)
{
nextPlrBullets.push(this.plrBullets[i]);
}
}
this.plrBullets = nextPlrBullets;
this.x += constrain(mouseX - this.x, -10, 10);
for (let i = 0; i<eneBullets.length; i++)
{
let c = eneBullets[i];
if (c.x > this.x && c.x < this.x + 25 && c.y > this.y && c.y < this.y + 25)
{
this.health--;
c.alive = false;
if (this.health<=0)
{
print("you lost")
}
}
}
noFill();
stroke(0,255,0);
rect(this.x,this.y,this.size,this.size);
}
}
class enemy{
constructor()
{
this.x = random(15,width-15);
this.y = random(-100,0);
this.size = 25;
this.cooldown = 0;
this.alive = true;
}
update()
{
this.y+=2.5;
this.cooldown++;
if (this.cooldown>20)
{
this.cooldown = 0;
eneBullets.push(new Bullet(false,this.x,this.y));
}
let nextEneBullets = [];
for (let i = 0; i<eneBullets.length; i++)
{
eneBullets[i].update();
if (eneBullets[i].alive)
{
nextEneBullets.push(eneBullets[i]);
}
}
eneBullets = nextEneBullets;
for (let i = 0; i<my_self.plrBullets.length; i++)
{
let c = my_self.plrBullets[i];
if (c.x > this.x && c.x < this.x + 25 && c.y > this.y && c.y < this.y + 25)
{
this.alive = false
c.alive = false;
}
}
if (this.y>height+35)
{
this.y = random(-100,0);
}
noFill();
stroke(255,0,0);
rect(this.x,this.y,this.size,this.size);
}
}
class Bullet{
constructor(dir,x,y)
{
this.x = x;
this.speed = 5;
this.dir = dir;
this.alive = true;
this.y = y;
}
update()
{
if (this.dir)
{
this.y -= this.speed;
}else{
this.y += this.speed;
}
if (this.y<-20)
{
this.alive = false;
}//this.y = -25;
if(this.dir)
{
stroke(0,255,0);
}else{
stroke(255,0,0);
}
line(this.x,this.y,this.x,this.y+25);
}
}
let my_self;
let eneBullets = [];
let enemies = [];
function setup()
{
createCanvas(640,640);
rectMode(CENTER);
my_self = new myself();
for (let i = 0; i<10; i++)
{
enemies.push(new enemy());
}
}
function draw()
{
background(0);
if(random(1) < 0.02 && enemies.length<15){
enemies.push(new enemy());
}
my_self.update();
let newEnimies = [];
for (let i = 0; i<enemies.length; i++)
{
enemies[i].update();
if (enemies[i].alive)
{
newEnimies.push(enemies[i]);
}
s}
enemies = newEnimies;
if (enemies.length == 0)
{
print("you win");
}
}
Sorry that the collisions are a bit wacky but when converting processing to p5 here are some tips:
1: don’t use array lists use arrays with push instead of add
2: no data types only LET
3: print not println
4: class constructors are declared as “constructor” instead of class name
5: use “function” before function declaration instead of void
6: pvector is vector
7: google is your friend
8: the p5 documentation
I have been working on a game for a while and I would like to have a different class for each type of Creature that there is. Right now, all of the different creatures' AI is run in a long switch and I would like a superclass to ovveride that function with that AI for that creature. I have this set up but it won't override.
Am I forgetting something?
Bunny.java:
package creature;
import org.newdawn.slick.opengl.Texture;
import creature.Creature;
import creature.CreatureType;
import data.Tile;
public class Bunny extends Creature{
public Bunny(CreatureType type, float x, float y, float speed1) {
super(type, x, y, speed1);
}
public void AI(int type) {
System.out.println("test");
}
}
Creature.java:
public Creature(CreatureType type, float x, float y, float speed1) {
this.texture = drawImg(type.textureName);
this.textureHamster = drawImg("creatures/HamsterFace");
this.healthBackground = drawImg("health_background");
this.healthForeground = drawImg("health_foreground");
this.healthBorder = drawImg("health_border");
this.startTile = startTile;
this.x = x;
this.y = y;
this.intX = (int) x;
this.intY = (int) y;
this.width = texture.getImageWidth();
this.height = texture.getImageHeight();
this.speed1 = speed1;
this.speed = speed;
this.intspeed = speed;
this.grid = grid;
this.health = type.health;
this.inithealth = type.health;
this.hiddenHealth = health;
this.startHealth = health;
this.dir = false;
this.dchosen = false;
this.setx = 0;
this.hurt = 0;
this.panick = 0;
this.deathWish = 0;
this.pdir = -1;
this.myX = x;
this.myY = HEIGHT / 2;
this.right = false;
this.left = false;
this.fade = 0;
this.fir = true;
this.aiType = type.aiType;
this.yOffset = 0;
}
.....
public void AI(int type) {
if(panic > 0)
panic--;
hurt();
speed = speed1;
switch(type) {
case 1:
if(panic > 0) {
if(pickRandom(150, 300) < 10) {
direction = !direction;
}
if(direction) {
if(!right) {
x += speed;
} else {
if(falling < 2)
gravity = 8;
}
} else {
if(!left) {
x -= speed;
} else {
if(falling < 2)
gravity = 8;
}
}
} else {
if(getRange(WIDTH / 2, myX) > 200) {
directionCoolDown++;
if(directionCoolDown > pickRandom(150, 3000)) {
direction = !direction;
directionCoolDown = 0;
}
if(direction) {
if(!right) {
x += speed / 3.2;
} else {
if(falling < 2)
gravity = 8;
}
} else {
if(!left) {
x -= speed / 3.2;
} else {
if(falling < 2)
gravity = 8;
}
}
} else {
if(myX < WIDTH / 2) {
direction = true;
} else {
direction = false;
}
}
}
break;
case 2:
yOffset = -25;
if(!angry) {
pdir = 0;
if(getRange(Player.getX(), myX) < 300) {
hamsterFace = true;
} else {
hamsterFace = false;
}
if(!hamsterFace) {
directionCoolDown++;
if(directionCoolDown > pickRandom(150, 3000)) {
direction = !direction;
directionCoolDown = 0;
}
if(direction) {
if(!right) {
x += speed / 3.2;
} else {
if(falling < 2)
gravity = 8;
}
} else {
if(!left) {
x -= speed / 3.2;
} else {
if(falling < 2)
gravity = 8;
}
}
}
} else {
pdir++;
hamsterFace = false;
if(myX < Player.getX()) {
direction = true;
} else {
direction = false;
}
if(direction) {
if(!right) {
x += speed / 1;
} else {
if(falling < 2)
gravity = 8;
}
} else {
if(!left) {
x -= speed / 1;
} else {
if(falling < 2)
gravity = 8;
}
}
if(getRange(myX, Player.getX()) < 5 && getRange(myY, Player.getY()) < 5) {
hurtPlayer(-2);
direction = !direction;
if(direction) {
if(!right) {
x += speed * 10;
} else {
if(falling < 2)
gravity = 8;
}
} else {
if(!left) {
x -= speed * 10;
} else {
if(falling < 2)
gravity = 8;
}
}
}
}
if(panic > 1) {
angry = true;
} else {
if(pdir > pickRandom(1000,2000)) {
angry = false;
}
}
break;
}
}
.....
(Both classes are in the same package)
EDIT: I fixed the typo....
you have in the Bunny class:
public void AI() {
System.out.println("test");
}
in the Creature class:
public void AI(int type) {
if(panic > 0)
....
so
void AI(int type) and void AI() are NOT the same method (check the signature and how they take different parameters!)
therefore the Bunny class is not overriding anything from the parent class
--
edit:
now that your classes have a method void AI(int type) then we can say that
Bunny override the Creature AI method and everytime you call bunny.AI(f) your bunny method will be called!
I have asked a similar question but i think i needed to be more clear on this.
Using Andriod and Open GL ES 2.0
This is my FrameTime Calculation
private long mCurrentTime = SystemClock.uptimeMillis()/1000; // set on entry to the render class
....
long mNewTime = SystemClock.uptimeMillis();
float FrameTime = (mNewTime - mCurrentTime)/ 1000.0f;
mCurrentTime = mNewTime;
To move my model i now say something like
Model.MoveX(PositionX += Speed * FrameTime);
This moves the model fine.
If i move between 3 points lets say -10, 0, 10 after couple of moves the model appears to be in the wrong position. if i increase the speed the error becomes more noticeable.
What cause this error and how can i fix it.
below is the function for moving the cube
private float MinCubeMovement = -10.0f;
private float MaxCubeMovement = 10.0f;
private float Middle = 0.0f;
private static boolean MoveLeftFlag = false;
private static boolean MoveRightFlag = false;
private int PositionFlag = 0;
....
private void UpdatePlayer(float FrameTime)
{
if(MoveLeftFlag == true)
{
if(PositionFlag == 0)
{
if(Cube1.PositionX >= MinCubeMovement)
{
Cube1.MoveLocalX(-CubeMoveSpeed * FrameTime );
}
else if(Cube1.PositionX <= MinCubeMovement)
{
PositionFlag = -1;
MoveLeftFlag = false;
}
}
if(PositionFlag == 1)
{
if(Cube1.PositionX >= Middle)
{
Cube1.MoveLocalX(-CubeMoveSpeed * FrameTime );
}
else if(Cube1.PositionX <= Middle)
{
MoveLeftFlag = false;
PositionFlag = 0;
}
}
}
if(MoveRightFlag == true)
{
if(PositionFlag == 0)
{
if(Cube1.PositionX <= MaxCubeMovement)
{
Cube1.MoveLocalX(CubeMoveSpeed * FrameTime );
}
else if(Cube1.PositionX >= MaxCubeMovement)
{
MoveRightFlag = false;
PositionFlag = 1;
}
}
if(PositionFlag == -1)
{
if(Cube1.PositionX <= Middle)
{
Cube1.MoveLocalX(CubeMoveSpeed * FrameTime );
}
else if(Cube1.PositionX >= Middle)
{
MoveRightFlag = false;
PositionFlag = 0;
}
}
}
}
MoveLocalX adds a float to the positionX in my model class, this works fine
I am writing an Android game right now and I would need some help in the collision of the wall on screen. When I drag the ball in the top and right it able to collide in wall but when I drag it faster it was able to overlap in the wall
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
// if the player moves
case MotionEvent.ACTION_MOVE: {
if (playerTouchRect.contains(x, y)) {
boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;
boolean canMove = false;
boolean foundFinish = false;
if (x != pLastXPos) {
if (x < pLastXPos) {
left = true;
} else {
right = true;
}
pLastXPos = x;
}
if (y != pLastYPos) {
if (y < pLastYPos) {
up = true;
} else {
down = true;
}
pLastYPos = y;
}
plCellRect = getRectFromPos(x, y);
newplRect.set(playerRect);
newplRect.left = x - (int) (playerRect.width() / 2);
newplRect.right = x + (int) (playerRect.width() / 2);
newplRect.top = y - (int) (playerRect.height() / 2);
newplRect.bottom = y + (int) (playerRect.height() / 2);
int currentRow = 0;
int currentCol = 0;
currentRow = getRowFromYPos(newplRect.top);
currentCol = getColFromXPos(newplRect.right);
if(!canMove){
canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] == Cell.wall;
canMove =true;
}
finishTest = mapManager.getCurrentTile().pMaze[currentRow][currentCol];
foundA = finishTest == Cell.valueOf(letterNotGet + "");
canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] != Cell.wall;
canMove = (finishTest == Cell.floor || finishTest == Cell.pl) && canMove;
if (canMove) {
invalidate();
setTitle();
}
if (foundA) {
mapManager.getCurrentTile().pMaze[currentRow][currentCol] = Cell.floor;
// finishTest
letterGotten.add(letterNotGet);
playCurrentLetter();
/*sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);*/
foundS = letterNotGet == 's';
letterNotGet++;
}if(foundS){
AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
builder.setTitle(mainActivity.getText(R.string.finished_title));
LayoutInflater inflater = mainActivity.getLayoutInflater();
View view = inflater.inflate(R.layout.finish, null);
builder.setView(view);
View closeButton =view.findViewById(R.id.closeGame);
closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View clicked) {
if(clicked.getId() == R.id.closeGame) {
mainActivity.finish();
}
}
});
AlertDialog finishDialog = builder.create();
finishDialog.show();
}
else {
Log.d(TAG, "INFO: updated player position");
playerRect.set(newplRect);
setTouchZone();
updatePlayerCell();
}
} // end of (CASE) if playerTouch
break;
} // end of (SWITCH) Case motion
}//end of Switch
return true;
}//end of TouchEvent
private void finish() {
// TODO Auto-generated method stub
}
public int getColFromXPos(int xPos) {
val = xPos / (pvWidth / mapManager.getCurrentTile().pCols);
if (val == mapManager.getCurrentTile().pCols) {
val = mapManager.getCurrentTile().pCols - 1;
}
return val;
}
/**
* Given a y pixel position, return the row of the cell it is in This is
* used when determining the type of adjacent Cells.
*
* #param yPos
* y position in pixels
* #return The cell this position is in
*/
public int getRowFromYPos(int yPos) {
val = yPos / (pvHeight / mapManager.getCurrentTile().pRows);
if (val == mapManager.getCurrentTile().pRows) {
val = mapManager.getCurrentTile().pRows - 1;
}
return val;
}
/**
* When preserving the position we need to know which cell the player is in,
* so calculate it from the centre on its Rect
*/
public void updatePlayerCell() {
plCell.x = (playerRect.left + (playerRect.width() / 2))
/ (pvWidth / mapManager.getCurrentTile().pCols);
plCell.y = (playerRect.top + (playerRect.height() / 2))
/ (pvHeight / mapManager.getCurrentTile().pRows);
if (mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] == Cell.floor) {
for (int row = 0; row < mapManager.getCurrentTile().pRows; row++) {
for (int col = 0; col < mapManager.getCurrentTile().pCols; col++) {
if (mapManager.getCurrentTile().pMaze[row][col] == Cell.pl) {
mapManager.getCurrentTile().pMaze[row][col] = Cell.floor;
break;
}
}
}
mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] = Cell.pl;
}
}
public Rect getRectFromPos(int x, int y) {
calcCell.left = ((x / cellWidth) + 0) * cellWidth;
calcCell.right = calcCell.left + cellWidth;
calcCell.top = ((y / cellHeight) + 0) * cellHeight;
calcCell.bottom = calcCell.top + cellHeight;
Log.d(TAG, "Rect: " + calcCell + " Player: " + playerRect);
return calcCell;
}
public void setPlayerRect(Rect newplRect) {
playerRect.set(newplRect);
}
private void setTouchZone() {
playerTouchRect.set(
playerRect.left - playerRect.width() / TOUCH_ZONE,
playerRect.top - playerRect.height() / TOUCH_ZONE,
playerRect.right + playerRect.width() / TOUCH_ZONE,
playerRect.bottom + playerRect.height() / TOUCH_ZONE);
}
public Rect getPlayerRect() {
return playerRect;
}
public Point getPlayerCell() {
return plCell;
}
public void setPlayerCell(Point cell) {
plCell = cell;
}
}*
This is an architectural problem. You can read more about it here.
In essence, your physics simulation (as simple as might be) is coupled to your framerate (which is capped at 60fps). Any events occurring faster than 60Hz cannot be processed accurately hence your bug.
The solution is to run the collision detection on an independent thread and draw the state it calculates at 60fps.
Also, take a look at these gamedev questions that refer to the same article.