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

2020/10/11

Check collision before adding

1
2
3
4
Roshan123 Roshan123

2020/10/11

#
Plz write a program to check collision If red team destroys blue team, then blue team will be removed from the world and it will be added again to the world again within 1sec. But i want it to add randomly(i was successful in it...) but after adding it to the world it should not interesting with any actor.class (i don't know how to do it, so plz tell me)
RcCookie RcCookie

2020/10/11

#
The simplest way I can think of is that it simply checks collisions AFTER it was spawned and if it collides, it sets itself to a new random location. Just put that in a while loop as long as it intersects something and you should be good.
//Inside of the class

@Override
protected void addedToWorld(World w) {
    while(getOneIntersectingObject(null) != null) {
        //set a new random location. You may wanna use your own code here
        setLocation((int)(Math.random() * w.getWidth()), (int)(Math.random() * w.getHeight()));
    }
}
The only risk here is that if there is no possible location or just very few, this could just loop forever (or at least very long). Therefore, you can add a variable kinda like a for loop that also counts up to limit the possible iterations:
final static int MAX_ITERATIONS = 100;

int i = 0;
while(getOneIntersectingObject(null) != null && i++ < MAX_ITERATIONS) {...}
if(i == MAX_ITERATIONS) w.removeObject(this);
RcCookie RcCookie

2020/10/11

#
If you have other code in addedToWorld, put this to the top and add ‚return‘, if the object can’t find a location to prevent it from executing the other code that might not me meant to be executed when it’s not added
Roshan123 Roshan123

2020/10/12

#
Will this scenario lag after implementing ur 1st idea I have no idea about it so can u guess that will it lag or not becaz at every 8 seconds 1 tank will be destructed (so the loop will run at every x seconds to average second i.e. 8 seconds) After playing the scenario plz guess will it lag or work efficiently or is their any other idea which you would be sure enough that it will not lag the scenario
RcCookie RcCookie

2020/10/12

#
You should definitely use the second version where it is limited to 100 trys. That version should not lag. Btw here is the complete second version:

static final int MAX_ITERATIONS= 100;

@Override
protected void addedToWorld(World w) {
    int i = 0;
    while(getOneIntersectingObject(null) != null && i++ < MAX_ITERATIONS) {
        //set a new random location. You may wanna use your own code here
        setLocation((int)(Math.random() * w.getWidth()), (int)(Math.random() * w.getHeight()));
    }
    if(i == MAX_ITERATIONS) {
        w.removeObject(this);
        return;
    }
}
Roshan123 Roshan123

2020/10/12

#
And what about the text which u were asking If u need the score board then i may post it
Roshan123 Roshan123

2020/10/12

#
And last question Plz tell me that is the scenario lagging on ur device???
RcCookie RcCookie

2020/10/12

#
It’s not
Roshan123 Roshan123

2020/10/12

#
If u don't mind then will u share me the speed of the processor I was doing it on 2.20Hz But it was lagging a little Actually i made surplus classes in that scenario Will the lag remove by writing like this:-Before //In world Addobj(tank); Addobj(tank2); Addobj(Bot_tank); Addobj(Bot_tank2); Addobj(counter....blablabla...); After In world If greenfoot.iskeydown 1 Addobj(tank); Addobj(counter); Addobj(Bot_tank....bla...bla...); If greenfoot.iskeydown 2 Addobj(tank2); Addobj(Bot_tank2); Addobj(counter....blablabla...); Note:their r more classes which i haven't mentioned So plz kindly share me the speed of the processor and also tell me that will the idea work to stop lag on certain operating system
RcCookie RcCookie

2020/10/12

#
I don’t understand the point of that - yes, it does run faster when adding them only under a certain condition, but THEN THEY ARE ONLY ADDED UNDER A CERTAIN CONDITION. Also, adding objects is usually not the source of lag, if it only happens at the beginning. Lag comes from extensive runtime calculations like constant collision checking or input checks.
RcCookie RcCookie

2020/10/12

#
In terms of my specs, I’m only on a laptop: Intel i7 7th generation (2.7 GHz). If you want to compare performance, on my Sandbox Universe 2D scenario I’m getting - if nothing else is running - about 90-95 FPS.
RcCookie RcCookie

2020/10/12

#
Online
Roshan123 Roshan123

2020/10/17

#
Is their any other way Its Not working......
Roshan123 Roshan123

2020/10/17

#
Anybody plz reply.....
DatHeroAndy DatHeroAndy

2020/10/17

#
Roshan123 wrote...
Plz write a program to check collision If red team destroys blue team, then blue team will be removed from the world and it will be added again to the world again within 1sec. But i want it to add randomly(i was successful in it...) but after adding it to the world it should not interesting with any actor.class (i don't know how to do it, so plz tell me)
So, if I am understanding this correctly, you don't want to use getOneIntersectingObject(), right?
There are more replies on the next page.
1
2
3
4