Decelerate without using any behaviours, only events

Get help using Construct 2

Post » Fri Dec 11, 2015 5:33 am

I created a sprite sensor and have pinned it to an object, lets call it "Object A".
While the sensor is overlapping with another sprite, I want the "Object A" to decelerate from its current motion into a full stop.
At the moment it is abruptly stopping, and I cant seem to make good use of lerp.

Is there anyway that I can decelerate to a full stop, or smoothly ease into a full stop, from its current speed? Any ideas?

Is it possible to check the speed of a sprite without behaviors, only in event sheet?

Thanks.
Brick by brick, stone by stone.
B
56
S
25
G
18
Posts: 273
Reputation: 13,363

Post » Fri Dec 11, 2015 6:45 am

Here's how I like to do deceleration. You add or subtract from the speed depending if it's positive or negative, and the min/max is to prevent acceleration after passing 0.

if speed>0
--- set speed to max(speed-deceleration*dt,0)
If speed<0
--- set speed to min(speed+deceleration*dt,0)

You can use any value for deceleration. If you want it to stop in a certain distance you could use this formula:
Deceleration=(starting_speed^2)/(2*distance)
You could probably set that under a on collision event.


If you want to check the speed of a Sprite from events the easiest way would be to read it from a behavior expression or if you're moving with events you hopefully have a speed variable to control how fast it goes.
Besides that you can find the speed by keeping track of the object's previous position and calculating the distance from the current position. After that you divide by dt.
Speed=distance/time

Global number oldx=0
Global number oldy=0

...
// bottom of the event sheet
Every tick
--- set text to "speed: "& distance(Sprite.x,Sprite.y, oldx,oldy)/dt
--- set oldx to Sprite.x
--- set oldy to Sprite.y
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Fri Dec 11, 2015 10:18 am

@R0J0hound

Phew, it finally worked. Been messing with all sorts of values for the last 3.5 hours with breaks, but finally got some values that did it. At first I wasn't sure what was wrong, the problem being as it turned out I was entering values that were too small considering the*dt factor.

Cant get used to it so far but I surely will with time. My values are all over the place unfortunately. Need to rework some events.

So basically I am using the *dt values everywhere now, but isn't there a simpler way just to enter that once at the beginning, the dt conversion and then use it once?
If its not simpler then no worries, this is good anyhow, just glad that the code is running so far!

Thanks again!
Brick by brick, stone by stone.
B
56
S
25
G
18
Posts: 273
Reputation: 13,363

Post » Fri Dec 11, 2015 11:33 am

One more question. Is it possible that Im getting a laggy movement because of all the "*dt" in the equations?

I have an equation: speed1+speed2+(12*60)*dt

Variable values: speed1 = 20*60*dt
speed2 = 10*60*dt

So in the end it turns out to be: (20*60*[dt])+(10*60*[dt])+(12*60)*[dt]

(I put the dt in brackets for a clearer read).


Now as you see I have a hell lot of "*dt", and I'm getting somewhat laggy movement, but I not sure if its my browser (as I was getting lags before, as I did post some earlier threads, but I don't mean to derail this thread).

I'm going to have to test it on another computer I suppose to make sure.

Whats your take on it? Am I using the dt correctly in this situation? Or should there be only 1 "dt" in the final equation?

Thanks once again for the help.
Brick by brick, stone by stone.
B
56
S
25
G
18
Posts: 273
Reputation: 13,363

Post » Fri Dec 11, 2015 5:42 pm

I'm not sure you need dt at all in those equations. Speed is just a number, but when you apply a speed to position then you apply dt. In the same way you'd use dt when applying an acceleration to a speed.

speed1 = 1200
speed2 = 600

total_speed=speed1+speed2

//applying speed to move an object
sprite: set x to self.x+speed*dt

//applying acceleration to change speed
add acceleration*dt to speed
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Sat Dec 12, 2015 5:54 am

@R0J0hound

If that is the case how do I go comparing a value that has *dt to a value that doesn't have *dt?

For example:
If Speed>100*dt

If the variable Speed does not have *dt this comparison will not work properly.
I think Ill stick to the leaving the dt everywhere, at least for now. We shall see how it goes.

Do you think this might have a big impact on memory, FPS and performance?
Brick by brick, stone by stone.
B
56
S
25
G
18
Posts: 273
Reputation: 13,363

Post » Sat Dec 12, 2015 9:44 am

You could use system compare two values in that case?

Adding the dt in your expression when comparing..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sat Dec 12, 2015 10:14 pm

If that is the case how do I go comparing a value that has *dt to a value that doesn't have *dt?

For example:
If Speed>100*dt

Using dt there doesn't make sense. Speed should be the same regardless of dt. With that if the fps is 60 the comparison is speed<1.66, but if the fps drops to 50 then it will be speed<2.

"dt" should only be used where it's needed, and it's only needed in cases where you want to know how much a value will change in a length of time if it's going at a certain rate. Or more simply in equation form:
rate*time = amount

Some examples:
You want a object to move 100 pixels per second:
x = x+ 100*dt

you want an object to rotate 30 degrees per second:
angle= angle+ 30*dt

you want a speed to accelerate by 10 pixels/sec^2.
speed = speed + 10*dt
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993


Return to How do I....?

Who is online

Users browsing this forum: richcoz81, Studio Mercato and 38 guests