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 can I get nonlinear distance from an object with physics.

Author
Message
brunuu
5
Years of Service
User Offline
Joined: 4th Jun 2018
Location:
Posted: 4th Mar 2020 18:57
Normal physical formulas will not work very well here due to the physics setup. because of the command SetPhysicsScale
setPhysicsGravity(0, 100)
SetPhysicsScale(0.01)

the objective is to obtain the distance covered by an object that has free movement.

like this
Mesher
User Banned
Posted: 4th Mar 2020 19:17
To solve for distance use the formula for distance d = st, or distance equals speed times time.

distance = speed x time

Rate and speed are similar since they both represent some distance per unit time like miles per hour or kilometers per hour. If rate r is the same as speed s, r = s = d/t. You can use the equivalent formula d = rt which means distance equals rate times time.

distance = rate x time

To solve for speed or rate use the formula for speed, s = d/t which means speed equals distance divided by time.

speed = distance/time

To solve for time use the formula for time, t = d/s which means time equals distance divided by speed.

time = distance/speed

Good luck
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 4th Mar 2020 22:45 Edited at: 4th Mar 2020 22:52
some pseudocode:

thats about the only way you can do it, i believe. and, you'll need some tweaking to find an acceptable "accuracy".

not sure that what mesher said applies to your inquiry but good reference, otherwise.
nz0
AGK Developer
16
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 4th Mar 2020 22:56
A simple way would be to do a direct distance calculation accumulator every frame between last position and current position.

(Just read Virtual Nomad's post and we appear to agree )

You could optimise it slightly by checking if the new position=old position and skip the distance formula.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 4th Mar 2020 23:37 Edited at: 5th Mar 2020 00:09
worth adding since i don't know what the project might also entail but if there is any collision involved, simple currentxy# - oldxy# will be misleading/potentially (and thoroughly) inaccurate.

consider a sprite with a velocity of 1000 pointed at a wall 500 units away. it could run into the wall and come back to where it started which would give you a LastMove# of 0.0 ; see this extreme example of what i mean.

if this kind of thing might affect you, you'd be looking at tracking each contact point, applying sprite offsets (or otherwise account for shapes involved), and the distance formula to each"step" of movement in a given frame.

btw, when you venture into the velocities anywhere near those in the vid, the "system" gets "iffy".

IE, i'm checking for contact between the ball and the paddle. if it hits the bottom wall, the ball goes red (which you see plenty of) while i've confirmed that it has actually hit the paddle (where the ball should go green, and doesn't) much of the time (i've got that on auto-pilot, obviously, which leaves only small gaps at the edges for the ball to hit the bottom).

so, it reacts properly (physics-wise) but the contact data retrieved can't be trusted.

add: make sure to review this since you've been focused on box2d lately, if you haven't already. i found it somewhat insightful where i went in completely blind, otherwise
nz0
AGK Developer
16
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 5th Mar 2020 00:09
Quote: "consider a sprite with a velocity of 1000 pointed at a wall 500 units away. it could run into the wall and come back to where it started which would give you a LastMove# of 0.0 ; see this extreme example of what i mean.
"


I see what you mean.
Depends what the OP exactly wants and if any collisions are involved.

I wouldn't trust using data you send to the physics engine to match what you expect to see on the screen. "Iffy" is not a strong enough word to describe half of the things I've seen happen with it

brunuu
5
Years of Service
User Offline
Joined: 4th Jun 2018
Location:
Posted: 5th Mar 2020 00:25 Edited at: 5th Mar 2020 00:26
yes there are collisions, that's why I was trying to use physics formulas

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th Mar 2020 00:51
then you're probably on the same road i am with the g-breaker project which is, given the AppGameKit implementation of box2d, venturing outside of its intended use.

in that project, i've abused what's available and getting to the point where i should write my own physics from scratch to account for what i'm trying to squeeze out of box2d (i'm adjusting velocities manually, each frame, barely hanging on to a "natural" feeling, for example).

but, if you're intent on pushing forward, consider some pre-calculations (my next step, if i continue with g-breaker/am up for the challenge) which would probably include time(s) of impact and 2d rays to help but, then, borders on the edge of the "do it manually/from scratch" notion.

if it's not more much that what you've depicted in the last image (balls vs walls), it shouldn't be too terribly hard. i just haven't done it personally.

regardless, good luck
blink0k
Moderator
11
Years of Service
Recently Online
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 5th Mar 2020 01:15
Quote: "i'm adjusting velocities manually, each frame"

Are you using linear velocity or impulse?
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th Mar 2020 02:28
don't want to hijack this thread. check ur PM
brunuu
5
Years of Service
User Offline
Joined: 4th Jun 2018
Location:
Posted: 5th Mar 2020 02:41
well, i did it, i think it works decent.

the distance traveled by the object starts to count when the mouse is released.


Mesher
User Banned
Posted: 5th Mar 2020 23:41
Well done
blink0k
Moderator
11
Years of Service
Recently Online
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 6th Mar 2020 02:29
An interesting exercise might be to save those points in an array and use DrawLine() to see how accurate it is

Login to post a reply

Server time is: 2024-04-20 00:04:50
Your offset time is: 2024-04-20 00:04:50