I'm making a game engine, and I want to have a rope object in the game. I've created the rope, and it behaves perfectly, except that if I apply a force to the bottom of the rope (like attaching the player to the rope and moving the player), the force doesn't propagate to the rest of the rope.
This is what the rope looks like, even when I try to move the player:
I would like the rope to move along with the player, but keep the player attached to the bottom of the rope.
This is the update method from the Rope class
public void update() {
for (int i = 0; i < segments.size(); i++) {
RopeSegment previous = null;
if (i != 0) {
previous = segments.get(i - 1);
}
final RopeSegment seg = segments.get(i);
// do collision detection
seg.update();
// if we are not the first (position locked) segment
if (previous != null) {
// if we are out of range of the previous segment
if (seg.getCenter().toPoint().distanceSq(previous.getCenter().toPoint()) > MAX_DIST * MAX_DIST) {
// pull us back in
final Vec2D previousCenter = previous.getPosition();
seg.applyForce(previousCenter.subtract(seg.getPosition()).multiply(0.01));
seg.setPosition(previousCenter.add(seg.getPosition().subtract(previousCenter).unit()
.multiply(MAX_DIST)));
}
}
}
// lock position of first segment
segments.get(0).setPosition(getLockposition());
segments.get(0).setVelocity(new Vec2D(0, 0));
}
and this is the relevant code from the update method of the Player class
if (rope != null) {
if (rope.getLockposition().toPoint().distanceSq(getCenter().toPoint()) > Rope.MAX_DIST * Rope.MAX_DIST) {
setCenter(rope.getLastSegment().getCenter());
}
}
If I were you: I would find the number of rope segments (Depending on height), there are between the player and the top of the screen (Y : 0px); thereby, allowing you to constantly update an integer; or appropriate size variable, with how many segments to draw. Then, every time you redraw, you can make the rope above the player. This would entitle deleting most of what you have, but I believe it to be a bit more efficient. If you want to make the rope look more 'Rope Like', then you could use an ArrayList, updated every time you go to draw, which will have a class defining position and a Vec2D which holds velocity, which will be used to increase the x position of the rope. e.g.
List<Rope> ropes = new ArrayList<>(); //Fill This With Ropes. . . //First is The Farthest Up!
class Rope {
int x, y;
short velocity;
//Constructor
}
void updateRopes(int playerX) {
for(Rope r : ropes)
r.x += (r.velocity < (playerX - r.x)) ? r.velocity : (playerX - r.x); //You Can Make a Variable Which Holds (playerX - r.x) For Efficiency, or Keep For RAM
}
void playerMove() {
int yDec = ropes.size() / 5; //5 Is the Presumable Speed of The Character. . .
int tempVeloc = 5;
for(int i = 0; i < ropes.size(); i++)
if((i % yDec) == 0 & i != 0) tempVeloc -= 1;
//The Other Player-Move Based Code. . .
}
EDIT:
I believe your problem is that you are not actually editing the classes inside of the array, you are making a constant copy of them; thereby not changing the object because you have not made a pointer to them. . .
What you should do is this:
RopeSegment seg = segments.get(i - 1);
Related
I have been working on android apps for a long time but now I have decided to create a game aside with my pre-calculus final. I have completed the whole code and it works perfectly except one tiny issue. First of the game is based on flying pig(my classmate's face) avoiding top and bottom osticle. I developed an algorithm so that the obsticles are evenly spaced and based on random selection placed either as the top or bottom of the screen but never both at the same time!. The algorithm that needs improvement is in the 3rd code segment!
This is the screenshot of the problem: screenshot here
(My account is new so stackoverflow wont let me to share photos directly)
This is the class that calls updates for all dynamic objects (ship = pig, bacon = bonus item, BM = BarrierManager class's update() which updates the obsticles)
//this will update the resources
void Update(float dt) {
ship.update(dt);
//bumbing
if (!ship.death) {
background.update(dt);
**BM.update(dt);**
for (int i = 0; i % 5 == 0; i++) {
bacon.update(dt, BM.position);
}
}
ArrayList<Point> bacon_point = new ArrayList<Point>(bacon.getArray());
if (ship.bump(bacon_point.get(0), bacon_point.get(1), bacon_point.get(2), bacon_point.get(3))) {
bacon.setX(-200);
bacon.setY(-200);
Message msg = BM.game_panel.game.handler.obtainMessage();
msg.what = 0;
BM.game_panel.game.handler.sendMessage(msg);
}
for (int i = 0; i < BM.TopWalls.size(); i++) {
ArrayList<Point> temp = new ArrayList<Point>(BM.TopWalls.get(i).getArray());
//after we have accest the TopWalls arraylist we can call bump that check TopWalls object's points position with the pig's points
ArrayList<Point> temp2 = new ArrayList<Point>(BM.BottomWalls.get(i).getArray());
//after we have accest the TopWalls arraylist we can call bump that check BottomWalls object's points position with the pig's points
if ((ship.bump(temp.get(0), temp.get(1), temp.get(2), temp.get(3))) || (ship.bump(temp2.get(0), temp2.get(1), temp2.get(2), temp2.get(3))) || ship.death) {
ship.death = true;
game.onStop();
while(f==0) {
MediaPlayer mp = MediaPlayer.create(game, R.raw.explosion_fart);
mp.start();
f++;
}
f++;
Message msg = BM.game_panel.game.handler.obtainMessage();
msg.what = 1;
BM.game_panel.game.handler.sendMessage(msg);
i = BM.TopWalls.size()-1;
if(f == 8){
thread.setRunning(false);
}
}
}
}
In the BarrierManager I have created this update method which takes float dt = MainTheards general time for the game.
TopWalls is ArrayList this ArrayList is composed of top obsticles. Bottom walls is the same but BottomWalls.
//zreb decides if it should create TopWalls or BottomWalls object. This parameter is later transfered to the Barier.update method where I work with it
public void update(float dt){
for (int i=0;i<Num+1; i++) {
int zreb = new Random().nextInt(2);
//{if} to make the spacing bigger
if (i % 5 == 0){
**TopWalls.get(i).update(dt, true, zreb);
BottomWalls.get(i).update(dt, false, zreb);**
if(zreb == 0){
position.set(TopWalls.get(i).getX(), TopWalls.get(i).getY());
}
else{
position.set(BottomWalls.get(i).getX(),BottomWalls.get(i).getY());
}
}
}
}
Now this algoritm in the Barrier.class is where the magic went wrong. This update method takes the float dt mentioned earlier, a boolean variable for determining if the obsticle we work with at that instance is the Top or Bottom, and the zreb random int that determines if the top or bottom obsticle is going to be shown.
//problem! needs to be discussed
public void update(float dt, boolean b, int zreb) {
//checking if the barrier is still there
if (x<-bitmap.getWidth()){
//'b'is passed from the Barriermanager - 'update' method, determining if we have to use monkey-true or farmer-false
int raz = 200;
int vyska = BM.dpos;
//'zreb' helps me to randomly determine if monkey or ballard is going to appear
//here it determines if we are going to have Top or Bottom obsticle in the game
if(zreb == 1) {
vyska = BM.dpos - raz;
}
else {
vyska = BM.dpos + raz;
}
//koniec tohto trienedia
if (b)
{
//setting the y value for the top wall
y = vyska - BM.dl/2 - bitmap.getHeight()/2;
}
else{
//setting the y value for bottom wall
y = vyska + BM.dl/2 + bitmap.getHeight()/2;
}
//setting x-value
x = (int) (x +bitmap.getWidth()*(BM.TopWalls.size()-1));
}
x = (int) (x - BM.game_panel.ShipSpeed*dt);
}
Do you have any idea why this "one-or-the-other" condition is not working properly?
This would help me lot because this error made me deactivate the app from the store.
I'm relatively new to programming, and i'm trying to create a program that creates a purple ball where i click that moves to the right until it is off the screen, where i can have unlimited balls on the screen at once. I've made a program that does this, but i can only have one on the screen at once, if i click a second time, the first ball disappears and is replaced with a new one. Oh, and when i click a second time, the ball doesn't start where the cursor is, it starts from where the last ball was on the X-Axis.
Help please!
Here's the code:
int moveX, moveY;
void setup() {
background(255);
moveY = 200;
moveX = 0;
size(500,400);
}
void mouseClicked() {
moveY = mouseY;
moveX++;
}
void draw() {
if (moveX >= 1){
background(255);
fill(255, 0, 255);
ellipse(moveX, moveY, 40, 40);
moveX++;
}
}
As donfuxx suggests, giving each ball it's own coordinates.
One way to do this is using an array to store multiple values(coordinates).
To do this you need to get familiar with for loops and arrays.
They may look scary at first, but once you get the hang of them they're quite easy.
Wherever you can think of a situation where repetition is needed, you can use a for loop to make your life easier.
For loop have the following syntax:
for keyword (3 elements: a start point,an end point(condition) and an increment,(separated by the ; character)
Let's say you want to move from a(0) to b(10) one step at a time:
for(int currentPos = 0 ; currentPos < 10; currentPos++){
println("step: " + currentPos);
}
If you can walk, you can also skip :)
for(int currentPos = 0 ; currentPos < 10; currentPos+=2){
println("step: " + currentPos);
}
even backwards if you want:
for(int currentPos = 10 ; currentPos > 0; currentPos--){
println("step: " + currentPos);
}
This is very useful when traversing all sort of data(coordinates of a ball in a scene, etc.)
How do you organize your data ? You place it in a list or array.
An array contains elements of the same type and has a set length.
The syntax to declare an array is like so:
ObjectType[] nameOfArray;
and you can initialize an empty array:
int[] fiveNumbers = new int[5];//new keyword then the data type and length in sq.brackets
or you can initialize the array with values:
String[] words = {"ini","mini","miny","moe"};
You access elements in an array using square brackets and the index of the object in the list you want to access. Arrays have a length property so you can easily count objects.
background(255);
String[] words = {"ini","mini","miny","moe"};
for(int i = 0 ; i < words.length; i++){
fill(map(i,0,words.length, 0,255));
text(words[i],10,10*(i+1));
}
Now back to your original question.
Here is your code using for loops and arrays:
int ballSize = 40;
int maxBalls = 100;//maximum number of balls on screen
int screenBalls = 0;//number of balls to update
int[] ballsX = new int[maxBalls];//initialize an empty list/array of x coordinates
int[] ballsY = new int[maxBalls];//...and y coordinates
void setup() {
size(500, 400);
fill(255, 0, 255);
}
void mouseClicked() {
if (screenBalls < maxBalls) {//if still have room in our arrays for new ball coordinates
ballsX[screenBalls] = mouseX;//add the current mouse coordinates(x,y)
ballsY[screenBalls] = mouseY;//to the coordinate arrays at the current ball index
screenBalls++;//increment the ball index
}
}
void draw() {
println(screenBalls);
background(255);
for (int i = 0 ; i < screenBalls; i++) {//start counting from 0 to how many balls are on screen
ballsX[i]++;//increment the x of each ball
if(ballsX[i]-ballSize/2 > width) ballsX[i] = -ballSize/2;//if a ball goes off screen on the right, place it back on screen on the left
ellipse(ballsX[i], ballsY[i], ballSize, ballSize);//display each ball
}
}
There are multiple ways to tackle this. Arrays have fixed size. If you don't want to be constrained by that you can use an ArrayList (sort of a variable size array). Later you might want to look into how you can make an object that can update and draw itself. Have fun!
First of all i want to say that my English isnt that good, and I'm a beginner programmer. So take it easy with me :L
So I'm making a 2D game where ground is randomly spawned. And its made of blocks...
How I do this is first i create the blocks and then I add them to Rectangle ArrayList. Blocks render correctly. But they won't take any collision when player hits them.
At this moment collision doesn't work at all. When i press D (right) player runs towards right ignoring collision complitely. When i press A (left) player don't move at all.
First I make this ArrayList:
static ArrayList<Rectangle> BlockArray = new ArrayList<Rectangle>();
Then I give blocks their X,Y,Width,Height... values in a for loop and after that I add them to the list like this :
BlockArray.add(Block[i]);
Then In player class I run this function every render loop. It should tell if player can move to right or left or none...:
ArrayList<Rectangle> rects = WorldGenerator.BlockArray;
for(int i = 0; i < rects.size(); i++) {
// LEFT
if(player.x >= rects.get(i).getX() + rects.get(i).getWidth() && player.y >= rects.get(i).getY() + rects.get(i).getHeight()){
canGoLeft = true;
}
else{
canGoLeft = false;
}
// RIGHT
if(player.x <= rects.get(i).getX() && player.y >= rects.get(i).getY() + rects.get(i).getHeight()){
canGoRight = true;
}
else{
canGoRight = false;
}
}
And then finally when user gives input it checks if those booleans are true or not :
if (Gdx.input.isKeyPressed(Keys.A) && canGoLeft == true){
player.x -= delta * 350f;
}
if (Gdx.input.isKeyPressed(Keys.D) && canGoRight == true){
player.x += delta * 350f;
}
So thats my code. Hopyfully I didn't forget to mention something. And hopefully someone can help me to solve this problem. Also Like I said before I'm beginner at programming so I might have just a stupid fail in game logic...
As far as your collision logic goes, you are changing canGoRight and canGoLeft for each rectangle regardless of previous collision checks. This means that effectively, the last rectangle in your list is the only one being checked for collisions.
To resolve that issue, you'll want to change it to be like this (I just added a ! to the conditions, you should rework them rather just inverting the final result):
ArrayList<Rectangle> rects = WorldGenerator.BlockArray;
canGoLeft = true;
canGoRight = true;
for(int i = 0; i < rects.size(); i++) {
// LEFT
if(!(player.x >= rects.get(i).getX() + rects.get(i).getWidth() && player.y >= rects.get(i).getY() + rects.get(i).getHeight())) {
canGoLeft = false;
}
// RIGHT
if(!(player.x <= rects.get(i).getX() && player.y >= rects.get(i).getY() + rects.get(i).getHeight())) {
canGoRight = false;
}
}
This way, you assume they can move in a given direction, and any single rectangle that would block that direction will prevent movement that direction.
EDIT: I also looked into your conditions, and it looks like any rectangle to the right of the player will prevent them going to the left, not just a rectangle whose right side is up against the left side of the player. A much better comparison would be the difference between the player's and rectangle's half-widths and half-heights. This article can explain in detail why.
As far as bigger picture goes, I mentioned in a comment that collision detection has already been written many times, and libgdx includes solid collision detection code that you should use, rather than writing your own collision detection code.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have these classes which is the 3rd and 4th child of a base class in my game:
/*
* List of player states:
* * normal * explode */
public class LocalPlayer extends MovingObjects{
//minus 1 laps to start to accommodate going over the start line for the first time
public int lapsCompleted = -1;
public LocalPlayer(int localPlayerNumber) {
/*
* This line below and the variable localPlayerNumber will not be needed or will be different
* if later on decide to do custom rocket designs - not needed for this stage but bear in mind if the decision is
* made to go down that road this and the game object will have to be reconsidered as early as possible.
*/
super(16, "assets/images/player" + localPlayerNumber , ".jpg", new Vector(373, 450 + (55 * localPlayerNumber)));
}
//basic constructor just initialises MovingObjects Variables to zero values
public LocalPlayer(){
super();
}
///// Uploading Position Methods
public void update(){
if(objectState == "explode"){
}
if(objectState == "normal"){
super.update();
//look for edge of map and stop player leaving and reduce speed to 0
if(position.x > rightEdge - icon.getIconWidth()){
position.x = rightEdge - icon.getIconWidth();
speed = 0;
}else{
if(position.x < leftEdge){
position.x = leftEdge;
speed = 0;
}
}
if(position.y > downEdge - icon.getIconHeight()){
position.y = downEdge - icon.getIconHeight();
speed = 0;
}else{
if(position.y < upEdge){
position.y = upEdge;
speed = 0;
}
}
}
}
///// Movement Methods
//Increases speed
public void up(){
if(speed == 0){
speed = 2;// 2 is minimum speed to achieve all driving angles
}else
if(speed < 11){
speed++;
}
}
//Decreases speed
public void down(){
if(speed > 2){
speed--;
}else{
speed = 0;
}
}
//Turns image and angle 22.5 degrees to the right
public void right(){
if(angle == 337.5)
{
angle = 0;
imageNumber = 0;
}else{
angle = angle + 22.5;
imageNumber = imageNumber + 1;
}
}
//Turns image and angle 22.5 degrees to the left
public void left(){
if(angle == 0)
{
angle = 337.5;
imageNumber = 15;
}else{
angle = angle - 22.5;
imageNumber = imageNumber - 1;
}
}
// works out start grid currently only vertical lines
//add more levels either start all the same way or needs updating
public Vector getStartPos(int serverNumber, CheckPoints line)
{
int row ;
Vector vReturn;
if (serverNumber % 2 == 0) {
// even
row = serverNumber/2;
//this needs some explaining:
//vectorX = row * width of image * gap to next player
//vectorY = getline y and inset slightly(will see how goes)
vReturn = new Vector((line.pos1.x + 10 ) - row * (50 + 10), line.pos1.y + 5);
} else {
// odd
row = (serverNumber + 1)/2;
vReturn = new Vector((line.pos2.x +10) - row * (50 + 10), line.pos2.y - 55);
}
return vReturn;
}
}
and:
import java.awt.image.BufferedImage;
public class NetworkedLocalPlayer extends LocalPlayer{
RocketSpecificServer server = new RocketSpecificServer();
public NetworkedLocalPlayer(String ipAddress,int numOfImages,String filePre, String fileE, CheckPoints finishLine) {
//sign player into server
server.setUpNetwork(ipAddress);
LoadContent.gameSettings = (GameSettings) server.signIn();
//get server number (needed for initialising)
serverNumber = Integer.parseInt(((StringReturnSerial)server.getServerNumber()).s);
//this is temp and should be changed later!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//filePre = filePre + serverNumber;
filePre = filePre + 1;
//Initialise image variables
imageVariables(numOfImages,filePre,fileE);
//work out pos
position = getStartPos(serverNumber,finishLine);
//get images
initImageArray();
}
public void update(){
//updates players info table checks the state of this object
//hasn't been changed by another player
LoadContent.serverPlayerInfo =(PlayerPositionsSerial) server.getPos();
objectState = LoadContent.serverPlayerInfo.playersArray[serverNumber][4];
//usual update
super.update();
//updates server
LoadContent.serverPlayerInfo = (PlayerPositionsSerial) server.updatePos(angle, position.x, position.y,objectState);
}
}
The problem is the update method. In the NetworkedLocalPLayer player class it asks the server for a serialised class which holds all the players positions in the game (and someother bits) that is assigned to the static version in LoadContent class which implements my gameloop. the update method then checks its own position in the playertable array to update its own objectState variable which I can see when using breakpoints comes back as its supposed to as "normal". It then call the parents update method which starts executing and will stop on line in the LocalPlayer class :
if(objectState == "normal"){
the next line is :
super.update();
which should call the super update method of the MovingObjects class which i can also provide if you think it will help but bassically the I have a break opint inside the MovingObjects class and on the super.update() call in the LocalPlayer class which never get hit.
When i change a variable in the game loop to turn multiplayer off which has nothing to do with this but directly used the localPlayer class as it is this method fire perfectly fine and has done unchanged for ages. So is there a reason I cant use super like this? im now getting any errors or exceptions
Thank you
for any help John harris
Never compare Strings with ==. Do it like this (assuming objectState cannot be null - otherwise you could write "explode".equals(objectState) but I find it harder to read):
public void update(){
if(objectState.equals("explode")){
}
if(objectState.equals("normal")){
super.update();
// ...
}
}
If using Java 7, you can also switch on Strings (have a look at the changes introduced with Java 7).
You problem is that objectState == "normal" is always false.
Use "normal".equals(objectState) instead
I know this has been asked a lot but I'd like to know how to rotate a Tetris piece?
I already made a long and bad solution (~170 lines of code) but there should be easier way to do it.
My tetris pieces consist of 4 blocks which all know their location (row and column) in the matrix. Matrix itself is char-typed, so 4 blocks are all letters. It looks like this for example:
......
..T...
.TTT..
......
I tried to simulate my matrix as coordinate system by counting the middle row and column and using it as an origo and then tried to apply this simple algorithm I found:
90 degree rotation (x,y) = (-y,x)
It appears that it works only if my piece is in the center of matrix. I have no idea what I should do, I've been thinking this all day. Here's my method:
public void rotatePiece(ArrayList<Block> random) {
int distance = 0; // how far is the origo
for (int i=0; i < 4; ++i)
board[random.get(i).getRow()][random.get(i).getColumn()] = '.'; // erases the current location of the piece
for (int i=0; i < 4; ++i) {
distance = Math.abs(random.get(i).getColumn()-middleColumn);
if (random.get(i).getColumn() < middleColumn)
random.get(i).setColumn(random.get(i).getColumn()+(distance*2)); // "throws" the location of the block to the other side of the origo
else
random.get(i).setColumn(random.get(i).getColumn()-(distance*2));
int help = random.get(i).getColumn();
random.get(i).setColumn(random.get(i).getRow()); // (x, y) = (-y, x)
random.get(i).setRow(help);
}
for (int i=0; i < 4; ++i)
board[random.get(i).getRow()][random.get(i).getColumn()] = random.get(0).getStyle(); // saves the new location of the piece in the matrix
I would recommend defining four states for each block-group.
enum ROTATION {
UP, DOWN, LEFT, RIGHT;
ROTATION rotateLeft() {
switch(this) {
case UP: return LEFT;
case LEFT: return DOWN;
case DOWN: return RIGHT;
case RIGHT: return UP;
}
return null; // wont happen;
}
ROTATION rotateRight() {
ROTATION r = this;
// wow I'm lazy, but I've actually seen this in production code!
return r.rotateLeft().rotateLeft().rotateLeft();
}
}
abstract class Brick {
Point centerPos;
ROTATION rot;
abstract List<Point> pointsOccupied();
}
class TBrick extends Brick {
List<Point> pointsOccupied() {
int x = centerPos.x();
int y = centerPos.y();
List<Point> points = new LinkedList<Point>();
switch(rot) {
case UP: points.add(new Point(x-1,y);
points.add(new Point(x,y);
points.add(new Point(x+1,y);
points.add(new Point(x, y+1);
break;
case Down: points.add(new Point(x-1,y);
points.add(new Point(x,y);
points.add(new Point(x+1,y);
points.add(new Point(x, y-1);
break;
// finish the cases
}
}
}
You can use a rotation matrix.
You will need to set the origin of your rotation appropriately, which may mean translating the location of the piece with respect to the playing field (such that the origin is in the centre, for example), applying the rotation matrix and then translating it back to its correct location on the playing field coordinates.
The easiest and computational fastest way to do this, would to use precompute them.
That means a tetris piece will look like
class TetrisBlock {
String position[4];
int curPos = 0;
void rotateLeft() {
curPos++;
if (curPos > 3)
curPos = 0;
}
....
}
And then you could define something like
class TetrisTBlock extends TetrisBlock {
...
// in constructor
position={"....\n.T..\nTTT.\n....",
".T..\nTT..\n.T..\n.....",
// I guess you get the idea
...
You do this for every type of block and then you can also add members for adding/removing them from the board.
If you optimize you would go away from the chars....
I think the best way is to hard-code it. Take into consideration that each figure is different and each of the figure's rotation phase is also different. And for each rotation phase - determine which parts of the grid you need to be free (avoid collision).
For a visual representation check this