I'm making a platformer where the player is controlled by the keyboard (awsd) and the aim is controlled by the mouse, but it doesnt shoot straight to the crosshair. It seems it only have like 20 directions to shoot, and that it chooses the one closest to the CH.
Actually, it's only got 32 directions, not 20, which is still pretty shit anyway. You can make a custom 360 degree movement if you're any good at trigonometry, or look for a 360 shooting tutorial, I think there's a few. If it's MMF2 you're using, you can use the Vector movement for really easy 360 degree shooting.
There are plenty of examples of 360 degree shooting.
..and I haven't seen one that works. MMF2 just doesn't allow enough decimal points on floats for it to move in truly 360 degrees (or more). The best I could get was a kind of 'homing missile'
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.
Assuming you mean places, it has enough for pretty much anything we're likely to need it for. My guess would be you were applying the calculations immediately to the position of the object, which means you lose all your fractions every loop. Common mistake.
Well, where do I put the calculations then? I'd have to find a place that accepts floats and MMF 2 seems to just mix freely the actions that only calculate in integers with the floats. Is there like a document somewhere that shows which ones use floats and which use integers?
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.
What I found works is to run all my calculations based on "absolute positions" which I then translate into less precise pixel positions every program loop. By that I mean that if my game runs in 320x240 resolution, I'd have alterable values for x and y positions that run from 320,000 to 240,000 instead. Then you run all your calculations off those values and at the end of everything, set the objects x,y co-ordinates on screen to that value divided by 1,000. When the next program loop occurs, all your absolute positions are still accurate. Collision detection is still "only" pixel perfect, but nobody will notice and it won't effect game-play.
(edit): As for the actual 360-degree shooting. Do something like this:
Player presses 'fire' button
- create bullet object at player (hotspot) x,y position
- set alterable value bullet x,y position to bullet x,y (x1000)
- set alterable values for x and y movement per loop based on the angle that the cursor is away from the object times the speed of the object. You'll need some trig equations for this, but basically you're taking the overall speed of the bullet and making that the hypotenuse of your triangle, and then figuring out the ratio of the sides for the x and y position changes per loop (it's been a couple years since I took a trig class but you can find this stuff online and it's easy).
- Next, you just tell MMF to update the bullets absolute x,y positions by the amount of it's x,y movement alterable values every time the program loop runs. Then, once it's done that, take the absolute x,y positions and divide them by 1,000 to get their position on screen.
The advantage of using custom shooting engines is that you have significantly more control (read: you can actually do something to them) over your bullets once they've been fired. Using more advanced math, you can create homing missiles this way, or have a weapon that fires projectiles that follow a sine wave on their way to the target. You can also make them move faster than the maximum built into MMF. Keep in mind though that if you do, you'll have to work up a collision detection system that'll account for this. A good way to do that is through fast loops that, much like the sub-pixel positioning, actually move the bullet several times within each program loop before updating the screen.
If you haven't already done so, you should also consider creating a custom platform movement for your play so that he doesn't get stuck in ceilings and what-not. The built in platform movement in MMF is utter crap, and it'll improve the quality of your game tremendously if you create your own movement engine. Unlike the 360-shooting, there ARE several tutorials on this site under the "articles" tab that'll explain exactly how to do this.
Originally Posted by Benta Claus . __ . You calculate positions in alterable values then set the object to those values. Just a thought.
I think I tried that. Didn't seem to work.
Lol, Kirby. Trig doesn't work well in MMF2. Not in my code at least :/. The only way I found that worked was setting the X,Y of the intended target of the bullet, and making it always 'look' in that direction using one of those direction objects. After it's in the location of the target, make it look in the direction relative from the shooter to the bullet (i.e. if bullet it 26 degrees from shooter, set bullet to moving at 26 degrees, not looking at target anymore) so they don't become homing missiles. I think a few other people who did '360 degree' engines also used the same method. It's a bit too accurate when shooting long distance, but you can add a random number when setting the X,Y, so that should fix it .
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.