How to fix this collision detection? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to fix this collision detection? [duplicate]



This question already has an answer here:



I'm trying to create a simple Space Invaders game in JavaFX. This is my first time so I have no idea how you properly do this. I'm trying to add collision detection, to remove a sprite if it's hit by a bullet.



All my sprites are rectangles with an imagePattern.



The enemies are 60 x 43, the bullet is 4 x 8. My idea was to just check if the bullet is within the translateX of a sprite, and if its the same Translate Y - its height. But for some reason this code doesn't work. It causes an Exception in thread "JavaFX Application Thread" java.lang.NullPointerException when a bullet hits a sprite.


Exception in thread "JavaFX Application Thread" java.lang.NullPointerException


if (game.lookup("#bullet") != null)
for (Sprite sprite : enemies)
if (game.lookup("#bullet").getTranslateX() >= sprite.getTranslateX() && game.lookup("#bullet").getTranslateX() <= sprite.getTranslateX() + 60 && game.lookup("#bullet").getTranslateY() == sprite.getTranslateY() - 43)
System.out.println("HIT!");





Some help would be much appreciated.



Note: I am sure this is not the most efficient way to go about it, but its the only idea I have.



The stack trace:


Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at Main$1.handle(Main.java:131)
at javafx.graphics/javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$0(AnimationTimer.java:57)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:357)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:514)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:319)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Please post the exception stack trace
– Karol Dowbecki
Aug 10 at 9:33





Not sure if thats readable.. I can post my entire code if you need it too.
– Behar
Aug 10 at 9:37






That is the if statement, however anon fixed that error. It turns out i had simple forgotten a # symbol in the game.lookup(bullet) statement. However after fixing that, now the detection dosent work at all, the "HIT!" message is never printed, even tho the bullet passes thru all the sprites. And its weird because before the excpetions only began when the bullet actually hit a sprite.
– Behar
Aug 10 at 9:43





1 Answer
1



One of your "bullet" keys seems to be missing its '#'.





Waw, so i fixed that, and now the error is gone. But it still does not print the "HIT!" message when a sprite it hit. Which is weird because the error only began when a sprite was hit before, so it kinda seemed to work.
– Behar
Aug 10 at 9:39





You have an upper and a lower bound for X, but only a single Y value - are you sure that bullets can't pass beyond the sprite boundary (e.g. depending on speed)?
– anon
Aug 10 at 9:47





Fundamentally speaking, I strongly advise you to introduce enums or named constants and local variables with clear names. Fetching your bullet once with a constant key would have prevented the exception in the first place. Same applies for your height / width constants. Cumbersome typing avoids even more cumbersome errors.
– anon
Aug 10 at 9:51





Hey anon, yea as i mentioned im sure there are much better ways to do this. But im a beginner in programming, ill be sure to check out the stuff you mentioned tho. That being said the error was exacly what you said, i increased the translate Y in my bullet by 8 every frame, so i obviously should not have used the == operator but instead the > operator. Thank you so much for your help anyway!
– Behar
Aug 10 at 9:56

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard