Why is the last bracket a invalid character error - java

Originally my error with was with the last bracket, the last bracket in the code shown below says invalid character delete this token and it would not save and I got an error:
"Save could not be completed. Try File > Save As... if the problem persists. Reason: Some characters cannot be mapped using the "Cp1252" character encoding. Either change the encoding or remove the characters which are not supported by the "Cp1252" character encoding."
This code worked fine until I called this method to another class, I figured that was the problem so I commented the call out but the error remained.
I already converted this to a UTF-8 and the error with the bracket as an invalid character is still there.
I am using Eclispe.
Any help is much appreciated, Thanks!
public void render(int xPos, int yPos, int tile, int color, boolean mirrorX, boolean mirrorY)
{
xPos -= xOff;
yPos -= yOff;
int xTile = tile % 32;
int yTile = tile / 32;
int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
for (int y = 0; y < 8; y++)
{
if (y + yPos < 0 || y + yPos >= height)
{
continue;
}
int ySheet = y;
if (mirrorY)
{
ySheet = 7-y;
}
for (int x = 0; x < 8; x++)
{
if (x + xPos < 0 || x + xPos >= width)
{
continue;
}
int xSheet = x;
if (mirrorX)
{
xSheet = 7-x;
}
int col = (color >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) & 255;
if (col < 255)
{
pixels[(x + xPos) + (y + yPos) * width] = col;
}
}
}
}

I converted the file back to Cp1252 and four characters showed up after the bracket, I don't know where they came from but at least my error is fixed.

Related

Global variable "x" does not exist, Processing 3.2.3. Processing and Kinect

I was beginning with coding in Processing when I encountered an error which I can't find a solution for.
**DISCLAIMER: I'm new to coding, so I'm having trouble understanding how it works lol
I was attempting to use processing to write a code for a kinect program that would create a ripple effect, but I can't figure out how to define two variables.
Code:
// A simple ripple effect. Click on the image to produce a ripple
// Author: radio79
// Code adapted from http://www.neilwallis.com/java/water.html
// Code adapted from https://forum.processing.org/two/discussion/25348/can-i-apply-ripple-effect-to-kinect
import org.openkinect.processing.*;
Kinect kinect;
PImage img;
Ripple ripple;
void setup() {
size(1920, 1080);
kinect = new Kinect(this);
kinect.initVideo();
img = new PImage(kinect.colorWidth, kinect.colorHeight);
ripple = new Ripple();
//frameRate(60);
}
void draw() {
image(kinect.getVideoImage(), 0, 0);
img.loadPixels();
for (int loc = 0; loc < Kinect.colorWidth * Kinect.colorHeight; loc++) {
img.pixels[loc] = ripple.col[loc];
}
img.updatePixels();
ripple.newframe();
}
class Ripple {
int i, a, b;
int oldind, newind, mapind;
short ripplemap[]; // the height map
int col[]; // the actual pixels
int riprad;
int rwidth, rheight;
int ttexture[];
int ssize;
Ripple() {
// constructor
riprad = 3;
rwidth = width >> 1;
rheight = height >> 1;
ssize = width * (height + 2) * 2;
ripplemap = new short[ssize];
col = new int[width * height];
ttexture = new int[width * height];
oldind = width;
newind = width * (height + 3);
}
void newframe() {
// update the height map and the image
i = oldind;
oldind = newind;
newind = i;
i = 0;
mapind = oldind;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
short data = (short)((ripplemap[mapind - width] + ripplemap[mapind + width] +
ripplemap[mapind - 1] + ripplemap[mapind + 1]) >> 1);
data -= ripplemap[newind + i];
data -= data >> 5;
if (x == 0 || y == 0) // avoid the wraparound effect
ripplemap[newind + i] = 0;
else
ripplemap[newind + i] = data;
// where data = 0 then still, where data > 0 then wave
data = (short)(1024 - data);
// offsets
a = ((x - rwidth) * data / 1024) + rwidth;
b = ((y - rheight) * data / 1024) + rheight;
//bounds check
if (a >= width)
a = width - 1;
if (a < 0)
a = 0;
if (b >= height)
b = height-1;
if (b < 0)
b=0;
col[i] = img.pixels[a + (b * width)];
mapind++;
i++;
}
}
}
}
void mouseDragged() {
for (int j = mouseY - ripple.riprad; j < mouseY + ripple.riprad; j++) {
for (int k = mouseX - ripple.riprad; k < mouseX + ripple.riprad; k++) {
if (j >= 0 && j < height && k>= 0 && k < width) {
ripple.ripplemap[ripple.oldind + (j * width) + k] += 512;
}
}
}
}
The error is: 'The global variable "x" does not exist', 'The global variable "y" does not exist' and so forth. Please help.
The variables I need help defining appear on line 18 for the first time, they are colorWidth and colorHeight
The line reads:
img = new PImage(kinect.colorWidth, kinect.colorHeight);
The colorWidth and colorHeight are underlined in red.
I have tried using this method:
public
float colorWidth;
float colorHeight;
But, only the second line appears to be defined properly. The first line emits the message "colorWidth cannot be resolved or is not a field" when the program runs, or "illegal modifier for parameter colorWidth; only final is permitted" when the underline is clicked.
Picture of what the program shows after the public code
PLEASE HELP!!! Thank you!
The keyword public here just means nothing, it must be said of a function or variable. And you just don't need it. So removing it should fix the problem!

Color not rendering properly

This stems from a problem that many people get from following vanZeben's game engine tutorial you can find here. The problem is a the rendering function vanZeben uses. It is very awkward and limiting. I need to remove it and replace it with code that renders the color that is on the sprite sheet.
Edit: Here is a link to the entire project's source.
The closest I've come is removing color >> from the int col initialization. It still removes all color, except for a tile that I added after I finished the tutorial.
//I know the problem comes from this section of code.
int col = (color >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8) & 255);
if (col < 255)
{
for(int yScale = 0; yScale < scale; yScale++)
{
if (yPixel + yScale < 0 || yPixel + yScale >= height) continue;
for(int xScale = 0; xScale < scale; xScale++)
{
if (xPixel + xScale < 0 || xPixel + xScale >= width) continue;
pixels[(xPixel + xScale) + (yPixel + yScale) * width] = col;
}
}
}
I just need col to equal the color of the pixel on the sprite sheet.

Get x and y from index in pixel array

So I have an array of pixels to represent an image. I'm currently trying to get an x and y value from an element of the pixel array, but I can only get the x successfully.
My current code:
public int[] draw(int[] pixels, int index, int xOffs, int yOffs) {
int x0 = index % width;
int y0 = (index * x0) / height;
for (int y = y0 * size; y < y0 * size + size; y++) {
int yPix = y + yOffs - (size * y0);
if (yPix < 0 || yPix >= Game.height) continue;
for (int x = x0 * size; x < x0 * size + size; x++) {
int xPix = x + xOffs - (size * x0);
if (xPix < 0 || xPix >= Game.width) continue;
int src = this.pixels[x + y * width];
pixels[xPix + yPix * Game.width] = src;
}
}
return pixels;
}
x0 returns the correct value, e.g if the index was 4, then it would return 0.
y0 always returns 0.
Basically what I'm trying to achieve is a sprite sheet using pixel manipulation.
Thanks.
I am no expert but I think int y0 = (index * x0) / height; should be int y0 = index/width;.
PS. why are you adding (y0 * size) to y and the subtracting it again in yPix, it seems unnecessary and also it makes your code a little bit unreadable.

java unreachable code error (Render class game development)

i have been working on a java game engine but my render keeps getting the unreachable code error.The error appears at the setPixels method.
public class Renderer {
private int width, height;
private byte[] pixels;
public Renderer(GameContainer gc){
width = gc.getWidth();
height = gc.getHeight();
pixels = ((DataBufferByte)gc.getWindow().getImage().getRaster().getDataBuffer()).getData();
}
public void setPixel(int x, int y, float a, float r, float g, float b){
if((x < 0 || x>= width || y < 0 || y>= height) || a == 0){
return;
int index = (x + y * width) * 4;
pixels[index] = (byte)((a * 255f) + 0.5f);
pixels[index + 1] = (byte)((b * 255f) + 0.5f);
pixels[index + 2] = (byte)((g * 255f) + 0.5f);
pixels[index + 3] = (byte)((r * 255f) + 0.5f);
}
}
public void clear(){
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
setPixel(x,y,1,0,1,1);
}
}
}
}
I think this is what you are trying to do?
Your if statement should not be enclosing all the statements in your function.
public void setPixel(int x, int y, float a, float r, float g, float b){
// Check for invalid values
if((x < 0 || x>= width || y < 0 || y>= height) || a == 0){
// Break out of function if invalid values detected
return;
}
// Update pixel
int index = (x + y * width) * 4;
pixels[index] = (byte)((a * 255f) + 0.5f);
pixels[index + 1] = (byte)((b * 255f) + 0.5f);
pixels[index + 2] = (byte)((g * 255f) + 0.5f);
pixels[index + 3] = (byte)((r * 255f) + 0.5f);
}
The return statement ends the execution of a method. In the event that the if statement is run in the code below, the method will hit the return and end before doing all the other stuff. You don't seem to need a return statement in setPixel since there isn't a need to end the method prematurely.
public void setPixel(int x, int y, float a, float r, float g, float b) {
if((x < 0 || x>= width || y < 0 || y>= height) || a == 0){
//return;
int index = (x + y * width) * 4;
pixels[index] = (byte)((a * 255f) + 0.5f);
pixels[index + 1] = (byte)((b * 255f) + 0.5f);
pixels[index + 2] = (byte)((g * 255f) + 0.5f);
pixels[index + 3] = (byte)((r * 255f) + 0.5f);
}
}

Minicraft Collision Detection

I'm trying to understand how this code works for collision detection. I can tell the goal is a bounding box, and that we are testing each possible point for the entity, but I am uncertain of the purpose of the signed shift operator in this instance. In fact I don't even understand why it would ever be useful, just what it does. Can anyone elaborate?
protected boolean move2(int xa, int ya) {
if (xa != 0 && ya != 0) throw new IllegalArgumentException("Move2 can only move along one axis at a time!");
int xto0 = ((x) - xr) >> 4;
int yto0 = ((y) - yr) >> 4;
int xto1 = ((x) + xr) >> 4;
int yto1 = ((y) + yr) >> 4;
int xt0 = ((x + xa) - xr) >> 4;
int yt0 = ((y + ya) - yr) >> 4;
int xt1 = ((x + xa) + xr) >> 4;
int yt1 = ((y + ya) + yr) >> 4;
boolean blocked = false;
for (int yt = yt0; yt <= yt1; yt++)
for (int xt = xt0; xt <= xt1; xt++) {
if (xt >= xto0 && xt <= xto1 && yt >= yto0 && yt <= yto1) continue;
level.getTile(xt, yt).bumpedInto(level, xt, yt, this);
if (!level.getTile(xt, yt).mayPass(level, xt, yt, this)) {
blocked = true;
return false;
}
}
if (blocked) return false;
List<Entity> wasInside = level.getEntities(x - xr, y - yr, x + xr, y + yr);
List<Entity> isInside = level.getEntities(x + xa - xr, y + ya - yr, x + xa + xr, y + ya + yr);
for (int i = 0; i < isInside.size(); i++) {
Entity e = isInside.get(i);
if (e == this) continue;
e.touchedBy(this);
}
isInside.removeAll(wasInside);
for (int i = 0; i < isInside.size(); i++) {
Entity e = isInside.get(i);
if (e == this) continue;
if (e.blocks(this)) {
return false;
}
}
x += xa;
y += ya;
return true;
}
It may be worth noting that the Entity knows its exact x and y position by pixel, but a tile doesn't know its position at all. The world has an array of tiles, but only knows its tile position... so when it comes time to do collision detection the function must have to determine which tile position to get from the player position.
The full source is available here: http://www.ludumdare.com/compo/ludum-dare-22/?action=preview&uid=398
Note that tiles are 16x16
Dividing by a power of two is often expressed as a right-shift n bits where n is the power.
Used to be when writing C or assembly you did that because it was MUCH faster than doing an actual divide. Left shift is the same as multiplying by the equivalent power of two and is also MUCH faster than hardware multiply. Nowadays most compilers will special-case this and emit shifts instead of multiply/divide for powers of 2.
Of course if your tile size is not a power of 2 you have to divide instead.

Categories

Resources