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 / A little help with Physics and opposing forces

Author
Message
BuccaneerBob
AGK Developer
14
Years of Service
User Offline
Joined: 18th Aug 2010
Location: Canada
Posted: 28th Feb 2017 03:41
Hey folks!

Trying out a small app using 2D Physics, but running into an issue, I have 2 sprites on the screen and I'm trying to make Sprite A. repel Sprite B.
IE - The closer Sprite A gets to Sprite B; Sprite B runs away.

Using physics is completely new to me though, I've got Physics On, so the sprites will fall to the bottom of the screen by default and I'm fairly confident that what I need is a force pushing against a sprite based on the position of the Sprite A.

I was thinking something like, I'm just not sure what I would use to calculate X,Y as the opposing force? Thoughts?
SetSpritePhysicsForce (SpriteB GetSpriteXByOffset(SpriteA),GetSpriteYByOffset(SpriteA),X,Y)
StepPhysics(1.0/60.0)

Thanks All

Rob
Robert Janes (Samu Games)
http://www.samugames.com/artifact
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 28th Feb 2017 07:31 Edited at: 28th Feb 2017 07:33
like this ? (1 and 2 is your sprite pos.)
dx=x2-x1
dy=y2-y1

l = sqrt(dx*dx + dy*dy)
if l<1.0 then l=1.0

this n xy give you a value bewteen -1 and 1 you can multiply with a force value
nx = -dx / l
ny = -dy / l
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 7th Mar 2017 00:30
this is how you set what sprites do with physics
SetSpritePhysicsOn (spr,1=static, 2=dynamic, 3=kinematic )

the gravity can be controlled with
SetPhysicsGravity(x,y)
x - The X component of the gravity vector.
y - The Y component of the gravity vector
for no gravity SetPhysicsGravity(0,0)

The bounciness and how they interact with other physics
SetSpritePhysicsRestitution( iSpriteIndex, restitution(between 0 and 1) )
a number greater than 1 is not recommended
fubar

Login to post a reply

Server time is: 2024-09-29 23:36:17
Your offset time is: 2024-09-29 23:36:17