Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / problem getting my 'laser' to go in the right direction

Author
Message
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 5th Jun 2013 08:56
Hello,

I've been trying to work out this code with mixed results.


Basic premise is a space ship firing lasers in whatever direction its facing

spriteCounter = the laser thats about to be fired
3 = the sprite for the players ship

I use some code to point the players ship wherever the pointer is clicking at the moment and it works perfectly.

I thought I would use this code to find the angle to shoot the laser at but its not working.

I can either get it to do the following:
- shoot 1 direction
- shoot various directions but NOT exactly where the ship is pointing

What should be the relevant portion of code :



trig is not my strong point so I assume I'm making a math error but I could also just be conceptualizing it wrong also?

Any tips welcome! Thanks!

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 5th Jun 2013 10:56
I wonder if RADians are running amock somewhere, maybe the Atanfull command.

What I'd expect is to at least have a consistent direction - it might not be exact, like you might need to subtract the angle from 180 or something like that, but to have totally random results would lead me to check if one of those commands is expecting radians.

Radians would exaggerate the angle you see, so even though an angle difference might be just 5 degrees - if the command is expecting radians, it'll be more like 40 degrees.

That's the only reason I can think that you'd have such spurious results.

I got a fever, and the only prescription, is more memes.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 5th Jun 2013 11:38 Edited at: 5th Jun 2013 11:42
i think you forget declare float for this.
spX =
spY =

and all the variable names are not good.

this you can replace
velocityX = (sprite_hit_X - agk::GetSpriteXByOffset(spriteCounter) * FORCE); // multiply the sprite hit point by the velocity
velocityY = (sprite_hit_Y - agk::GetSpriteYByOffset(spriteCounter) * FORCE);

=

velocityX = spX * FORCE;
velocityY = spY * FORCE;

a better name for sp is direction or dirX dirY

the direction you can get without sin/cos (the delta/distance you already use for ATanFull)
dx#=x2#-x1#
dy#=y2#-y1#
length#=sqrt(dx#*dx# + dy#*dy#)
if length#<1.0 then length#=1.0
dirX#=dx#/length#
dirY#=dy#/length#

//split force in x y
forceX#=dirX# * force#
forceY#=dirY# * force#

#=float
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 5th Jun 2013 13:17
The thing I've noticed about using Atanfull in conjuction with sin and cos is that the angle given by Atanfull seems start at a different point than sin and cos. I find that with Atanfull, the unit circle is goes clockwise with 0 degrees at 12 o'clock. I believe sin and cos also go clockwise but their 0 starts at 3 o'clock. So there's a difference of 90 degrees.

With this line:

Try adding 90 to it:


If that doesn't work, try subtracting 90.

If that doesn't work...listen to someone else.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 5th Jun 2013 13:24
@Hodgey
about different point :
you can also exchange sin/cos or the sign.
i can't remember me, i try each time i need this.

@erebusman
if you want shoot with ship angle, the sprite image/ship nose should look
up.
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 5th Jun 2013 17:00
Thanks for all the great suggestions , heading out to work but I will try these out this evening!

erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 6th Jun 2013 06:31 Edited at: 6th Jun 2013 07:21
Okay back from work and I played with all of the suggestions

We have the winning code with some comments about some things that worked, didnt work and adjustments that were needed

Thank you to everyone for your contribution:



Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Jun 2013 10:27 Edited at: 6th Jun 2013 13:13
??? why, it rotate the laser sprite
SetSpritePhysicsAngularVelocity

i made a example


next example
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 7th Jun 2013 06:29
@Markus

The code samples you have seem to both do the same thing to me?

However I think I know what you mean.. does the laser sprite spin around in circles?

I noticed the same behavior so I made the sprite round and it looks sort of like a "photon torpedo" from Star Trek.

I would like to also have a straight 'laser beam' so I like the idea you are using too, I may have to try that out.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Jun 2013 09:08 Edited at: 7th Jun 2013 09:09
i try a longer sprite but the sprite angle did not hit the mouse pointer target!?
the same problem you wrote at top.
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 7th Jun 2013 09:58 Edited at: 7th Jun 2013 09:59
If you want to use a blank sprite to create a laser, I have found it better to create a horizontal line for the sprite not a vertical one and put the offset at the left.

That way the angle of zero degrees is in line with the maths of sin/cos. - i.e. pointing to the right.
Chris Harries
10
Years of Service
User Offline
Joined: 13th Jun 2013
Location:
Posted: 13th Jun 2013 09:51
The thing I've noticed about using Atanfull in conjuction with sin and cos is that the angle given by Atanfull seems start at a different point than sin and cos. I find that with Atanfull, the unit circle is goes clockwise with 0 degrees at 12 o'clock. I believe sin and cos also go clockwise but their 0 starts at 3 o'clock. So there's a difference of 90 degrees.

Login to post a reply

Server time is: 2024-04-28 19:53:42
Your offset time is: 2024-04-28 19:53:42