A challenge! (To see how more experienced programmers would solve this problem.)

FlammusLucis

  • Posts: 74
Hello! I might be a little too new to be posting a challenge, but I would like to know how more experienced stencylers would do this.

So for the challenge:
In my game I wanted to be able to throw a knife, so that it spun in the air and continued forward until it hit a wall. If the blade of the knife hit the wall, then it would stop spinning and stick there. However; if the handle hit, then it would bounce off and fall to the ground. (Also, there needs to be a mechanism that kills the knife in some way, so that the scene doesn't get cluttered with a lot of actors.)

I will be posting how I did this. :)

(Edit: attached how I did this.)

« Last Edit: January 13, 2016, 12:46:38 pm by FlammusLucis »

stefan

  • *
  • Posts: 2263
Well that was easy haha =p

Create an image with a knife pointing up and give it a circle collision model. This hasn't been tested and might need some minor changes such as the rotation range in the collision event (I cant recall if positive rotation was clock wise), but you should get the idea ;)

You will probably have to add some other stuff as well, but that depends on what you are planning to do with it.


ManicZombie

  • Posts: 126
Out of curiosity, can you fill me in on what this does?

If direction of Self > 359
point Self towards direction of Self - 360 degrees

All the rest I can follow. Thanks!

stefan

  • *
  • Posts: 2263
Because of the 45 - 90 degree check.
If I dont set the rotation to 0 after it surpasses 359, i will never get into the 45 - 135 range as it then would be something like 405 - 495.

ceosol

  • *
  • Posts: 2279
You could also point self towards "remainder of (direction of self/360)" at the time of collision. Say the direction was 2610, the remainder of 2610/360 is 90.

tigerteeth

  • Posts: 733
I love that "remainder of" block

stefan

  • *
  • Posts: 2263
I never knew that block did that haha....
I always thought it would return something like this:

5 / 2 = 2.5
Remainder of 5 / 2 being 0.5

I probably have to re-consider my opinion about that block :v

ceosol

  • *
  • Posts: 2279
I never knew that block did that haha....
I always thought it would return something like this:

5 / 2 = 2.5
Remainder of 5 / 2 being 0.5

I probably have to re-consider my opinion about that block :v

Yeah, the remainder of 5/2 would be 1. It takes what is left over after the rounded value is subtracted out. Floor of 5/2 = 2, 2*2 = 4, 5-4 = 1.

stefan

  • *
  • Posts: 2263
Yeah i realise that now. Thanks for the heads up =)

tigerteeth

  • Posts: 733
In my grid-based movement system, I have a rule where the main actor won't stop moving until his X and Y positions are both divisible by 32, which uses that block. It's just a great block.

hendriza01

  • *
  • Posts: 82
Learn't something new, will definitely be using that block a lot more myself as well, thanks guys :)

FlammusLucis

  • Posts: 74
Thank you for the replies! I did it by using animations, and checking which animation the knife was at when it collided.
(I'm attaching my game to the original post.)

Rimrook

  • Posts: 251
knife projectile has two hitboxes for handle and and blade. Basically check which one collides with the wall then run the script for whichever. It's not really that difficult. I can't imagine this being any more complicated than that.

A truer stylistic approach would to have an animation of the knife spinning with all sorts of artistic flare, then randomly stick the knife or not. Why would the exactness of it matter, and a random of two results would be more even than a simulation where conditions can sway the outcome to either side. But that's a loose thought on the subject.