I am using a value for the resolution of the x axis, but when I increase it, something goes wrong…
I’ll add the code so you can see it. I also made changes to the slope calculation. just averaging the slopes of the current position with lots of other points, but its working only on 4 points to each direction for some reason.
I already added a player and an enemy class (I won’t include the enemy class because it is npt related to the problem and I am not actually using it now).
The blue rectangle is the bounding box of the player, red line is the position, and green line is the angle.
nothing is really working now, I am just trying to get the angle works.
int window_width = 900, window_height = 700;
int terrainResolutionX = 2;
int slope_offset = 4;
float xoff = 0.0;
float[] terrain = new float[window_width / terrainResolutionX + 1];
float playerRotation = 0;
float playerFloorLineB = 0;
int playerX = 0, playerY = 0;
int playerWidth = 40, playerHeight = 50;
int playerAcceleration = 2;
float playerVel = 0;
int playerMaxVel = 20;
boolean key_a = false, key_d = false;
int enemyCount = 0;
ArrayList<Enemy> enemies = new ArrayList<Enemy>();
char[] enemyTypes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
int score = 0;
void setup(){
size(900, 700);
playerX = window_width / 2;
generateTerrain();
for(int i = 0; i < 0; i++){
newEnemy();
print(enemies.get(i).enemyType);
}
}
void draw() {
background(255);
updateKeysStates();
playerX += playerVel;
if(playerX > window_width - 1){
playerX = window_width - 1;
}else if(playerX < 0){
playerX = 0;
}
if(playerVel < 0.25 && playerVel > -0.25){
playerVel = 0;
}
float[] tempSlopes = new float[slope_offset * 2];
float[] yValues = new float[slope_offset * 2];
float playerSlope = 0;
for(int i = 0; i < slope_offset * 2; i++){
int operator = 0;
if(i < slope_offset){
operator = -1;
}
if(playerX + (i - 3 + operator) < slope_offset || playerX + (i - 3 + operator) > window_width - slope_offset){
yValues[i] = terrain[playerX / terrainResolutionX];
}else{
yValues[i] = terrain[(playerX / terrainResolutionX) + (i - 3 + operator)];
}
}
for(int i = 0; i < slope_offset * 2; i++){
int operator = 0;
if(i < slope_offset){
operator = -1;
}
tempSlopes[i] = (terrain[playerX / terrainResolutionX] - yValues[i]) / (i - 3 + operator);
playerSlope += tempSlopes[i];
}
playerSlope /= (slope_offset * 2);
playerVel -= playerSlope * 0.3;
if(playerVel > playerMaxVel){
playerVel = playerMaxVel;
}
if(playerVel < -playerMaxVel){
playerVel = -playerMaxVel;
}
playerVel -= 0.2 * playerVel;
stroke(0);
for(int i = 0; i < (window_width / terrainResolutionX) - 1; i++){
line(i * terrainResolutionX, terrain[i], (i * terrainResolutionX) + 1, terrain[i + 1]);
}
stroke(255, 0, 0);
line(playerX, terrain[(int)(playerX / terrainResolutionX)], playerX, terrain[(int)(playerX / terrainResolutionX)] - playerHeight);
stroke(0, 255, 0);
float angle1;
angle1 = degrees(atan(playerSlope));
float angle2 = 90 - angle1 + 90;
float ratioA = playerHeight / sin(HALF_PI);
float side1H = ratioA * sin(radians(angle2));
float side2W = sqrt(pow(playerHeight / 2, 2) - pow(side1H, 2));
line(playerX, terrain[playerX / terrainResolutionX], playerX - side1H, terrain[playerX / terrainResolutionX] - side2W);
for(Enemy e : enemies){
e.update();
e.show();
}
float temp = 0;
for(int i = enemies.size() - 1; i >= 0; i--){
if(enemies.get(i).xPos >= window_width - 1 || enemies.get(i).xPos <= 1){
temp++;
enemies.remove(i);
}else if(enemies.get(i).yPos + enemies.get(i).size / 2 >= terrain[floor(enemies.get(i).xPos / terrainResolutionX)]){
temp++;
enemies.remove(i);
}
}
for(int i = 0; i < temp; i++){
newEnemy();
}
fill(0);
textSize(40);
textAlign(LEFT, TOP);
text(score, 10, 10);
pushMatrix();
noFill();
stroke(0, 0, 255);
translate(playerX, terrain[playerX / terrainResolutionX] - 0.5 * playerHeight);
//rotate(radians(angle1));
rect(-playerWidth / 2, -playerHeight / 2, playerWidth, playerHeight);
popMatrix();
}
void generateTerrain(){
for(int i = 0; i < window_width / terrainResolutionX; i++){
xoff = xoff + .01;
float n = noise(xoff) * 150;
terrain[i] = window_height - n;
}
}
void updateKeysStates(){
if(key_a){
playerVel -= playerAcceleration;
}
if(key_d){
playerVel += playerAcceleration;
}
}
void newEnemy(){
enemies.add(new Enemy(random(60, 840), enemyTypes[(int)random(0, enemyTypes.length)], random(6, 9), random(-15, 15), random(25, 35)));
}
void keyPressed(){
if(key == 'a'){
key_a = true;
}
if(key == 'd'){
key_d = true;
}
}
void keyReleased(){
if(key == 'a'){
key_a = false;
}
if(key == 'd'){
key_d = false;
}
}
I you have any questions about the code, just ask me
I need this really fast so I hope you have an answer.