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 / how to use SetSpritePhysicsVelocity

Author
Message
codemypantsoff
12
Years of Service
User Offline
Joined: 21st Jul 2012
Location:
Posted: 21st Jul 2012 19:40
I have an object that i want to push in a direction once at a given angle - then gravity will take over

I have tried



however i really am not getting an accurate result

here is the use case i have a cannon that fires downward so say my cannon is pointed to 220 degrees i want to apply a physics velocity that will make it shoot out of the cannon in the right direction - after it is fired it will fall the same as any other object but have that arc of being fired.

Hope that clear its too early to be doing this stuff
basjak
14
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 21st Jul 2012 21:17
This command will last only for one frame rate. You should put it in a loop.

codemypantsoff
12
Years of Service
User Offline
Joined: 21st Jul 2012
Location:
Posted: 22nd Jul 2012 02:54
Basjak.....
I got to that point and i only want to apply a huge force once to the bullet ... my real question is how do i get an x force and y force value to send it in the right direction?
ICERGB
22
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Canada
Posted: 22nd Jul 2012 07:22 Edited at: 22nd Jul 2012 07:30
SetSpritePhysicsImpulse might be better for shooting a cannonball.

I was using it to allow a person to use a finger to flick a ball.
codemypantsoff
12
Years of Service
User Offline
Joined: 21st Jul 2012
Location:
Posted: 22nd Jul 2012 08:01
So how do i find the 4 points needed for setspritephysicsimpulse thats the part i dont quie get
ICERGB
22
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Canada
Posted: 22nd Jul 2012 08:38 Edited at: 22nd Jul 2012 08:40
I made a program to test it but which would be the best way to do it but I integrated it into a big program so I can't show you.

I think the basics of it are
SetSpritePhysicsImpulse( iSpriteIndex, x, y, vx, vy )

x and y is the point on the sprite you want hit so I used
sprite20+getspritewidth(20)/2 (same for sprite height)

the vx and vy (based on a hit of 5000)
// where up impulse up is negative; impulse right is positive
// impulse left negative and down is positive

so a sprite you want to shoot at 90 degrees would be
vx=5000 and vy=0

45 degrees would be
vx=2500 and vy=-2500
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Jul 2012 09:03 Edited at: 22nd Jul 2012 09:04
Velocity Is the right way to go here. Impulse is more for a constant force acting on an object. Setting the velocity gives more of a cannonball being fired effect. I used it to fire Bob in Cannonball Bob...

If you are using impulse the first x,y is the point in space to apply the force, the second set is the strength of the force. This means you can apply forces from a point away from your sprite to simulate fans blowing and similar forces.

Edit: looking at your code it could be that you need to make the Y component negative as positive goes down the screen...?


this.mess = abs(sin(times#))
codemypantsoff
12
Years of Service
User Offline
Joined: 21st Jul 2012
Location:
Posted: 22nd Jul 2012 23:58
hey thanks guys - okay here is the layout

at the top of the screen i have the 'cannon' and you can move it from 90 to 270 (pointing down). I am trying to propel the 'bullet' in the direction the 'cannon' is facing - I can get the SetSpritePhysicsVelocity command to fire it correctly but not sure how to fire it at an angle (i know 1000 in the x and 1000 in the y will get me a 135 degree angle and -1000 in the x and 1000 in the y will get me a bullet at 225 degrees but how do i figure out say 115 degrees)

I know its a trig problem and solution but its been too many years since i had to do these type of problems
ICERGB
22
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Canada
Posted: 23rd Jul 2012 10:42 Edited at: 23rd Jul 2012 10:45
Yeah I did it the hard way...




etc

Login to post a reply

Server time is: 2024-11-23 19:59:39
Your offset time is: 2024-11-23 19:59:39