This site requires JavaScript, please enable it in your browser!
Greenfoot back
Roshan123
Roshan123 wrote ...

2020/10/27

Explanation in need

Roshan123 Roshan123

2020/10/27

#
I have seen a lot many discussion Most of the users use getoneinsertingobj for collision(i mean, according to me what i searched for...) But why they don't use range I have used it for better collision and its working perfectly Can anyone explain me :- 1which one is better 2 what are the demerits
danpost danpost

2020/10/27

#
Roshan123 wrote...
I have seen a lot many discussion Most of the users use getoneinsertingobj for collision(i mean, according to me what i searched for...) But why they don't use range I have used it for better collision and its working perfectly Can anyone explain me :- 1which one is better 2 what are the demerits
(1) they both are useful and use would depend on the specifics of the behavior wanted; (2) demerits are only prevalent when use is inconsistent with what is wanted; getOneIntersectingObject is not the most used. Actually, it is getIntersectingObjects or isTouching ( which is really equivalent to getIntersectingObjects(<class>) != null ) that are most used. At any rate, these are true collision methods, where getObjectsInRange does not have to be (although useful for the collision of round objects). getObjectsInRange can be used for limiting "sight" as well as collision. It is also possible to use multiple "collision" methods to form a non-standard hit box area (one that is neither rectangular, diamond-shaped nor round. My Hitbox Visualizer scenario was an attempt at showing as much.
RcCookie RcCookie

2020/10/27

#
There are actually quite a few ways to check collisions: getOneIntersectingObject(Class) ➡️ Returns one object of the specified class that GRAPHICALLY intersects this actor, or null if there is no getIntersectingObject(Class) ➡️ Returns a list of actors that GRAPHICALLY intersect this actor. If there are no, the list will be empty getOneObjectAtOffset(int, int, Class) ➡️ Returns one object that GRAPHICALLY intersects the tile at the specified offset of the actors location, or null if there is no getObjectsAtOffset(int, int, Class) ➡️ Returns a list of actors that GRAPHICALLY intersect the tile at the specified offset of the actors location. If there are no, the list will be empty getNeighbours(int, boolean, Class) ➡️ Returns a list of actors that are POSITIONED in the specified distance when going out tile by tile. The boolean effects weather diagonal tile „walking“ is allowed getObjectsInRange(int, Class) ➡️ Returns a list of actors which are at most the given distance from the LOCATION of this actor away isTouching(Class) ➡️ Returns true if this actor GRAPHICALLY intersects any object of the given class Additionally, there are some methods that check the state of collision of you already have the actors to compare: intersects(Actor) ➡️ Returns true if this actor GRAPHICALLY intersects the given actor For this you need to use the class ActorVisitor because it is not visible: ActorVisitor.containsPoint(Actor, int, int) ➡️ Returns true if the image of the given actor contains the given point. Note that the pixel coordinate is not in cells but in pixels
Roshan123 Roshan123

2020/10/27

#
What should i write in the act method to use actor visitor???
Roshan123 Roshan123

2020/10/27

#
And what about neighbour What happens if i will write false or true @RcCookie i was not able to understand the role of boolean in it
danpost danpost

2020/10/27

#
Roshan123 wrote...
And what about neighbour What happens if i will write false or true @RcCookie i was not able to understand the role of boolean in it
I updated the scenario. I was not aware my hit boxes were miss-aligned. You can see how neighbors works with both true and false by playing around with it. false gives a diamond shape hit box (a square on end) and true give an upright square hitbox that the diamond can be inscribed in.
RcCookie RcCookie

2020/10/27

#
getNeighbours has three parameters: int: range - The maximum steps to take to reach another actor boolean: diagonal - Weather diagonal steps are allowed. If not, each step can go from the current cell either up, down, left or right. If diagonal is allowed, it takes only one step to reach a cell that is only touching at the corner.
RcCookie RcCookie

2020/10/27

#
Example usage: getOneIntersectingObject: Collide with another actor
1
2
3
4
5
Player other = getOneIntersectingObject(Player.class);
if(other == null) return;
float vX = other.getSpeedX(), vY = other.getSpeedY();
other.setSpeed(getSpeedX(), getSpeedY());
setSpeed(vX, vY);
getIntersectingObjects: Count how many objects intersect
1
int count = getIntersectingObjects(null).size();
getOneObjectAtOffset: Move only if the tile to move to is not occupied
1
2
if(getOneObjectAtOffset(getSpeedX(), getSpeedY(), Obstacle.class) != null) return;
setLocation(getX() + getSpeedX(), getY() + getSpeedY());
getObjectsAtOffset: Delete all objects in front of the player
1
2
3
4
5
for(Actor a : getObjectsAtOffset(10, 0, null)) {
    getWorld().removeObject(a);
}
// Or simply
getWorld().removeObjects(getObjectsAtOffset(10, 0, null));
getNeighbours: Color all tiles next to a certain one, but not diagonally
1
2
3
4
5
6
for(Tile t : getWorld().getObjects(Tile.class)) {
    t.paintToColor(Color.BLACK);
}
for(Tile t : getNeighbours(1, false, Tile.class)) {
    t.paintToColor(Color.RED);
}
getObjectsInRange: Turn all objects in a range of 10 towards this actor
1
2
3
for(Actor a : getObjectsInRange(10, null)) {
    a.turnTowards(getX(), getY());
}
isTouching: Loose the game if you touch an obstacle
1
if(isTouching(Obstacle.class)) Greenfoot.stop();
intersects: Check if two objects intersect each other and if so color them differently
1
2
3
Player a, b;
if(a.intersects(b)) setColors(Color.RED);
else setColors(Color.BLUE);
containsPoint: Check weather the mouse touches an actors image
1
2
3
4
5
6
public boolean touchesMouse() {
    try {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        return ActorVisitor.containsPoint(this, MouseInfoVisitor.getPx(mouse), MouseInfoVisitor.getPy(mouse));
    } catch(Exception e) { return false; }
}
Roshan123 Roshan123

2020/10/28

#
1
2
3
4
5
6
for(Tile t : getWorld().getObjects(Tile.class)) {
    t.paintToColor(Color.BLACK);
}
for(Tile t : getNeighbours(1, false, Tile.class)) {
    t.paintToColor(Color.RED);
}
Their is no method called paintToColor I think it needs some packages to be imported
Roshan123 Roshan123

2020/10/28

#
import java.awt.Color*; Is it correct or incorrect???
RcCookie RcCookie

2020/10/28

#
No, that are just example methods that show what should be done in a certain case. These methods are: getSpeedX() getSpeedY() setSpeed() paintToColor() setColors() They just indicate what could be implemented right there
You need to login to post a reply.