Collision detection in a game loop? -
I am working on a simple 2D game (bird eye view). I have projectiles, which are circles. There are obstacles around the level of the game, all of which are rectangular (axis alliance). I would like projectiles to bounce the obstacles when they collide. I'm not sure how to implement it in my game loop. It is being speculated that something like this would happen:
zero gameplay () {Fenceli P = ..; P.updateLocation (p.getVelocity (), p.getDirection ()); Inner barrier = p.intersects (barriers); If (intersected! = Null) {// we hit an obstacle after going on tick this time // Figure 1 where // reflection should be our refurbished space now off. Point ptNew = intersected.reflect (p.getLastPoint (), P. jetvelocity (), PJet Direction ()); // Our new location after bounced block ptNew.setLocation (ptNew); }} So after we throw the ball, we can check whether we are hitting one of the obstructions (or completely inside), if so, So let us make a calculation to find out that our reflected position must be removed from the block in accordance with our starting point and velocity / direction.
Thanks
-------- - Update ------------------------ < A little more specific - Eric's comments are given, I need to make sure the projectile bounces properly, I can not pass them through the obstacle if their speed is so fast. So they reach a single game loop iteration.
In that case, I think that I need to test a contrary point, speed, direction, as opposed to all obstacles (possibly repeatedly) until further intersections are not possible for speed. Something like this:
zero gender () {faintly p = ...; Barrier barrier = movement progeclaim (p, block); While (obstacle! = Null & amp; p.stillHasVelocityThisFrame ()) {Bayer = HillProxycle (P, Obstacles); } // At this point, the projectile is bouncing / moving around. } Zero Step projectilelet (projection projectile, list & amp;; Bayer & gt; barriers) {for (barrier barrier: obstacles) {// tbd barrier transferred = test2dVectorAgainstRectangle (Prosecu. // test of vector // above projectile) To see if it pierces any obstruction, if necessary, turn it off, modify the // position of projectile, and reduce the available distance, // still on this frame Travel to If intersected {return intersected;}} // no intersections return tap;} Yes, it is already becoming difficult. < P> Thanks
You want to use vector mathematics to check your image and collision. It is clear that if you are on one side of one line on the other side of the line (on the edge of the world rectangle) on 1 tick and on the other side it is quite easy to find that only you can check One touch and will also issue of confrontation with the math. Then you have a point to reflection which, again using vectors. Plotting plot with other objects is a bit driver, but still can be done. Make a list of all the conflicts within a loop and find those efforts that come with their movement path - again use some dot product magic. Or, if you find out many conflicts, you can always tick each object before it stays and apply conflicts from there.
One thing you would never want to do is to move an object to another object. This allows you to get stuck on one another and work very accurately - depending on how you do your collision code.
Comments
Post a Comment